Android将base64图片保存到本地

                                                                     

 需求:将base64图片保存到图库,并且查看

1.             第一步,因为是外部存储,所以需要动态申请权限   

new RxPermissions(mActivity).request(Manifest.permission.WRITE_EXTERNAL_STORAGE)  
        .subscribe(new Consumer<Boolean>() {
            @Override
            public void accept(Boolean aBoolean) throws Exception {

        });

  2             第二步将base64转成bitmap

因为base64转bitmap是耗时操作,最后放在子线程中

Observable.create((ObservableOnSubscribe<Bitmap>) e -> {

    这里需要注意,如果是标准的base64的字符串,需要截取,这里需要注意下
    byte[] decode = Base64.decode(base64.split(",")[1], Base64.DEFAULT);  

    Bitmap bitmap = BitmapFactory.decodeByteArray(decode, 0, decode.length);
    e.onNext(bitmap);
}).observeOn(AndroidSchedulers.mainThread()).subscribeOn(Schedulers.io())
.subscribe(new Consumer<Bitmap>() {
    @Override
    public void accept(Bitmap bitmap) throws Exception {
        boolean b = saveBitmap(context, bitmap, filePath);
    }
}, new Consumer<Throwable>() {
    @Override
    public void accept(Throwable throwable) throws Exception {
        saveCallBack.save(false);
    }
});

3.         将bitmap写入文件中,更新图库

public static boolean saveBitmap(Context context,Bitmap bitmap, String path, Bitmap.CompressFormat format, int quality) {
    if (bitmap == null) {
        return false;
    }
    FileOutputStream out = null;
    try {
        boolean newFile = FileUtil.createFile(path);
        if (!newFile) {
            return false;
        }
        out = new FileOutputStream(path);
        boolean compress = bitmap.compress(format, quality, out);
        out.flush();

       //这里记得必须加入更新图片
        Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);
        File savefile = new File(path);
        if (savefile.exists() && savefile.length() > 0) {
            Uri uri = Uri.fromFile(savefile);
            intent.setData(uri);
            context.sendBroadcast(intent);
        }

        return compress;
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (out != null) {
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
    return false;
}

 

这里贴一些方法

 //获取手机相册的路径,拼接图片的名称,返回完整的路径

public static String getDiskCacheImg(String imgName) {
    String cachePath;
    cachePath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES).getAbsolutePath()+File.separator+imgName;
    return cachePath;
}

//创建文件的方法

public static boolean createFile(String path) throws IOException {
    boolean isSuccess = true;
    File file = new File(path);
    File parentFile = file.getParentFile();
    if (parentFile.exists()) {
        if (!parentFile.isDirectory()) {
            isSuccess &= deleteFolder(parentFile.getAbsolutePath());
            isSuccess &= createDirectory(parentFile.getAbsolutePath());
        }
    } else {
        isSuccess &= createDirectory(parentFile.getAbsolutePath());
    }

    if (!file.exists()) {
        isSuccess &= file.createNewFile();
    } else {
        if (file.isFile()) {
            return file.canRead() & file.canWrite();
        }

        isSuccess &= deleteFolder(file.getAbsolutePath());
        if (isSuccess) {
            isSuccess &= createFile(file.getAbsolutePath());
        }
    }

    return isSuccess;
}

 

public static boolean deleteFolder(String sPath) {
    boolean flag = false;
    File file = new File(sPath);
    if (!file.exists()) {
        return true;
    } else {
        return file.isFile() ? deleteFile(sPath) : deleteDirectory(sPath);
    }
}

@SuppressLint({"NewApi"})
public static boolean createDirectory(String path) {
    File dir = new File(path);
    boolean isSuccess = true;
    File parentFile = dir.getParentFile();
    if (parentFile.exists()) {
        if (!parentFile.isDirectory()) {
            isSuccess &= deleteFolder(parentFile.getAbsolutePath());
            isSuccess &= createDirectory(parentFile.getAbsolutePath());
        }
    } else {
        isSuccess &= createDirectory(parentFile.getAbsolutePath());
    }

    if (!dir.exists()) {
        isSuccess &= dir.mkdirs();
    } else {
        if (dir.isDirectory()) {
            return dir.canExecute() & dir.canRead() & dir.canWrite();
        }

        isSuccess &= deleteFolder(dir.getAbsolutePath());
        if (isSuccess) {
            isSuccess &= createDirectory(dir.getAbsolutePath());
        }
    }

    return isSuccess;
}

 

public static boolean deleteFile(String sPath) {
    boolean flag = false;
    File file = new File(sPath);
    if (!file.exists()) {
        return true;
    } else {
        if (file.isFile()) {
            flag = file.delete();
        }

        return flag;
    }
}

 

            

public static boolean deleteDirectory(String sPath) {
    if (!sPath.endsWith(File.separator)) {
        sPath = sPath + File.separator;
    }

    File dirFile = new File(sPath);
    if (!dirFile.exists()) {
        return true;
    } else if (!dirFile.isDirectory()) {
        return false;
    } else {
        boolean flag = true;
        File[] files = dirFile.listFiles();
        if (files != null && files.length > 0) {
            if (null != files) {
                for(int i = 0; i < files.length; ++i) {
                    if (files[i].isFile()) {
                        flag = deleteFile(files[i].getAbsolutePath());
                        if (!flag) {
                            break;
                        }
                    } else {
                        flag = deleteDirectory(files[i].getAbsolutePath());
                        if (!flag) {
                            break;
                        }
                    }
                }
            }

            if (!flag) {
                return false;
            } else {
                return dirFile.delete();
            }
        } else {
            return dirFile.delete();
        }
    }
}

 

具体的文件创建代码   https://download.csdn.net/download/xueyoubangbang/15450937

 


版权声明:本文为xueyoubangbang原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。