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

修改v0.5.3-beta3 Android10不能拍照的问题,测试通过,请检阅 #773

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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
package com.zhihu.matisse.internal.utils;

import android.app.Activity;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
Expand Down Expand Up @@ -74,34 +75,50 @@ 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 (photoFile != null) {
mCurrentPhotoPath = photoFile.getAbsolutePath();
mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(),
mCaptureStrategy.authority, photoFile);
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);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, mCurrentPhotoUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
if (Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
//Android 10创建uri的方式和使用uri加载图片的方式在10以下的手机是同样适用的
mCurrentPhotoUri = createImageUri(context);
} else {//<Q
File photoFile = null;
try {
photoFile = createImageFile();
} catch (IOException e) {
e.printStackTrace();
}
if (mFragment != null) {
mFragment.get().startActivityForResult(captureIntent, requestCode);
} else {
mContext.get().startActivityForResult(captureIntent, requestCode);
if (photoFile != null) {
mCurrentPhotoPath = photoFile.getAbsolutePath();
mCurrentPhotoUri = FileProvider.getUriForFile(mContext.get(),
mCaptureStrategy.authority, photoFile);

}
}
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);
for (ResolveInfo resolveInfo : resInfoList) {
String packageName = resolveInfo.activityInfo.packageName;
context.grantUriPermission(packageName, mCurrentPhotoUri,
Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION);
}
}
if (mFragment != null) {
mFragment.get().startActivityForResult(captureIntent, requestCode);
} else {
mContext.get().startActivityForResult(captureIntent, requestCode);
}
}
}

/**create photo uri in Android 10 */
private Uri createImageUri(Context context) {
String status = Environment.getExternalStorageState();
if (status.equals(Environment.MEDIA_MOUNTED)) {
return context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, new ContentValues());
} else {
return context.getContentResolver().insert(MediaStore.Images.Media.INTERNAL_CONTENT_URI, new ContentValues());
}
}

Expand Down