我正在尝试裁剪从Gallery
和Camera
中选择的图像。我不能裁剪大部分图像。它没有调用onActivityResult()
。以下是我的代码。每次在裁剪图像后点击OK
,我都会得到以下log cat输出。
代码:
private void doCrop() {
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities(
intent, 0);
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Can not find image crop app",
Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
// intent.putExtra("outputX", 200);
// intent.putExtra("outputY", 200);
// intent.putExtra("aspectX", 1);
// intent.putExtra("aspectY", 1);
intent.putExtra("outputX", myImg.getWidth());
intent.putExtra("outputY", myImg.getHeight());
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
intent.putExtra("outputFormat",
Bitmap.CompressFormat.PNG.toString());
intent.putExtra("crop", "true");
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent(new ComponentName(res.activityInfo.packageName,
res.activityInfo.name));
startActivityForResult(i, CROP_FROM);
}
}
}
logcat的:
08-13 15:59:29.220: D/skia(7247): WARNING: One-shot allocator has already allocated (alloc count = 1)
08-13 15:59:29.290: D/skia(7247): WARNING: One-shot allocator has already allocated (alloc count = 1)
08-13 15:59:30.140: D/dalvikvm(288): GC_EXPLICIT freed 8K, 8% free 6788K/7367K, paused 2ms+2ms
08-13 15:59:34.110: D/dalvikvm(7247): GC_FOR_ALLOC freed 4824K, 44% free 10227K/18055K, paused 26ms
08-13 15:59:34.110: I/dalvikvm-heap(7247): Grow heap (frag case) to 11.929MB for 1956256-byte allocation
08-13 15:59:34.140: D/dalvikvm(7247): GC_FOR_ALLOC freed <1K, 33% free 12137K/18055K, paused 23ms
08-13 15:59:34.190: D/dalvikvm(7247): GC_CONCURRENT freed 1K, 25% free 13662K/18055K, paused 2ms+3ms
08-13 15:59:34.230: D/skia(7247): WARNING: One-shot allocator has already allocated (alloc count = 1)
08-13 15:59:34.280: D/dalvikvm(7247): GC_FOR_ALLOC freed 2554K, 33% free 12132K/18055K, paused 30ms
08-13 15:59:34.310: D/skia(7247): WARNING: One-shot allocator has already allocated (alloc count = 1)
08-13 15:59:34.340: D/dalvikvm(7247): GC_FOR_ALLOC freed <1K, 27% free 13331K/18055K, paused 30ms
08-13 15:59:34.390: D/dalvikvm(7247): GC_FOR_ALLOC freed 2000K, 33% free 12132K/18055K, paused 30ms
08-13 15:59:34.420: D/skia(7247): WARNING: One-shot allocator has already allocated (alloc count = 1)
08-13 15:59:34.450: D/dalvikvm(7247): GC_FOR_ALLOC freed <1K, 26% free 13430K/18055K, paused 33ms
08-13 15:59:34.500: D/skia(7247): WARNING: One-shot allocator has already allocated (alloc count = 1)
08-13 15:59:34.530: D/dalvikvm(7247): GC_FOR_ALLOC freed 2097K, 28% free 13152K/18055K, paused 30ms
08-13 15:59:34.550: E/JavaBinder(7247): !!! FAILED BINDER TRANSACTION !!!
08-13 15:59:34.560: W/InputManagerService(142): Window already focused, ignoring focus gain of: com.android.internal.view.IInputMethodClient$Stub$Proxy@40988068
08-13 15:59:39.620: D/dalvikvm(224): GC_EXPLICIT freed <1K, 31% free 17001K/24391K, paused 3ms+3ms
答案 0 :(得分:1)
请尝试完整的源代码。
我正在使用相同的项目。以下是该活动的代码:
代码:
private void doCrop() {
final ArrayList<CropOption> cropOptions = new ArrayList<CropOption>();
Intent intent = new Intent("com.android.camera.action.CROP");
intent.setType("image/*");
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
} else {
intent.setData(mImageCaptureUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
intent.putExtra("return-data", true);
if (size == 1) {
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROP_FROM_CAMERA);
} else {
for (ResolveInfo res : list) {
final CropOption co = new CropOption();
co.title = getPackageManager().getApplicationLabel(res.activityInfo.applicationInfo);
co.icon = getPackageManager().getApplicationIcon(res.activityInfo.applicationInfo);
co.appIntent= new Intent(intent);
co.appIntent.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
cropOptions.add(co);
}
CropOptionAdapter adapter = new CropOptionAdapter(getApplicationContext(), cropOptions);
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Choose Crop App");
builder.setAdapter( adapter, new DialogInterface.OnClickListener() {
public void onClick( DialogInterface dialog, int item ) {
startActivityForResult( cropOptions.get(item).appIntent, CROP_FROM_CAMERA);
}
});
builder.setOnCancelListener( new DialogInterface.OnCancelListener() {
@Override
public void onCancel( DialogInterface dialog ) {
if (mImageCaptureUri != null ) {
getContentResolver().delete(mImageCaptureUri, null, null );
mImageCaptureUri = null;
}
}
} );
AlertDialog alert = builder.create();
alert.show();
}
}
}