读取U盘TXT文件、图片并使用SharedPreferences 保存本地

读取U盘中的.txt文件,获取内容并保存。
首先要先获取U盘路径:

public static String[] getUSBPaths() {
        List<String> pathList = new ArrayList<String>();
        try (BufferedReader bufReader = new BufferedReader(new FileReader(MOUNTS_FILE))) {
            String line;
            while ((line = bufReader.readLine()) != null) {
                String[] splits = line.split(" ");

                String mountPoint = splits[1];
                String fileSystemType = splits[2];

                if (!mountPoint.startsWith("/storage") || mountPoint.contains("emulated")
                        || !fileSystemType.matches("(vfat)|(fuse)|(sdcardfs)")) {
//                    Log.d(TAG, "will continue.point=" + mountPoint);
                    continue;
                }

                pathList.add(mountPoint);
            }
        } catch (IOException e) {
            Log.e(TAG, "", e);
        }
        return pathList.toArray(new String[pathList.size()]);
    }

读取U盘中xxx.txt文件:

public static String readTxtFromUSB(Context context){
        String readStr="";
        String foldername = getUSBPaths()[0] + "/";
        Log.i(TAG, "foldername = " + foldername);
        /*File folder = new File(foldername);
        if (folder == null || !folder.exists()) {
            folder.mkdir();
        }*/
        File targetFile = new File(foldername + USBTxtFileName);
        try{
            if(!targetFile.exists()){
                targetFile.getParentFile().mkdir();
                targetFile.createNewFile();
                return "No File error ";
            } else {
                Log.i(TAG, "targetFile.exists() = " + targetFile.exists());
                InputStream in = new BufferedInputStream(new FileInputStream(targetFile));
                BufferedReader br= new BufferedReader(new InputStreamReader(in, "UTF-8"));
                String tmp;
                int x = 0;
//                     String [] arr = new String[128];
                ArrayList<String> List = new ArrayList<String>();
                while((tmp=br.readLine())!=null){
                    List.add(x, tmp) ;
//                    	 arr[x] = tmp;
                    System.out.println("123+"+List);
//                    	 System.out.println("123+"+arr[x]);
                    x++;
                }
                Log.i(TAG, "tmp = " + tmp);
                br.close();
                in.close();
                readStr =  List.toString();
                Log.i(TAG, "readStr = " + readStr);
            }
        } catch (Exception e) {
            Toast.makeText(context,e.toString(),Toast.LENGTH_LONG).show();
            Log.i(TAG, "e.toString() = " + e.toString());
            return e.toString();
        }
        return readStr;
    }

通过U盘读取xxx.png图片,并转成Bitmap格式:

public static Bitmap readPictureFromUSB(){

        String foldername = getUSBPaths()[0] + "/";
        Log.i(TAG, "foldername = " + foldername);
        File targetFile = new File(foldername + USBPictureName);
        try{
            FileInputStream fs = new FileInputStream(targetFile);
            Bitmap bitmap  = BitmapFactory.decodeStream(fs);
            return bitmap;
        } catch (Exception e) {
            Log.i(TAG, "e.toString() = " + e.toString());
            return null;
        }
    }

封装SharedPreferences工具类对文件进行保存:

public class SharedPreferencesUtils {
    private static SharedPreferencesUtils spUtils;

    private SharedPreferences sp;
    private SharedPreferences.Editor editor;

    public static SharedPreferencesUtils newInstance(Context context) {
        return newInstance(context, TvConstants.SHARE_FILE_NAME);
    }

    public static SharedPreferencesUtils newInstance(Context context, String spName) {
        if (spUtils == null)
            spUtils = new SharedPreferencesUtils(context, spName);
        return spUtils;
    }

    /**
     * SharedPreferencesUtils
     * @param context
     * @param spName
     */
    private SharedPreferencesUtils(Context context, String spName) {
        sp = context.getSharedPreferences(spName, Context.MODE_PRIVATE);
        editor = sp.edit();
        editor.apply();
    }

    /**
     * SP write String type value
     *
     * @param key
     * @param value
     */
    public void putString(String key, String value) {
        editor.putString(key, value).apply();
    }

    /**
     * SP read String
     *
     * @param key
     * @return
     */
    public String getString(String key) {
        return getString(key, null);
    }

    /**
     * SP read String
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public String getString(String key, String defaultValue) {
        return sp.getString(key, defaultValue);
    }

    /**
     * SP write int type value
     *
     * @param key
     * @param value
     */
    public void putInt(String key, int value) {
        editor.putInt(key, value).apply();
    }

    /**
     * SP read int
     *
     * @param key
     * @return
     */
    public int getInt(String key) {
        return getInt(key, -1);
    }

    /**
     * SP read int
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public int getInt(String key, int defaultValue) {
        return sp.getInt(key, defaultValue);
    }

    /**
     * SP write boolean type value
     *
     * @param key
     * @param value
     */
    public void putBoolean(String key, boolean value) {
        editor.putBoolean(key, value).apply();
    }

    /**
     * SP read Boolean
     *
     * @param key
     * @return
     */
    public boolean getBoolean(String key) {
        return getBoolean(key, false);
    }

    /**
     * SP read Boolean
     *
     * @param key
     * @param defaultValue
     * @return
     */
    public boolean getBoolean(String key, boolean defaultValue) {
        return sp.getBoolean(key, defaultValue);
    }

    /**
     * sp write in Bitmap
     * 1、将Bitmap压缩至字节数组输出流ByteArrayOutputStream
     * 2、利用Base64将字节数组输出流中的数据转换成字符串String
     * 3、将String保持至SharedPreferences
     * @param key
     * @param bitmap
     */
    public void putBitmap(String key, Bitmap bitmap){
        ByteArrayOutputStream byteArrayOutputStream=new ByteArrayOutputStream();
        bitmap.compress(Bitmap.CompressFormat.PNG, 50, byteArrayOutputStream);
        String headPicBase64= new String(Base64.encodeToString(byteArrayOutputStream.toByteArray(),Base64.DEFAULT));
        editor.putString(key,headPicBase64);
    }

    /**
     * sp read Bitmap
     * @param key
     */
    public Bitmap getBitmap(String key) {
        String headPic=sp.getString(key,"");
        Bitmap bitmap=null;
        if (headPic!="") {
            byte[] bytes = Base64.decode(headPic.getBytes(), 1);
            bitmap = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
        }
        return bitmap;
    }
}

同时挂载多个U盘的逻辑待完善 _ _
在这里插入图片描述

放张图,说不定有鸡腿,哈哈

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

DerMing_You

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值