使用Android使用Capture Image裁剪图像问题

时间:2014-09-27 12:11:31

标签: android imageview android-camera-intent

我从不同按钮使用相机功能Capture图像2次。捕获图像后,它转到Crop选项,用户可以裁剪图像。它也很好。

但是,现在的问题是当用户捕获第二张图像时,App会重定向到Crop图像并仅显示第一张图像而不是第二张图像。在将其设置为ImageView后,我也在删除图像。不知道出了什么问题?

请帮我解决这个问题。

我的代码:

Bitmap bm_PhotoProof = null;
    Bitmap bm_AddressProof = null;

    private static final String TEMP_PHOTO_FILE = "tmp_ihis.jpg";
    private static final int REQ_CODE_PICK_IMAGE_PHOTOPROOF = 0;
    private static final int REQ_CODE_PICK_IMAGE_ADDRESSPROOF = 1;
imgPhotoProof.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
                cameraIntent.putExtra("outputFormat",
                        Bitmap.CompressFormat.JPEG.toString());
                startActivityForResult(cameraIntent,
                        REQ_CODE_PICK_IMAGE_PHOTOPROOF);
            }
        });

        imgAddressProof.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub

                Intent cameraIntent = new Intent(
                        android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
                cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempUri());
                cameraIntent.putExtra("outputFormat",
                        Bitmap.CompressFormat.JPEG.toString());
                startActivityForResult(cameraIntent,
                        REQ_CODE_PICK_IMAGE_ADDRESSPROOF);
            }
        });
private Uri getTempUri() {
        return Uri.fromFile(getTempFile());
    }

    private File getTempFile() {

        File f = new File(Environment.getExternalStorageDirectory(),
                TEMP_PHOTO_FILE);
        try {
            f.createNewFile();
        } catch (IOException e) {
            Log.e("getTempFile()->", e.getMessage().toString());
        }

        return f;
    }

    public void cropCapturedImage(Uri picUri, String Type) {

        Intent cropIntent = new Intent("com.android.camera.action.CROP");
        cropIntent.setDataAndType(picUri, "image/*");
        cropIntent.putExtra("crop", "true");
        cropIntent.putExtra("aspectX", 0);
        cropIntent.putExtra("aspectY", 0);
        cropIntent.putExtra("outputX", 256);
        cropIntent.putExtra("outputY", 256);
        cropIntent.putExtra("return-data", true);

        if (Type.equals("Photo")) {
            startActivityForResult(cropIntent, 5);
        } else {
            startActivityForResult(cropIntent, 6);
        }
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == REQ_CODE_PICK_IMAGE_PHOTOPROOF) {

                File tempFile = getTempFile();
                cropCapturedImage(Uri.fromFile(tempFile), "Photo");

            } else if (requestCode == REQ_CODE_PICK_IMAGE_ADDRESSPROOF) {

                File tempFile = getTempFile();
                cropCapturedImage(Uri.fromFile(tempFile), "Address");
            }

            if (resultCode != Activity.RESULT_CANCELED) {
                if (requestCode == 5) {

                    if (data != null) {
                        Bundle extras = data.getExtras();
                        bm_PhotoProof = extras.getParcelable("data");
                    }

                    imgPhotoProof_Pic.setImageBitmap(bm_PhotoProof);

                    File tempFile = getTempFile();
                    if (tempFile.exists()) {
                        tempFile.delete();
                    }
                }
                if (requestCode == 6) {

                    if (data != null) {
                        Bundle extras = data.getExtras();
                        bm_AddressProof = extras.getParcelable("data");
                    }

                    imgAddressProof_Pic.setImageBitmap(bm_AddressProof);

                    File tempFile = getTempFile();
                    if (tempFile.exists()) {
                        tempFile.delete();
                    }
                }
            }
        }
    }

1 个答案:

答案 0 :(得分:0)

我已经解决了这个问题。

之前,我正在删除文件。现在,我已经清除了缓存,它解决了我的问题。

File tempFile = getTempFile();
      if (tempFile.exists()) {
      tempFile.delete();
}


File tempFile = getTempFile();
File cacheDir = getActivity().getCacheDir();
File file = new File(cacheDir, getTempFile().toString());
file.delete();