从相机拍摄时的图片裁剪问题

6
以下是我的相关代码片段,其中在从相机拍摄照片后会重定向到设备的裁剪应用程序。我通常遵循这个 示例。但在从相机拍摄照片并将其在裁剪应用程序中可用之间的阶段/时刻,它会持续显示加载,并使我的应用程序非常不响应。如果这种情况发生至少一次,每次打开应用程序时都会显示加载。 一个场景中不会出现此问题-> 当我不移动我的设备时(即仅当没有屏幕方向发生时)。请问有人可以建议我如何避免这个问题吗?
public void shotFromCamera(View view)
{
    Intent intent    = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
      intent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageCaptureUri);

                try {
                    intent.putExtra("return-data", true);

                    startActivityForResult(intent, PICK_FROM_CAMERA);
                } catch (ActivityNotFoundException e) {
                    e.printStackTrace();
                }
}

protected void onActivityResult(int requestCode, int resultCode,Intent data)
    {
        super.onActivityResult(requestCode, resultCode, data);

        if(resultCode==RESULT_OK && requestCode == PICK_FROM_CAMERA){

            doCrop();
                 } else if(resultCode == RESULT_OK && requestCode == CROP_FROM_CAMERA)
        {
            Bundle extras = data.getExtras();

            if (extras != null) {               
                Bitmap photoBitmap = extras.getParcelable("data");

                imageView.setImageBitmap(photoBitmap);
                if(mImageCaptureUri != null)
                {
                File f = new File(mImageCaptureUri.getPath());
                if (f.exists()) f.delete();
                }

                mImageCaptureUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory()+ "/MyApp",
                           "avatar" + String.valueOf(System.currentTimeMillis()) + ".jpg"));
                File file = new File(mImageCaptureUri.getPath()); 

                    selectedImagePath = mImageCaptureUri.getPath();
                    Log.d("pic1 ","pic to be created: "+selectedImagePath);
                    OutputStream fOut = null;

                    try {
                        fOut = new FileOutputStream(file);
                        photoBitmap.compress(Bitmap.CompressFormat.JPEG, 85, fOut);
                        fOut.flush();
                        fOut.close();
                    } catch (FileNotFoundException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                    } catch(IOException e)
                    {
                        e.printStackTrace();
                    }       

            }


        }
}

    private void doCrop() {
            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 );

            int size = list.size();

            if (size == 0) {            
                Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();

                return;
            } else {
                intent.setData(imageCaptureUri);

                intent.putExtra("outputX", 200);
                intent.putExtra("outputY", 200);
                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);
                }
   }

为了澄清我的问题,我按照示例中的源代码运行,出现了相同的问题。我还运行了另外几个重定向到裁剪应用程序的示例。在上述阶段改变方向时,相同的问题也会发生。
附言:如果有人有可以在方向更改时确保正常工作的示例,我很乐意接受它作为答案。
2个回答

1
你可以按照以下步骤进行操作-
  1. 从指定的URI获取位图,即使用完整大小的图像

    photo = android.provider.MediaStore.Images.Media.getBitmap(cr, Uri.fromFile(imageCaptureUri));

  2. 在压缩图像时使用Bitmap.CompressFormat.PNG

  3. 使用以下代码创建缩放后的位图

    photo= Bitmap.createScaledBitmap(background, YourWidth, YourHeight, true);

  4. 这是您的缩放(裁剪)图像。


1
谢谢你的回答 :) 我本来可以这样做的。但我想让用户自己裁剪。 - Kanth

0

doCrop()函数中的intent.setData(imageCaptureUri);这一行之前,检查imageCaptureUri的值是否为null

编辑

首先将相机结果保存在文件中,并将该文件传递给裁剪应用程序

intent.setData(Uri.fromFile(new File(path)));

是的,它绕过了问题,但并没有解决我的问题。您有其他建议和任何在这种情况下有效的方案吗? - Kanth
请查看以下链接以了解如何使用相机拍照:http://developer.android.com/training/camera/photobasics.html - Kirit Vaghela

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接