应用程序在从库中裁剪图像后停止

时间:2016-07-06 04:49:34

标签: android image

我无法在裁剪后检索图像,这是我的代码。我从图库中选择图像,裁剪图像然后按保存。但是按下保存后应用程序停止了。我只是想从中裁剪图像画廊,然后将其上传到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: android.os.BinderProxy@d10a5a5 time:3990330
07-06 16:17:16.890 18390-18390/jp.co.dorakuken.tcodereaderlibrary I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d10a5a5 time:3990331
07-06 16:17:25.426 18390-18390/jp.co.dorakuken.tcodereaderlibrary I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d10a5a5 time:3998866
07-06 16:17:27.858 18390-18390/jp.co.dorakuken.tcodereaderlibrary I/Timeline: Timeline: Activity_idle id: android.os.BinderProxy@d10a5a5 time:4001298

1 个答案:

答案 0 :(得分: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;

    }