android - 裁剪 Intent 问题

标签 android crop

我有这个代码用于裁剪从画廊或相机中选择的图像:

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 发送到下一个 Activity ,但问题出在 performcrop() 方法中,当所选图像尺寸如下时:

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

一切正常,但是当我们像下面这样更改这段代码时:

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

或最后两行中的任何其他尺寸,下一个 Activity 不会打开。 有什么问题吗?

android:minSdkVersion="7"

接受裁剪 Activity 的 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

最佳答案

Android 没有 com.android.camera.action.CROP Intent 。用起来很不靠谱。 看this link .使用一些第三方库进行可靠的裁剪。

关于android - 裁剪 Intent 问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29697937/

相关文章:

Angularjs多图像上传与裁剪

c++ - 如何使用 C++ 从 OpenCV 中的框架中删除黑色边框?

android - 如何从视频url获取帧并在ImageView中显示

android - 最佳实践 : Getting location updates in background

android - 在 android 中退出时清除应用程序缓存

java - 在 fragment 之间滑动时如何显示进度?

php - 调整图像大小以完美适合确定的框

java Android - 以编程方式处理图像缩放/裁剪

android - 可拉出的三角孔

Android 应用程序在方向更改时使用设备区域设置而不是强制区域设置