【问题标题】:Cropping image issue when taken from camera从相机拍摄时裁剪图像问题
【发布时间】:2013-07-30 03:53:18
【问题描述】:

下面是我相关的code snippet,在从相机拍摄照片后,它会重定向到设备的cropping app。我通常关注这个example。但是在从相机拍摄的照片和在裁剪应用程序中提供照片之间的阶段/时刻,它显示不断加载并使我的应用程序非常无响应。如果这种情况至少发生一次,那么每次我打开应用程序时,它都会显示正在加载。

one scenario 不会出现此问题--> 当我不移动我的设备时(即,仅当没有发生屏幕方向时)。有人可以建议我避免这个问题吗?

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

为了澄清我的问题,我按原样运行了该示例中的源代码,同样的问题发生了。我已经运行了其他几个重定向到裁剪应用程序的示例。在上述阶段更改方向时也会发生同样的问题。

P.S :如果有人有工作示例,即使在方向变化时也确实有效,我很乐意接受它作为答案。

【问题讨论】:

    标签: android image crop


    【解决方案1】:

    doCrop() 中的intent.setData(imageCaptureUri); 行之前检查imageCaptureUri != null 的值

    编辑

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

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

    【讨论】:

    【解决方案2】:

    你可以这样-

    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. 这是您的缩放(裁剪)图像。

    【讨论】:

    • 感谢您的回答 :) 我本可以这样做。但我想让用户自己裁剪。
    猜你喜欢
    • 1970-01-01
    • 2010-12-21
    • 1970-01-01
    • 1970-01-01
    • 2020-11-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-10
    相关资源
    最近更新 更多