MD5 校验值:d555a6389a0a865314e2706996812829
ImagePickerDelegate.java 文件包含反编译后的源代码,请注意,该内容仅供学习和参考使用,不得用于非法用途。
package ko.flutter.plugins.imagepicker; import android.app.Activity; import android.content.ActivityNotFoundException; import android.content.Intent; import android.content.pm.ResolveInfo; import android.media.MediaScannerConnection; import android.net.Uri; import android.os.Build; import androidx.core.app.ActivityCompat; import androidx.core.content.FileProvider; import java.io.File; import java.io.IOException; import java.util.ArrayList; import java.util.Iterator; import java.util.Map; import java.util.UUID; import ko.flutter.plugin.common.MethodCall; import ko.flutter.plugin.common.MethodChannel; import ko.flutter.plugin.common.PluginRegistry; public class ImagePickerDelegate implements PluginRegistry.ActivityResultListener, PluginRegistry.RequestPermissionsResultListener { static final int REQUEST_CAMERA_IMAGE_PERMISSION = 2345; static final int REQUEST_CAMERA_VIDEO_PERMISSION = 2355; static final int REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY = 2342; static final int REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY = 2346; static final int REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY = 2352; static final int REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA = 2343; static final int REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA = 2353; private final Activity activity; private final ImagePickerCache cache; private CameraDevice cameraDevice; final File externalFilesDirectory; final String fileProviderName; private final FileUriResolver fileUriResolver; private final FileUtils fileUtils; private final ImageResizer imageResizer; private MethodCall methodCall; private Uri pendingCameraMediaUri; private MethodChannel.Result pendingResult; private final PermissionManager permissionManager; public interface FileUriResolver { void getFullImagePath(Uri uri, OnPathReadyListener onPathReadyListener); Uri resolveFileProviderUriForFile(String str, File file); } public interface OnPathReadyListener { void onPathReady(String str); } public interface PermissionManager { void askForPermission(String str, int i); boolean isPermissionGranted(String str); boolean needRequestCameraPermission(); } public ImagePickerDelegate(final Activity activity, File file, ImageResizer imageResizer, ImagePickerCache imagePickerCache) { this(activity, file, imageResizer, null, null, imagePickerCache, new PermissionManager() { @Override public boolean isPermissionGranted(String str) { return ActivityCompat.checkSelfPermission(activity, str) == 0; } @Override public void askForPermission(String str, int i) { ActivityCompat.requestPermissions(activity, new String[]{str}, i); } @Override public boolean needRequestCameraPermission() { return ImagePickerUtils.needRequestCameraPermission(activity); } }, new FileUriResolver() { @Override public Uri resolveFileProviderUriForFile(String str, File file2) { return FileProvider.getUriForFile(activity, str, file2); } @Override public void getFullImagePath(Uri uri, final OnPathReadyListener onPathReadyListener) { Activity activity2 = activity; String[] strArr = new String[1]; strArr[0] = uri != null ? uri.getPath() : ""; MediaScannerConnection.scanFile(activity2, strArr, null, new MediaScannerConnection.OnScanCompletedListener() { @Override public void onScanCompleted(String str, Uri uri2) { onPathReadyListener.onPathReady(str); } }); } }, new FileUtils()); } ImagePickerDelegate(Activity activity, File file, ImageResizer imageResizer, MethodChannel.Result result, MethodCall methodCall, ImagePickerCache imagePickerCache, PermissionManager permissionManager, FileUriResolver fileUriResolver, FileUtils fileUtils) { this.activity = activity; this.externalFilesDirectory = file; this.imageResizer = imageResizer; this.fileProviderName = activity.getPackageName() + ".flutter.image_provider"; this.pendingResult = result; this.methodCall = methodCall; this.permissionManager = permissionManager; this.fileUriResolver = fileUriResolver; this.fileUtils = fileUtils; this.cache = imagePickerCache; } public void setCameraDevice(CameraDevice cameraDevice) { this.cameraDevice = cameraDevice; } CameraDevice getCameraDevice() { return this.cameraDevice; } public void saveStateBeforeResult() { MethodCall methodCall = this.methodCall; if (methodCall == null) { return; } this.cache.saveTypeWithMethodCallName(methodCall.method); this.cache.saveDimensionWithMethodCall(this.methodCall); Uri uri = this.pendingCameraMediaUri; if (uri != null) { this.cache.savePendingCameraMediaUriPath(uri); } } public void retrieveLostImage(MethodChannel.Result result) { Map<String, Object> cacheMap = this.cache.getCacheMap(); ArrayList arrayList = (ArrayList) cacheMap.get("pathList"); ArrayList arrayList2 = new ArrayList(); if (arrayList != null) { Iterator it = arrayList.iterator(); while (it.hasNext()) { arrayList2.add(this.imageResizer.resizeImageIfNeeded((String) it.next(), (Double) cacheMap.get("maxWidth"), (Double) cacheMap.get("maxHeight"), Integer.valueOf(cacheMap.get("imageQuality") == null ? 100 : ((Integer) cacheMap.get("imageQuality")).intValue()))); } cacheMap.put("pathList", arrayList2); cacheMap.put("path", arrayList2.get(arrayList2.size() - 1)); } if (cacheMap.isEmpty()) { result.success(null); } else { result.success(cacheMap); } this.cache.clear(); } public void chooseVideoFromGallery(MethodCall methodCall, MethodChannel.Result result) { if (!setPendingMethodCallAndResult(methodCall, result)) { finishWithAlreadyActiveError(result); } else { launchPickVideoFromGalleryIntent(); } } private void launchPickVideoFromGalleryIntent() { Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("video/*"); this.activity.startActivityForResult(intent, REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY); } public void takeVideoWithCamera(MethodCall methodCall, MethodChannel.Result result) { if (!setPendingMethodCallAndResult(methodCall, result)) { finishWithAlreadyActiveError(result); } else if (needRequestCameraPermission() && !this.permissionManager.isPermissionGranted("android.permission.CAMERA")) { this.permissionManager.askForPermission("android.permission.CAMERA", REQUEST_CAMERA_VIDEO_PERMISSION); } else { launchTakeVideoWithCameraIntent(); } } private void launchTakeVideoWithCameraIntent() { Intent intent = new Intent("android.media.action.VIDEO_CAPTURE"); MethodCall methodCall = this.methodCall; if (methodCall != null && methodCall.argument("maxDuration") != null) { intent.putExtra("android.intent.extra.durationLimit", ((Integer) this.methodCall.argument("maxDuration")).intValue()); } if (this.cameraDevice == CameraDevice.FRONT) { useFrontCamera(intent); } File createTemporaryWritableVideoFile = createTemporaryWritableVideoFile(); this.pendingCameraMediaUri = Uri.parse("file:" + createTemporaryWritableVideoFile.getAbsolutePath()); Uri resolveFileProviderUriForFile = this.fileUriResolver.resolveFileProviderUriForFile(this.fileProviderName, createTemporaryWritableVideoFile); intent.putExtra("output", resolveFileProviderUriForFile); grantUriPermissions(intent, resolveFileProviderUriForFile); try { try { this.activity.startActivityForResult(intent, REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA); } catch (ActivityNotFoundException unused) { createTemporaryWritableVideoFile.delete(); finishWithError("no_available_camera", "No cameras available for taking pictures."); } } catch (SecurityException e) { e.printStackTrace(); finishWithError("no_available_camera", "No cameras available for taking pictures."); } } public void chooseImageFromGallery(MethodCall methodCall, MethodChannel.Result result) { if (!setPendingMethodCallAndResult(methodCall, result)) { finishWithAlreadyActiveError(result); } else { launchPickImageFromGalleryIntent(); } } public void chooseMultiImageFromGallery(MethodCall methodCall, MethodChannel.Result result) { if (!setPendingMethodCallAndResult(methodCall, result)) { finishWithAlreadyActiveError(result); } else { launchMultiPickImageFromGalleryIntent(); } } private void launchPickImageFromGalleryIntent() { Intent intent = new Intent("android.intent.action.GET_CONTENT"); intent.setType("image/*"); this.activity.startActivityForResult(intent, REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY); } private void launchMultiPickImageFromGalleryIntent() { Intent intent = new Intent("android.intent.action.GET_CONTENT"); if (Build.VERSION.SDK_INT >= 18) { intent.putExtra("android.intent.extra.ALLOW_MULTIPLE", true); } intent.setType("image/*"); this.activity.startActivityForResult(intent, REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY); } public void takeImageWithCamera(MethodCall methodCall, MethodChannel.Result result) { if (!setPendingMethodCallAndResult(methodCall, result)) { finishWithAlreadyActiveError(result); } else if (needRequestCameraPermission() && !this.permissionManager.isPermissionGranted("android.permission.CAMERA")) { this.permissionManager.askForPermission("android.permission.CAMERA", REQUEST_CAMERA_IMAGE_PERMISSION); } else { launchTakeImageWithCameraIntent(); } } private boolean needRequestCameraPermission() { PermissionManager permissionManager = this.permissionManager; if (permissionManager == null) { return false; } return permissionManager.needRequestCameraPermission(); } private void launchTakeImageWithCameraIntent() { Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); if (this.cameraDevice == CameraDevice.FRONT) { useFrontCamera(intent); } File createTemporaryWritableImageFile = createTemporaryWritableImageFile(); this.pendingCameraMediaUri = Uri.parse("file:" + createTemporaryWritableImageFile.getAbsolutePath()); Uri resolveFileProviderUriForFile = this.fileUriResolver.resolveFileProviderUriForFile(this.fileProviderName, createTemporaryWritableImageFile); intent.putExtra("output", resolveFileProviderUriForFile); grantUriPermissions(intent, resolveFileProviderUriForFile); try { try { this.activity.startActivityForResult(intent, REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA); } catch (ActivityNotFoundException unused) { createTemporaryWritableImageFile.delete(); finishWithError("no_available_camera", "No cameras available for taking pictures."); } } catch (SecurityException e) { e.printStackTrace(); finishWithError("no_available_camera", "No cameras available for taking pictures."); } } private File createTemporaryWritableImageFile() { return createTemporaryWritableFile(".jpg"); } private File createTemporaryWritableVideoFile() { return createTemporaryWritableFile(".mp4"); } private File createTemporaryWritableFile(String str) { String uuid = UUID.randomUUID().toString(); try { this.externalFilesDirectory.mkdirs(); return File.createTempFile(uuid, str, this.externalFilesDirectory); } catch (IOException e) { throw new RuntimeException(e); } } private void grantUriPermissions(Intent intent, Uri uri) { Iterator<ResolveInfo> it = this.activity.getPackageManager().queryIntentActivities(intent, 65536).iterator(); while (it.hasNext()) { this.activity.grantUriPermission(it.next().activityInfo.packageName, uri, 3); } } @Override public boolean onRequestPermissionsResult(int i, String[] strArr, int[] iArr) { boolean z = iArr.length > 0 && iArr[0] == 0; if (i != REQUEST_CAMERA_IMAGE_PERMISSION) { if (i != REQUEST_CAMERA_VIDEO_PERMISSION) { return false; } if (z) { launchTakeVideoWithCameraIntent(); } } else if (z) { launchTakeImageWithCameraIntent(); } if (!z && (i == REQUEST_CAMERA_IMAGE_PERMISSION || i == REQUEST_CAMERA_VIDEO_PERMISSION)) { finishWithError("camera_access_denied", "The user did not allow camera access."); } return true; } @Override public boolean onActivityResult(int i, int i2, Intent intent) { if (i == REQUEST_CODE_CHOOSE_IMAGE_FROM_GALLERY) { handleChooseImageResult(i2, intent); return true; } if (i == REQUEST_CODE_TAKE_IMAGE_WITH_CAMERA) { handleCaptureImageResult(i2); return true; } if (i == REQUEST_CODE_CHOOSE_MULTI_IMAGE_FROM_GALLERY) { handleChooseMultiImageResult(i2, intent); return true; } if (i == REQUEST_CODE_CHOOSE_VIDEO_FROM_GALLERY) { handleChooseVideoResult(i2, intent); return true; } if (i != REQUEST_CODE_TAKE_VIDEO_WITH_CAMERA) { return false; } handleCaptureVideoResult(i2); return true; } private void handleChooseImageResult(int i, Intent intent) { if (i == -1 && intent != null) { handleImageResult(this.fileUtils.getPathFromUri(this.activity, intent.getData()), false); } else { finishWithSuccess(null); } } private void handleChooseMultiImageResult(int i, Intent intent) { if (i == -1 && intent != null) { ArrayList<String> arrayList = new ArrayList<>(); if (intent.getClipData() != null) { for (int i2 = 0; i2 < intent.getClipData().getItemCount(); i2++) { arrayList.add(this.fileUtils.getPathFromUri(this.activity, intent.getClipData().getItemAt(i2).getUri())); } } else { arrayList.add(this.fileUtils.getPathFromUri(this.activity, intent.getData())); } handleMultiImageResult(arrayList, false); return; } finishWithSuccess(null); } private void handleChooseVideoResult(int i, Intent intent) { if (i == -1 && intent != null) { handleVideoResult(this.fileUtils.getPathFromUri(this.activity, intent.getData())); } else { finishWithSuccess(null); } } private void handleCaptureImageResult(int i) { if (i == -1) { FileUriResolver fileUriResolver = this.fileUriResolver; Uri uri = this.pendingCameraMediaUri; if (uri == null) { uri = Uri.parse(this.cache.retrievePendingCameraMediaUriPath()); } fileUriResolver.getFullImagePath(uri, new OnPathReadyListener() { @Override public void onPathReady(String str) { ImagePickerDelegate.this.handleImageResult(str, true); } }); return; } finishWithSuccess(null); } private void handleCaptureVideoResult(int i) { if (i == -1) { FileUriResolver fileUriResolver = this.fileUriResolver; Uri uri = this.pendingCameraMediaUri; if (uri == null) { uri = Uri.parse(this.cache.retrievePendingCameraMediaUriPath()); } fileUriResolver.getFullImagePath(uri, new OnPathReadyListener() { @Override public void onPathReady(String str) { ImagePickerDelegate.this.handleVideoResult(str); } }); return; } finishWithSuccess(null); } private void handleMultiImageResult(ArrayList<String> arrayList, boolean z) { if (this.methodCall != null) { ArrayList<String> arrayList2 = new ArrayList<>(); for (int i = 0; i < arrayList.size(); i++) { String resizedImagePath = getResizedImagePath(arrayList.get(i)); if (resizedImagePath != null && !resizedImagePath.equals(arrayList.get(i)) && z) { new File(arrayList.get(i)).delete(); } arrayList2.add(i, resizedImagePath); } finishWithListSuccess(arrayList2); return; } finishWithListSuccess(arrayList); } public void handleImageResult(String str, boolean z) { if (this.methodCall != null) { String resizedImagePath = getResizedImagePath(str); if (resizedImagePath != null && !resizedImagePath.equals(str) && z) { new File(str).delete(); } finishWithSuccess(resizedImagePath); return; } finishWithSuccess(str); } private String getResizedImagePath(String str) { return this.imageResizer.resizeImageIfNeeded(str, (Double) this.methodCall.argument("maxWidth"), (Double) this.methodCall.argument("maxHeight"), (Integer) this.methodCall.argument("imageQuality")); } public void handleVideoResult(String str) { finishWithSuccess(str); } private boolean setPendingMethodCallAndResult(MethodCall methodCall, MethodChannel.Result result) { if (this.pendingResult != null) { return false; } this.methodCall = methodCall; this.pendingResult = result; this.cache.clear(); return true; } private void finishWithSuccess(String str) { MethodChannel.Result result = this.pendingResult; if (result == null) { ArrayList<String> arrayList = new ArrayList<>(); arrayList.add(str); this.cache.saveResult(arrayList, null, null); } else { result.success(str); clearMethodCallAndResult(); } } private void finishWithListSuccess(ArrayList<String> arrayList) { MethodChannel.Result result = this.pendingResult; if (result == null) { this.cache.saveResult(arrayList, null, null); } else { result.success(arrayList); clearMethodCallAndResult(); } } private void finishWithAlreadyActiveError(MethodChannel.Result result) { result.error("already_active", "Image picker is already active", null); } private void finishWithError(String str, String str2) { MethodChannel.Result result = this.pendingResult; if (result == null) { this.cache.saveResult(null, str, str2); } else { result.error(str, str2, null); clearMethodCallAndResult(); } } private void clearMethodCallAndResult() { this.methodCall = null; this.pendingResult = null; } private void useFrontCamera(Intent intent) { if (Build.VERSION.SDK_INT >= 22) { intent.putExtra("android.intent.extras.CAMERA_FACING", 0); if (Build.VERSION.SDK_INT >= 26) { intent.putExtra("android.intent.extra.USE_FRONT_CAMERA", true); return; } return; } intent.putExtra("android.intent.extras.CAMERA_FACING", 1); } }