我有这个代码用于裁剪从图库或相机中选择的图像:
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 :(得分:1)
Android没有com.android.camera.action.CROP
意图。使用它是非常不可靠的。
看看this link。使用一些第三方库进行可靠的裁剪。