diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/utils/MediaStoreCompat.java b/matisse/src/main/java/com/zhihu/matisse/internal/utils/MediaStoreCompat.java index 592c77ea6..c629c6655 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/utils/MediaStoreCompat.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/utils/MediaStoreCompat.java @@ -15,7 +15,9 @@ */ package com.zhihu.matisse.internal.utils; +import android.annotation.TargetApi; import android.app.Activity; +import android.content.ContentValues; import android.content.Context; import android.content.Intent; import android.content.pm.PackageManager; @@ -74,17 +76,25 @@ public void setCaptureStrategy(CaptureStrategy strategy) { public void dispatchCaptureIntent(Context context, int requestCode) { Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); if (captureIntent.resolveActivity(context.getPackageManager()) != null) { - File photoFile = null; - try { - photoFile = createImageFile(); - } catch (IOException e) { - e.printStackTrace(); - } + if (mCaptureStrategy.isPublic && Platform.hasAndroidQ()) { + mCurrentPhotoUri = createImageUri(); + if (mCurrentPhotoUri != null) + mCurrentPhotoPath = PathUtils.getPath(mContext.get(), mCurrentPhotoUri); + } else { + File photoFile = null; + try { + photoFile = createImageFile(); + } catch (IOException e) { + e.printStackTrace(); + } - if (photoFile != null) { - mCurrentPhotoPath = photoFile.getAbsolutePath(); - mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(), - mCaptureStrategy.authority, photoFile); + if (photoFile != null) { + mCurrentPhotoPath = photoFile.getAbsolutePath(); + mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(), + mCaptureStrategy.authority, photoFile); + } + } + if (mCurrentPhotoUri != null) { captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCurrentPhotoUri); captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { @@ -135,6 +145,21 @@ private File createImageFile() throws IOException { return tempFile; } + @TargetApi(Build.VERSION_CODES.Q) + private Uri createImageUri() { + String relativePath = Environment.DIRECTORY_PICTURES; + if (mCaptureStrategy.directory != null) { + relativePath += File.separator + mCaptureStrategy.directory; + } + String timeStamp = + new SimpleDateFormat("yyyyMMdd_HHmmss", Locale.getDefault()).format(new Date()); + String imageFileName = String.format("JPEG_%s.jpg", timeStamp); + ContentValues values = new ContentValues(); + values.put(MediaStore.Images.Media.DISPLAY_NAME, imageFileName); + values.put(MediaStore.MediaColumns.RELATIVE_PATH, relativePath); + return mContext.get().getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); + } + public Uri getCurrentPhotoUri() { return mCurrentPhotoUri; } diff --git a/matisse/src/main/java/com/zhihu/matisse/internal/utils/Platform.java b/matisse/src/main/java/com/zhihu/matisse/internal/utils/Platform.java index df41989ae..89c026f94 100644 --- a/matisse/src/main/java/com/zhihu/matisse/internal/utils/Platform.java +++ b/matisse/src/main/java/com/zhihu/matisse/internal/utils/Platform.java @@ -13,4 +13,8 @@ public static boolean hasICS() { public static boolean hasKitKat() { return Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; } + + public static boolean hasAndroidQ() { + return Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q; + } }