2015-07-01 39 views
0

我有一個表單,用戶可以從圖庫中選擇圖像或拍攝圖像。當用戶拍攝圖像時它可以正常工作,但當用戶從圖庫中選擇圖像時,它不能正常工作,然後我要求裁剪圖像的意圖。從圖庫中裁剪出的Android圖像不能正常工作

在模擬器上從圖庫中裁剪圖像的工作發現,但在我測試過的兩個手機上,當我選擇裁剪應用程序時,裁剪應用程序崩潰或者它沒有被破壞,它不起作用並在imageView上顯示圖像。

這是我的代碼:

final int PIC_CROP = 2; 
    final int CAMERA_CAPTURE = 1; 
    final int PICK_FROM_FILE = 3; 
    private Uri picUri; 

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

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

      picUri = data.getData();// get image URI 
      performCrop(); 
     } else if (requestCode == PIC_CROP) {// crop and compress image 
      if (resultCode != RESULT_OK) 
       return; 
      Bundle extras = data.getExtras(); 
      Bitmap thePic = extras.getParcelable("data"); 
      FileOutputStream out = null; 
      ByteArrayOutputStream bao = new ByteArrayOutputStream(); 
      double width = thePic.getWidth(); 
      double height = thePic.getHeight(); 
      double ratio = 650/width; 
      int newheight = (int) (ratio * height); 
      thePic = Bitmap.createScaledBitmap(thePic, 650, newheight, true); 
      try { 
       out = new FileOutputStream(file); 
       thePic.compress(Bitmap.CompressFormat.JPEG, 295, out); 
       thePic.compress(Bitmap.CompressFormat.JPEG, 295, bao); 

       byte[] ba = bao.toByteArray(); 
       switch (whichimg) { 
       case 1: 
        simg1 = Base64.encodeBytes(ba); 
        break; 
      } catch (Exception e) { 

      } 

     } 
    } 

if (item == 0) {// take photo 
        Intent captureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(captureIntent, CAMERA_CAPTURE); 
        alert.dismiss(); 
       } else if (item == 1) {// photo from gallery 
        Intent intent = new Intent(); 
        intent.setType("image/*"); 
        intent.setAction(Intent.ACTION_GET_CONTENT); 
        startActivityForResult(
          Intent.createChooser(intent, (getString(R.string.choosephototo))), 
          CAMERA_CAPTURE); 
        alert.dismiss(); 
       } 

這是裁剪代碼:

private void performCrop() { 
    try { 
     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     cropIntent.setDataAndType(picUri, "image/*"); 
     cropIntent.putExtra("crop", "true"); 
     //cropIntent.putExtra("aspectX", 1); 
     //cropIntent.putExtra("aspectY", 1); 
     //cropIntent.putExtra("outputX", 356); 
     //cropIntent.putExtra("outputY", 256); 
     cropIntent.putExtra("return-data", true); 
     startActivityForResult(cropIntent, PIC_CROP); 
    } catch (ActivityNotFoundException anfe) { 
     MyToast.makeText(NewAdd2.this, DariGlyphUtils.reshapeText(getString(R.string.devicecouldcop))); 
    } 
} 

你能幫助我嗎?爲什麼它不能正常工作?

感謝

+0

請加上崩潰的蹤跡! – Vaiden

+0

@Vaiden我的應用程序不會崩潰,裁剪應用程序崩潰 –

+0

您仍然會在logcat – Vaiden

回答

0

試試下面鏈接,你的問題,它會工作

可以在問題的performCrop()方法

Link For Crop

+0

中獲取堆棧跟蹤,請嘗試此鏈接,它已經過測試 – Mano