2016-07-06 85 views
-1

我無法在裁剪後檢索圖像,這裏是我的代碼。我從圖庫中選擇圖像,裁剪該圖像然後按save.But按下保存應用程序停止。我是隻是從畫廊裁剪圖像,然後將其上傳到ImageView。從圖庫中裁剪圖像後停止應用程序

final int RESULT_GALLERY = 1; 
final int CROP_PIC = 2; 
protected void setListeners(){ 
switchCamera.setOnClickListener(new View.OnClickListener() { 
    @Override 
    public void onClick(View v) { 
     Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
     startActivityForResult(galleryIntent , RESULT_GALLERY); 
     } 
    }); 
} 

這裏是ActivityResult

protected void onActivityResult(int requestCode, int resultCode, Intent data) { 
if(resultCode==RESULT_OK) 
{ 
    if(requestCode==RESULT_GALLERY) 
     { 
      picUri = data.getData(); 
      performCrop(); 
     } 
    else if (requestCode==CROP_PIC) 
    { 
     String imagePath= getRealPathFromURI(picUri); 
     android.graphics.Bitmap myBitmap = BitmapFactory.decodeFile(imagePath); 
     ImgView.setImageBitmap(myBitmap); 
    } 
} 

這個函數的裁剪操作。

private void performCrop() { 
// take care of exceptions 
try { 
// call the standard crop action intent (the user device may not 
// support it) 
Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
// indicate image type and Uri 
cropIntent.setDataAndType(picUri, "image/*"); 
// set crop properties 
cropIntent.putExtra("crop", "true"); 
// indicate aspect of desired crop 
cropIntent.putExtra("aspectX", 1); 
cropIntent.putExtra("aspectY", 1); 
// indicate output X and Y 
cropIntent.putExtra("outputX", 256); 
cropIntent.putExtra("outputY", 256); 
// retrieve data on return 
cropIntent.putExtra("return-data", true); 
// start the activity - we handle returning in onActivityResult 
startActivityForResult(cropIntent, CROP_PIC); 
} 
// respond to users whose devices do not support the crop action 
catch (ActivityNotFoundException anfe) { 
    Toast toast = Toast.makeText(this, "This device doesn't support the crop action!", Toast.LENGTH_SHORT); 
    toast.show(); 
} 

在logcat的

07-06 16:17:16.061 18390-18390/? I/art: Late-enabling -Xcheck:jni 
07-06 16:17:16.626 18390-18417/jp.co.dorakuken.tcodereaderlibrary D/OpenGLRenderer: Use EGL_SWAP_BEHAVIOR_PRESERVED: true 
07-06 16:17:16.628 18390-18390/jp.co.dorakuken.tcodereaderlibrary D/Atlas: Validating map... 
07-06 16:17:16.660 18390-18417/jp.co.dorakuken.tcodereaderlibrary I/Adreno: QUALCOMM build     : 41a310d, I21d2ab1dda 
                      Build Date      : 08/24/15 
                      OpenGL ES Shader Compiler Version: E031.25.03.09 
                      Local Branch      : 
                      Remote Branch     : quic/LA.BF64.1.2.1_rb2.29 
                      Remote Branch     : NONE 
                      Reconstruct Branch    : NOTHING 
07-06 16:17:16.666 18390-18417/jp.co.dorakuken.tcodereaderlibrary I/OpenGLRenderer: Initialized EGL, version 1.4 
07-06 16:17:16.673 18390-18417/jp.co.dorakuken.tcodereaderlibrary D/OpenGLRenderer: Enabling debug mode 0 
07-06 16:17:16.890 18390-18390/jp.co.dorakuken.tcodereaderlibrary I/Timeline: Timeline: Activity_idle id: [email protected] time:3990330 
07-06 16:17:16.890 18390-18390/jp.co.dorakuken.tcodereaderlibrary I/Timeline: Timeline: Activity_idle id: [email protected] time:3990331 
07-06 16:17:25.426 18390-18390/jp.co.dorakuken.tcodereaderlibrary I/Timeline: Timeline: Activity_idle id: [email protected] time:3998866 
07-06 16:17:27.858 18390-18390/jp.co.dorakuken.tcodereaderlibrary I/Timeline: Timeline: Activity_idle id: [email protected] time:4001298 
+3

應用程序停止時,你能發佈的調試日誌或崩潰報告嗎? –

+0

@intellignt_idiot在調試日誌中,它只顯示「Frame is not availabe」,然後應用程序停止,並且沒有崩潰報告。 – pes

+1

你可以給我一個你的代碼的小測試項目,我可以直接運行和調試嗎? –

回答

0

試試這個

String imagePath = getRealPathFromURI(picUri); 
Bitmap myBitmap = BitmapFactory.decodeFile(imagePath); 


private String getRealPathFromURI(Uri contentURI) { 

     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 

     Cursor cursor = this.getContentResolver().query(contentURI, 
       filePathColumn, null, null, null); 
     cursor.moveToFirst(); 

     int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
     String filePath = cursor.getString(columnIndex); 
     Log.i("getrealpathfrom uri", "" + filePath); 
     cursor.close(); 
     return filePath; 

    }