1.场景
在android开发中保存应用的图片并插入到系统图库同时通知相册刷新的功能,做完后发现在部分手机上出现虽然图片保存成功了,但是相册却找不到图片的问题,查找文件夹图片也已经存在,可就是在相册里刷新不出来。
2.思路
2.1.保存图片的方法
public static File saveImage(Bitmap bmp) {File appDir = new File(Environment.getExternalStorageDirectory(), "zzs");if (!appDir.exists()) {appDir.mkdir();}String fileName = System.currentTimeMillis() + ".jpg";File file = new File(appDir, fileName);try {FileOutputStream fos = new FileOutputStream(file);bmp.compress(CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
}
以上代码便是将Bitmap保存图片到指定的路径/sdcard/Boohee/下,文件名以当前系统时间命名,但是这种方法保存的图片没有加入到系统图库中
2.2.调用系统提供的插入图库的方法
MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "title", "description");
调用以上系统自带的方法会把bitmap对象保存到系统图库中,但是这种方法无法指定保存的路径和名称,上述方法的title、description参数只是插入数据库中的字段,真实的图片名称系统会自动分配。看似上述这种方法就是我们要用到的方法,但是可惜的调用上述放法插入图库的方法图片并没有立刻显示在图库中,而我们需要立刻更新系统图库以便让用户可以立刻查看到这张图片。
2.3.调用系统提供的插入图库的方法
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));
上面那条广播是扫描整个sd卡的广播,如果你sd卡里面东西很多会扫描很久,在扫描当中我们是不能访问sd卡,所以这样子用户体现很不好,所以下面我们还有如下的方法:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(new File("/sdcard/Boohee/image.jpg"))););
或者还有如下方法:
final MediaScannerConnection msc = new MediaScannerConnection(mContext, new MediaScannerConnectionClient() { public void onMediaScannerConnected() { msc.scanFile("/sdcard/Boohee/image.jpg", "image/jpeg"); } public void onScanCompleted(String path, Uri uri) { Log.v(TAG, "scan completed"); msc.disconnect(); }
});
上面代码的图片路径不管是通过自己写方法还是系统插入图库的方法都可以很容易的获取到。
2.4.终极完美解决方案
如果我想把图片保存到指定的文件夹,同时又需要图片出现在图库里呢?sdk还提供了这样一个方法:
MediaStore.Images.Media.insertImage(getContentResolver(), "image path", "title", "description");
上述方法的第二个参数是image path,这样的话就有思路了,首先自己写方法把图片指定到指定的文件夹,然后调用上述方法把刚保存的图片路径传入进去,最后通知图库更新。
3.解决办法
(1)创建文件路径可选择Environment.getExternalStorageDirectory(),也就是(/storage/emulated/0/com.xx.xxx.xxx/),之前有问题的版本使用的是context.getExternalFilesDir(null)也就是(/storage/sdcard/Android/data/com.xxx.xxx/),部分手机相册无法找到此路径或者没有权限
// 首先保存图片File appDir;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {//android11以后appDir = getExternalFilesDir(null);}else {appDir = new File(Environment.getExternalStorageDirectory(), "zzs"); }if (!appDir.exists()) {appDir.mkdir();}String fileName = System.currentTimeMillis() + ".jpg";File file = new File(appDir, fileName);try {FileOutputStream fos = new FileOutputStream(file);bmp.compress(CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}
(2)保存的方法添加写入的动态权限,把文件插入到系统图库
//把文件插入到系统图库try {MediaStore.Images.Media.insertImage(this.getContentResolver(),file.getAbsolutePath(), fileName, null);} catch (FileNotFoundException e) {e.printStackTrace();}
(3) 通知图库更新,使用MediaStore插入到系统相册,使用广播Intent.ACTION_MEDIA_SCANNER_SCAN_FILE通知相册刷新
// 通知图库更新if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {String path = file.getAbsolutePath();Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri uri = Uri.fromFile(new File(path));intent.setData(uri);sendBroadcast(intent);} else {String relationDir = file.getParent();File file1 = new File(relationDir);sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.fromFile(file1.getAbsoluteFile())));}
完整代码
public void saveImageToGallery(Context context, Bitmap bmp) {// 首先保存图片File appDir;if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) {//android11以后appDir = getExternalFilesDir(null);}else {appDir = new File(Environment.getExternalStorageDirectory(), "zzs"); }if (!appDir.exists()) {appDir.mkdir();}String fileName = System.currentTimeMillis() + ".jpg";File file = new File(appDir, fileName);try {FileOutputStream fos = new FileOutputStream(file);bmp.compress(CompressFormat.JPEG, 100, fos);fos.flush();fos.close();} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}//把文件插入到系统图库try {MediaStore.Images.Media.insertImage(this.getContentResolver(),file.getAbsolutePath(), fileName, null);} catch (FileNotFoundException e) {e.printStackTrace();}// 通知图库更新if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {String path = file.getAbsolutePath();Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE);Uri uri = Uri.fromFile(new File(path));intent.setData(uri);sendBroadcast(intent);} else {String relationDir = file.getParent();File file1 = new File(relationDir);sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED,Uri.fromFile(file1.getAbsoluteFile())));}}