2015-04-17 52 views
1

我有這樣的代碼進行裁剪圖像從畫廊或相機選擇:問題與作物意圖

public class selectimage extends Activity { 
private final int GALLERY_ACTIVITY_CODE=200; 
private final int RESULT_CROP = 400; 
private static Uri mCapturedImageURI; 
private String capturedImageFilePath; 
private static int CAPTURE_PICTURE_INTENT; 
private static int RESULT_LOAD_IMAGE = 1; 
private Bitmap bm; 
@Override 
protected void onCreate(Bundle savedInstanceState) { 

    super.onCreate(savedInstanceState); 
    setContentView(R.layout.select_image); 
    ImageButton ib_camera=(ImageButton) findViewById(R.id.ib_camera); 
    ImageButton ib_gallery=(ImageButton) findViewById(R.id.ib_gallery); 
    ib_camera.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 

     /* Intent intent=new Intent 
     (android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
      startActivityForResult(intent, RESULT_CAMERA_IMAGE);*/ 
       String fileName = "temp.jpg"; 
       ContentValues values = new ContentValues(); 
       values.put(MediaStore.Images.Media.TITLE, fileName); 
       mCapturedImageURI = getContentResolver().insert 
      (MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values); 

       Intent intent = 
       new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 
       intent.putExtra 
      (MediaStore.EXTRA_OUTPUT, mCapturedImageURI); 
       startActivityForResult(intent, CAPTURE_PICTURE_INTENT); 
     } 
    }); 
    ib_gallery.setOnClickListener(new OnClickListener() { 

     @Override 
     public void onClick(View arg0) { 
      Intent i=new Intent 
     (Intent.ACTION_PICK,android.provider. 
     MediaStore.Images.Media.EXTERNAL_CONTENT_URI); 
      startActivityForResult(i, RESULT_LOAD_IMAGE); 

     } 
    }); 


    } 
    @Override 
    protected void onActivityResult(int requestCode, 
     int resultCode, Intent data) { 
    // TODO Auto-generated method stub 

    super.onActivityResult(requestCode, resultCode, data); 

    if(requestCode==RESULT_LOAD_IMAGE && resultCode==RESULT_OK 
     && data != null) 
    { 
     Uri Selected_image=data.getData(); 
     String[] filePathColumn = { MediaStore.Images.Media.DATA }; 
      Cursor cursor = getContentResolver().query(Selected_image, 
        filePathColumn, null, null, null); 
      cursor.moveToFirst(); 

      int columnIndex = cursor.getColumnIndex(filePathColumn[0]); 
      capturedImageFilePath = cursor.getString(columnIndex); 
      cursor.close(); 
      performCrop(capturedImageFilePath); 
      bm=BitmapFactory.decodeFile(capturedImageFilePath); 
      // ImageView iv=(ImageView) findViewById(R.id.iv1); 
      // iv.setImageBitmap(bm); 
    } 

    else if (requestCode == 
     RESULT_CROP && resultCode == Activity.RESULT_OK) { 
     Bundle extras = data.getExtras(); 
     Bitmap bb = extras.getParcelable("data"); 
     Intent intent=new Intent(selectimage.this,MainActivity.class); 
     String fileName = "myImage";//no .png or .jpg needed 
     try { 
      ByteArrayOutputStream bytes = new ByteArrayOutputStream(); 
      bb.compress(Bitmap.CompressFormat.PNG, 72, bytes); 
      FileOutputStream fo = 
      openFileOutput(fileName, selectimage.this.MODE_PRIVATE); 
      fo.write(bytes.toByteArray()); 
      // remember close file output 
      fo.close(); 
     } catch (Exception e) { 
      e.printStackTrace(); 
      fileName = null; 
     } 
     intent.putExtra("mybm",capturedImageFilePath); 
     startActivity(intent); 
    } 
    else { 
     /*bm=(Bitmap) data.getExtras().get("data");*/ 
     String[] projection = { MediaStore.Images.Media.DATA}; 
     Cursor cursor = 
     managedQuery(mCapturedImageURI, projection, null, null, null); 
     int column_index_data = cursor.getColumnIndexOrThrow 
     (MediaStore.Images.Media.DATA); 
     cursor.moveToFirst(); 
     capturedImageFilePath = cursor.getString(column_index_data); 
     performCrop(capturedImageFilePath); 
    } 

    /*Intent intent1=new Intent(this, MainActivity.class); 
    intent1.putExtra("mybm", capturedImageFilePath); 
    startActivity(intent1);*/ 
} 
@Override 
protected void onDestroy() { 
    finish(); 
    super.onDestroy(); 
} 
private void performCrop(String picUri) { 
    try { 
     //Start Crop Activity 

     Intent cropIntent = new Intent("com.android.camera.action.CROP"); 
     // indicate image type and Uri 
     File f = new File(picUri); 
     Uri contentUri = Uri.fromFile(f); 

     cropIntent.setDataAndType(contentUri, "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", 500); 
     cropIntent.putExtra("outputY", 580); 

     // retrieve data on return 
     cropIntent.putExtra("return-data", true); 
     // start the activity - we handle returning in onActivityResult 
     startActivityForResult(cropIntent, RESULT_CROP); 
    } 
    // respond to users whose devices do not support the crop action 
    catch (ActivityNotFoundException anfe) { 
     // display an error message 
     String errorMessage = "your device doesn't 
     support the crop action!"; 
     Toast toast = Toast.makeText(this, 
     errorMessage,  Toast.LENGTH_SHORT); 
     toast.show(); 
    } 
} 
    } 

