如何使用 OS Lollipop 在 Java 中为 Android 裁剪图像

Posted

技术标签:

【中文标题】如何使用 OS Lollipop 在 Java 中为 Android 裁剪图像【英文标题】:How to crop image in java for android with OS Lollipop 【发布时间】:2015-03-18 02:09:25 【问题描述】:

我有以下用于裁剪图像的代码。它在 android 版本 4 或 OS Kitkat 中运行良好,但在 Android 版本 5 或 OS Lollipop 上无法运行。

我已经找遍了整个世界却找不到答案……

这是我的代码:

在 OS Kitkat 中:此列表变量返回一个值。但是, 在 OS Lollipop 中:此列表变量返回一个空数组列表。

    final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();

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

    List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
    PackageManager test = getPackageManager();

    int size = list.size();

    if (size == 0)             

        Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

        return;
     else 
        intent.setData(mCapturedImageURI);

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

        if (size == 1) 
            Intent i = new Intent(intent);
            ResolveInfo res = list.get(0);

            i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

            startActivityForResult(i, CROP_FROM_CAMERA);
         else 
            for (ResolveInfo res : list) 
                final CropOption co = new CropOption();

                co.title    = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
                co.icon     = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
                co.appIntent= new Intent(intent);

                co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));

                cropOptions.add(co);
            

            CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);

            AlertDialog.Builder builder = new AlertDialog.Builder(this);
            builder.setTitle("Choose Crop App");
            builder.setAdapter( adapter, new DialogInterface.OnClickListener() 
                public void onClick( DialogInterface dialog, int item ) 
                    startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
                
            );

            builder.setOnCancelListener( new DialogInterface.OnCancelListener() 
                @Override
                public void onCancel( DialogInterface dialog ) 

                    if (mCapturedImageURI != null ) 
                        getContentResolver().delete(mCapturedImageURI, null, null );
                        mCapturedImageURI = null;
                    
                
             );

            AlertDialog alert = builder.create();

            alert.show();
        
    

【问题讨论】:

请忽略PackageManager test = getPackageManager();这不包括在内..抱歉,我在发布时忘记删除它.. 您能否更详细地解释一下问题所在?你遇到了什么错误? 它不会在 logcat 中返回任何错误...它只是在列表变量 (List) 中分配一个空数组列表。所以 list.size 变为 0 然后它执行 Toast.makeText方法然后返回到活动......我的应用程序没有崩溃,但它也没有裁剪...... 如果有人有关于在 Lollipop 上裁剪的解决方案,请回复 here。 【参考方案1】:

我已经授予了 android.permission.MANAGE_DOCUMENTS 权限。但面临同样的问题。经过大量搜索,我找到了解决方案。

暂时这里有一个解决方法:

Intent mIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
startActivityForResult(mIntent, CHOOSE_IMAGE);

这会强制打开旧的图片库而不是新的 Kitkat 文档视图。

现在,您可以通过在 onActivityResult 中调用以下命令来获取 Uri:

Uri selectedImageURI = data.getData();

希望这有助于解决您的问题。

【讨论】:

以上是关于如何使用 OS Lollipop 在 Java 中为 Android 裁剪图像的主要内容,如果未能解决你的问题,请参考以下文章