6

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

此问题不会发生在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);
                }
   }

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

PS:如果有人有工作示例,即使在方向更改上也确实有效,我很乐意接受它作为答案。

4

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

于 2013-07-31T09:13:43.810 回答
0

在输入之前检查 imageCaptureUri != null 的intent.setData(imageCaptureUri);doCrop()

编辑

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

intent.setData(Uri.fromFile(new File(path)));
于 2013-07-30T06:29:48.440 回答