後裁剪圖像將被保存和URI將被髮送到下一個活動,但問題是在performcrop()方法,當所選擇的圖像尺寸是這樣的:

cropIntent.putExtra("aspectX", 1); 
cropIntent.putExtra("aspectY", 1.3); 
// indicate output X and Y 
cropIntent.putExtra("outputX", 280); 
cropIntent.putExtra("outputY", 280); 

evrything工作正常,但是當我們改變像下面這段代碼:

cropIntent.putExtra("aspectX", 1); 
cropIntent.putExtra("aspectY", 1.3); 
// indicate output X and Y 
cropIntent.putExtra("outputX", 1000); 
cropIntent.putExtra("outputY", 1300); 

或最後兩行中的任何其他大小,下一個活動不會打開。 有什麼問題?

安卓的minSdkVersion = 「7」

在接受作物活動的logcat:

04-17 11:37:16.186: D/dalvikvm(1230): GC_FOR_ALLOC freed 1895K, 36% free 10945K/17031K, paused 42ms 
04-17 11:37:16.186: I/dalvikvm-heap(1230): Grow heap (frag case) to 11.415MB for 672816-byte allocation 
04-17 11:37:16.258: D/dalvikvm(1230): GC_CONCURRENT freed 652K, 36% free 10949K/17031K, paused 5ms+4ms 
04-17 11:37:16.337: D/dalvikvm(1230): GC_FOR_ALLOC freed 288K, 38% free 10661K/17031K, paused 43ms 
04-17 11:37:16.347: I/dalvikvm-heap(1230): Grow heap (frag case) to 11.049MB for 580016-byte allocation 
04-17 11:37:16.407: D/dalvikvm(1230): GC_CONCURRENT freed <1K, 35% free 11228K/17031K, paused 5ms+5ms 
04-17 11:37:16.447: W/WindowManager(705): Failure taking screenshot for (180x300) to layer 21025 
04-17 11:37:16.577: W/NetworkManagementSocketTagger(705): setKernelCountSet(10011, 1) failed with errno -2 
04-17 11:37:16.597: E/JavaBinder(705): !!! FAILED BINDER TRANSACTION !!! 
04-17 11:37:16.637: I/WindowManager(705): createSurface Window{4152ca20 com.chatresepid.billboard/com.chatresepid.billboard.selectimage paused=false}: DRAW NOW PENDING 
04-17 11:37:17.077: W/NetworkManagementSocketTagger(705): setKernelCountSet(10003, 0) failed with errno -2 
+0

你在logcat中得到了什麼? –

+0

你可以發佈你的logcat謝謝。 –

+1

我覺得這個問題與圖片大小有關。 –

回答

2

Android的多年平均值有com.android.camera.action.CROP意圖。使用它非常不可靠。 看看this link。使用一些第三方庫進行可靠的裁剪。

+0

這不是答案,也不是崩潰的原因,因爲它似乎是OP安裝了支持裁剪的應用程序。 – Opiatefuchs