Android judges whether com.android.camera.action.CROP exists

Copyright notice: This is the original article of xing'star. Please indicate the source for reprint!

This paper is synchronized from http://javaexception.com/archives/225

Recently, a user reported an error on the line. He crashed about 10 times in a row and looked at the information of the next stack. It was found that the prompt com.android.camera.action.CROP was not found, and the error ActivityNotFound was reported. According to the experience, it can be concluded that the user's device must have removed the application supporting Crop, so the user can directly jump to the meeting crash by doing the Intent privacy jump. After thinking about it, the solution is to do the detection before the jump, or the global detection.

Global detection method:

public boolean isAvailable(Context context, Intent intent) { 
  PackageManager packageManager = context.getPackageManager();
List list = packageManager.queryIntentActivities(intent, PackageManager.MATCH_DEFAULT_ONLY);
  return list.size() > 0;
}

After testing, it doesn't work at com.android.camera.action.CROP, so we have to give up. But this is supported by some Intent, and it's also a way

The second is detection before jump:

private void crop(String imagePath) {
    File file = new File(FileUtils.createRootPath(this) + "/" + System.currentTimeMillis() + ".jpg");
 
    cropImagePath = file.getAbsolutePath();
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", config.aspectX);
    intent.putExtra("aspectY", config.aspectY);
    intent.putExtra("outputX", config.outputX);
    intent.putExtra("outputY", config.outputY);
    intent.putExtra("scale", true);
    intent.putExtra("scaleUpIfNeeded", true);
    intent.putExtra("return-data", false);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true);
    startActivityForResult(intent, IMAGE_CROP_CODE);
}

 

My modified test code is as follows:

private boolean canCrop(String imagePath) {
    File file = new File(FileUtils.createRootPath(this) + "/" + System.currentTimeMillis() + ".jpg");
    Intent intent = new Intent("com.android.camera.action.CROP");
    intent.setDataAndType(getImageContentUri(new File(imagePath)), "image/*");
    intent.putExtra("crop", "true");
    intent.putExtra("aspectX", config.aspectX);
    intent.putExtra("aspectY", config.aspectY);
    intent.putExtra("outputX", config.outputX);
    intent.putExtra("outputY", config.outputY);
    intent.putExtra("scale", true);
    intent.putExtra("scaleUpIfNeeded", true);
    intent.putExtra("return-data", false);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file));
    intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());
    intent.putExtra("noFaceDetection", true);
    if (intent.resolveActivity(getPackageManager()) != null) {
        return true;
    } else {
        // App not installed
        return false;
    }
}

The key code is intent. Resolveactivity (getpackagemanager())! = null

Recommend an article worth reading:

https://juejin.im/entry/59a93530f265da24823642b3

Tags: Android

Posted on Wed, 06 Nov 2019 09:30:58 -0500 by justoneguy