解决了: 下面是我的办法 in MediaStoreCompat.java dispatchCaptureIntent(); try to add the code : if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) { mCurrentPhotoUri = Uri.fromFile(photoFile); } else { mCurrentPhotoUri = FileProvider.getUriForFile(context, "com.xxx.xxx.xxx" + ".provider", photoFile); Date date = new Date(); // Create an image file name mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(), mCaptureStrategy.authority, photoFile); String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); String imageFileName = String.format("scv%s", timeStamp); // 兼容android7.0 使用共享文件的形式 ContentValues contentValues = new ContentValues(); contentValues.put(MediaStore.Images.Media.TITLE, imageFileName); contentValues.put(MediaStore.Images.Media.DISPLAY_NAME, photoFile.getName()); contentValues.put(MediaStore.Images.Media.MIME_TYPE, "image/jpeg"); contentValues.put(MediaStore.Images.Media.DATA, mCurrentPhotoPath); long time = date.getTime() / 1000; contentValues.put(MediaStore.MediaColumns.DATE_ADDED, time); contentValues.put(MediaStore.MediaColumns.DATE_MODIFIED, time); if (photoFile.exists()) { contentValues.put(MediaStore.Images.Media.SIZE, 图片大小);// } mCurrentPhotoUri = mContext.get().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, contentValues); } try { MediaStore.Images.Media.insertImage(mContext.get().getContentResolver(), mCurrentPhotoPath, "", ""); } catch (FileNotFoundException e) { e.printStackTrace(); }
public static String insertImageToGallery(Context context, File file) { //把文件插入到系统图库 String s=""; try { s = MediaStore.Images.Media.insertImage(context.getContentResolver(), file.getAbsolutePath(), file.getName(), null); Log.d("FileUtils", s); } catch (FileNotFoundException e) { e.printStackTrace(); } // 最后通知图库更新 context.sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.parse("file://" + file.getAbsolutePath()))); return s;