Crop image on different devices

烂漫一生 提交于 2019-12-13 02:56:15

问题


I want to take a picture with the camera and crop it. This works great (with the second code) on newer devices with this code I found on the community wiki:

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.android.gallery", "com.android.camera.CropImage");

On some Android versions, including the newest, com.android.gallery doesn't exist anymore. You need to use this then:

Intent intent = new Intent("com.android.camera.action.CROP");
intent.setClassName("com.google.android.gallery3d", "com.android.gallery3d.app.CropImage");

Of course I want to support older devices too. What is meant with "some Android versions"? Can someone give me an API level? Or are there any final constances in the Android source which I can use to select the correct Strings for the intent?


回答1:


Some devices don't support cropping, meaning that their gallery application does not have it built it. The best solution is building a cropping mechanism into your app. Here is a good open source cropper:

https://github.com/edmodo/cropper




回答2:


I found a better code for this problem. This here will search for apps which are able to crop images and start the first that is found. Hope that help someone.

Intent cropApps = new Intent("com.android.camera.action.CROP");
cropApps.setType("image/*");

List<ResolveInfo> list = context.getPackageManager().queryIntentActivities(cropApps, 0);
int size = list.size();

if (size == 0) 
{           
    Toast.makeText(context, "Can not find image crop app", Toast.LENGTH_SHORT).show();      
    return null;
} 
else 
{
    ResolveInfo res = list.get(0);

    Intent intent = new Intent();
    intent.setClassName(res.activityInfo.packageName, res.activityInfo.name);

    intent.setData(imageCaptureUri);
    intent.putExtra("outputX", 96);
    intent.putExtra("outputY", 96);
    intent.putExtra("aspectX", 1);
    intent.putExtra("aspectY", 1);
    intent.putExtra("scale", true);
    intent.putExtra("return-data", true);

    startActivityForResult(intent, CROP_FROM_CAMERA);
}


来源:https://stackoverflow.com/questions/21879630/crop-image-on-different-devices

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!