Android6.0覆盖安装错误

机型MI4
Android版本:6.0.1MMB29M
MIUI版本V7.2.11.0.MXDCNDB (MIUI7)

现象: 应用保存图片到相册,保存失败。

保存图片代码:

Bitmap bitmap = event.bitmap;
String uriString = MediaStore.Images.Media.insertImage(getContentResolver(), bitmap, "", "");
if (uriString != null) {
  Uri uri = Uri.parse(uriString);
  sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + getFilePathByContentResolver(this.getApplicationContext(), uri))));
}

代码第二句,uriString返回null。查看日志发现错误:

04-28 16:00:58.323  8553  9226 E MediaStore: Failed to insert image
04-28 16:00:58.323  8553  9226 E MediaStore: java.io.FileNotFoundException: No such file or directory
04-28 16:00:58.323  8553  9226 E MediaStore:    at android.database.DatabaseUtils.readExceptionWithFileNotFoundExceptionFromParcel(DatabaseUtils.java:144)
04-28 16:00:58.323  8553  9226 E MediaStore:    at android.content.ContentProviderProxy.openAssetFile(ContentProviderNative.java:621)
04-28 16:00:58.323  8553  9226 E MediaStore:    at android.content.ContentResolver.openAssetFileDescriptor(ContentResolver.java:962)
04-28 16:00:58.323  8553  9226 E MediaStore:    at android.content.ContentResolver.openOutputStream(ContentResolver.java:704)
04-28 16:00:58.323  8553  9226 E MediaStore:    at android.content.ContentResolver.openOutputStream(ContentResolver.java:680)
04-28 16:00:58.323  8553  9226 E MediaStore:    at android.provider.MediaStore$Images$Media.insertImage(MediaStore.java:965)
04-28 16:00:58.323  8553  9226 E MediaStore:    at com.aurora.grow.android.activity.act.ResourceDetailActivity.onEventAsync(ResourceDetailActivity.java:1057)
04-28 16:00:58.323  8553  9226 E MediaStore:    at java.lang.reflect.Method.invoke(Native Method)
04-28 16:00:58.323  8553  9226 E MediaStore:    at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:498)
04-28 16:00:58.323  8553  9226 E MediaStore:    at de.greenrobot.event.EventBus.invokeSubscriber(EventBus.java:492)
04-28 16:00:58.323  8553  9226 E MediaStore:    at de.greenrobot.event.AsyncPoster.run(AsyncPoster.java:46)
04-28 16:00:58.323  8553  9226 E MediaStore:    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1113)
04-28 16:00:58.323  8553  9226 E MediaStore:    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:588)
04-28 16:00:58.323  8553  9226 E MediaStore:    at java.lang.Thread.run(Thread.java:818)
04-28 16:01:00.763  4808  6332 E octvm_drv: common drv: cpu_autosave_handle -> not implemented

错误分析:待分析……

修改方案:将图片保存到指定文件夹,然后通知图库更新。保存成功。

private void saveImageToGallery(Context context, Bitmap bmp) {
		// 首先保存图片
    File appDir = new File(Environment.getExternalStorageDirectory(), "picture");
    if (!appDir.exists()) {
        appDir.mkdir();
    }
    String fileName = System.currentTimeMillis() + ".jpg";
    File file = new File(appDir, fileName);
		try {
			FileOutputStream fos = new FileOutputStream(file);
			bmp.compress(Bitmap.CompressFormat.JPEG, 100, fos);
			fos.flush();
			fos.close();
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}

		// 把文件插入到系统图库
		try {
			MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), fileName, null);
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		}
		// 最后通知图库更新
		context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath())));
	}
Table of Contents