代码之家  ›  专栏  ›  技术社区  ›  Cilenco

在不同设备上裁剪图像

  •  0
  • Cilenco  · 技术社区  · 11 年前

    我想用相机拍一张照片并裁剪它。这在更新的设备上非常有用(使用第二个代码),我在社区wiki上找到了以下代码:

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

    在一些Android版本上,包括最新的com.Android.gallery已经不存在了。然后您需要使用此选项:

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

    当然,我也想支持旧设备。“某些Android版本”是什么意思?有人能给我一个API级别吗?或者Android源代码中是否有任何最终的常量,我可以使用这些常量来选择正确的字符串?

    2 回复  |  直到 11 年前
        1
  •  2
  •   KennyC    11 年前

    有些设备不支持裁剪,这意味着他们的图库应用程序没有内置裁剪。最好的解决方案是在应用程序中构建裁剪机制。这里有一个很好的开源cropper:

    https://github.com/edmodo/cropper

        2
  •  1
  •   Cilenco    11 年前

    我为这个问题找到了更好的代码。这里将搜索能够裁剪图像的应用程序,并启动找到的第一个应用程序。希望能帮助到某人。

    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);
    }