Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Q 上使用MediaStore 存储照片 #708

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
package com.zhihu.matisse.internal.utils;

import android.app.Activity;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
Expand All @@ -24,6 +26,7 @@
import android.os.Build;
import android.os.Environment;
import android.provider.MediaStore;

import androidx.fragment.app.Fragment;
import androidx.core.content.FileProvider;
import androidx.core.os.EnvironmentCompat;
Expand All @@ -42,9 +45,9 @@ public class MediaStoreCompat {

private final WeakReference<Activity> mContext;
private final WeakReference<Fragment> mFragment;
private CaptureStrategy mCaptureStrategy;
private Uri mCurrentPhotoUri;
private String mCurrentPhotoPath;
private CaptureStrategy mCaptureStrategy;
private Uri mCurrentPhotoUri;
private String mCurrentPhotoPath;

public MediaStoreCompat(Activity activity) {
mContext = new WeakReference<>(activity);
Expand Down Expand Up @@ -85,8 +88,25 @@ public void dispatchCaptureIntent(Context context, int requestCode) {
mCurrentPhotoPath = photoFile.getAbsolutePath();
mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(),
mCaptureStrategy.authority, photoFile);

if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.DISPLAY_NAME, photoFile.getName());
values.put(MediaStore.Images.Media.MIME_TYPE, "image/*");

if (mCaptureStrategy.directory != null) {
values.put(MediaStore.Images.Media.RELATIVE_PATH, Environment.DIRECTORY_PICTURES + "/" + mCaptureStrategy.directory);
}

ContentResolver contentResolver = context.getContentResolver();
mCurrentPhotoUri = contentResolver.insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);

} else {
mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(),
mCaptureStrategy.authority, photoFile);
captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
}
captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCurrentPhotoUri);
captureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
List<ResolveInfo> resInfoList = context.getPackageManager()
.queryIntentActivities(captureIntent, PackageManager.MATCH_DEFAULT_ONLY);
Expand Down