2014-09-27 84 views
0

我正在使用相機功能Capture圖像從不同的按鈕2次。捕獲的圖像後,它會去Crop選項,用戶可以裁剪圖像。它也工作正常。

但是,現在的問題是,當用戶捕獲第二個圖像,然後應用程序重定向到Crop圖像,只顯示第一個圖像,而不是第二個捕獲圖像。我將其設置爲ImageView之後刪除圖像。不知道什麼是錯的?

請幫我解決這個問題。

我的代碼:使用Android的捕獲圖像裁剪圖像問題

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

Android不具備''CROP' Intent':http://commonsware.com/blog/2013/01/23/no-android-不 - 不具備的,作物intent.html – CommonsWare 2014-09-27 12:16:05

回答

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