本文档为个人博客文档系统的备份版本、作者:小游、作者博客:点击访问
我们想获取到这个框架缓存的图片,如何实现?考虑使用下面的方法。
FileBinaryResource resource = (FileBinaryResource)Fresco.getImagePipelineFactory().getMainFileCache().getResource(new SimpleCacheKey(url));
InputStream is= resource.openStream();
bitmap bm=BitmapFactory.decodeStream(is));
在安卓6.0后,我们要使用一些权限的时候需要动态申请,参考文章
https://blog.csdn.net/dfqin/article/details/55190073
1.先检查是否有这个权限
int permissionCheck =ContextCompat.checkSelfPermission(thisActivity,Manifest.permission.WRITE_CALENDAR);
如果有权限则返回PackageManager.PERMISSION_GRANTED,否则返回PackageManager.PERMISSION_DENIED
2.没有权限的时候我们需要申请权限
ActivityCompat.requestPermissions(thisActivity,new String[]{Manifest.permission.READ_CONTACTS}, REQUEST_CODE)
MediaStore.Images.Media.insertImage(mcontext.getContentResolver(),BitmapFactory.decodeStream(is),"image_file","file");
不过这个方法在API29以后都不推荐使用了,虽然我api29的版本并不是很普及,但是我还是想用官方推荐的方法。
国内找了半天,没找到一个靠谱的。。。最后还是直接在Stack Overflow里面找到了解决方法。
https://stackoverflow.com/questions/36624756/how-to-save-bitmap-to-android-gallery/57265702?fbclid=IwAR3ao3-EXYztWeEfOhIaFE553MX74yC6o-Smi2A38kwlI6MrHpm74yTC4IU#57265702
不过别人这个使用kotlin写的。不过反正可以相互调用,所以自己单独保存为kotlin文件就可以了
private fun saveImage(bitmap: Bitmap, context: Context, folderName: String) {
if (android.os.Build.VERSION.SDK_INT >= 29) {
val values = contentValues()
values.put(MediaStore.Images.Media.RELATIVE_PATH, "Pictures/" + folderName)
values.put(MediaStore.Images.Media.IS_PENDING, true)
// RELATIVE_PATH and IS_PENDING are introduced in API 29.
val uri: Uri? = context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
if (uri != null) {
saveImageToStream(bitmap, context.contentResolver.openOutputStream(uri))
values.put(MediaStore.Images.Media.IS_PENDING, false)
context.contentResolver.update(uri, values, null, null)
}
} else {
val directory = File(Environment.getExternalStorageDirectory().toString() + separator + folderName)
// getExternalStorageDirectory is deprecated in API 29
if (!directory.exists()) {
directory.mkdirs()
}
val fileName = System.currentTimeMillis().toString() + ".png"
val file = File(directory, fileName)
saveImageToStream(bitmap, FileOutputStream(file))
if (file.absolutePath != null) {
val values = contentValues()
values.put(MediaStore.Images.Media.DATA, file.absolutePath)
// .DATA is deprecated in API 29
context.contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values)
}
}
}
private fun contentValues() : ContentValues {
val values = ContentValues()
values.put(MediaStore.Images.Media.MIME_TYPE, "image/png")
values.put(MediaStore.Images.Media.DATE_ADDED, System.currentTimeMillis() / 1000);
values.put(MediaStore.Images.Media.DATE_TAKEN, System.currentTimeMillis());
return values
}
private fun saveImageToStream(bitmap: Bitmap, outputStream: OutputStream?) {
if (outputStream != null) {
try {
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream)
outputStream.close()
} catch (e: Exception) {
e.printStackTrace()
}
}
}
参考这个https://blog.csdn.net/xypeng123/article/details/95595944
在module的build.gradle下添加下面语句:
apply plugin: 'kotlin-android'
apply plugin: 'kotlin-android-extensions'
还是报错
那是需要我们制定以下版本,解决方法如下
根目录的build.gradle中加上下面这两条
成功解决问题
最后的结果
if(ContextCompat.checkSelfPermission(mcontext, Manifest.permission.WRITE_EXTERNAL_STORAGE)== PackageManager.PERMISSION_GRANTED){
try {
String url=loveWechat.getForeground()!=null?wechat:alipay;
FileBinaryResource resource = (FileBinaryResource) Fresco.getImagePipelineFactory().getMainFileCache().getResource(new SimpleCacheKey(url));
InputStream is= resource.openStream();
ImgServer imgServer=new ImgServer();
if(imgServer.saveImage(BitmapFactory.decodeStream(is),mcontext,"pay")){
Toast.makeText(mcontext,"保存成功!",Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(mcontext,"保存失败!",Toast.LENGTH_SHORT).show();
}
is.close();
} catch (IOException e) {
Toast.makeText(mcontext,"保存失败!",Toast.LENGTH_SHORT).show();
}
}else{
Toast.makeText(mcontext,"需要对应的权限哦!",Toast.LENGTH_SHORT).show();
ActivityCompat.requestPermissions(Objects.requireNonNull(getActivity()),new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},101);
}
}else{
Toast.makeText(mcontext,"请等待图片加载!",Toast.LENGTH_SHORT).show();
}
保存图片的权限:
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />