如何保存返回种植数据的裁剪图像的机器人呢? [英] How to save cropped image in android instead of returning cropping data?
问题描述
我有一个应用程序,我可以让用户选择一个画廊图像或采取与相机拍摄照片,在那之后我给他裁剪的选项。
I have an application where I can let the user choose a gallery image or take a picture with camera, then after that I give him the option to crop it.
现在,当用户裁剪画廊农作物返回裁剪部分ByteArray的形象,是有可能传递参数要求图库农作物保存到文件(就像情况下的输出,当我开始一个意图捕捉和图像中的CAMER)
Now when the user crops the image the Gallery cropper returns ByteArray of the cropped part, is it possible to pass a parameter asking the Gallery cropper to save the output to file (Just like the case when I start a intent to capture and image in the camer)
例如:
这是裁剪的code,我使用:
This is the code of cropping that I Use:
// Initialize intent
Intent intent = new Intent("com.android.camera.action.CROP");
// set data type to be sent
intent.setType("image/*");
// get croppers available in the phone
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
// handle the case if there's no cropper in the phone
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
} else {
// now this is the case if cropper exists
// initialize the Uri for the captures or gallery image
Uri imageUri = Uri.fromFile(new File(mCurrentPhotoPath));
// Send the Uri path to the cropper intent
intent.setData(imageUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
// Here's my attempt to ask the intent to save output data as file
File f = null;
// Here I initialize empty file
try{
// This returns the file created
f = setUpCroppedFile();
mCurrentThumbPath = f.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
intent.putExtra("output", Uri.fromFile(f));
}
catch(IOException e){
e.printStackTrace();
}
// --------------------------------------------------------------------
// -----------> When changing this to false it worked <----------------
// --------------------------------------------------------------------
intent.putExtra("return-data", true);
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// If there's only 1 Cropper in the phone (e.g. Gallery )
if (size == 1) {
// get the cropper intent found
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROPPED_PHOTO);
}
// Please ignore the case if there are more . . I know how to handle it
更新:这个问题是固定的,我知道我会离开这个这里是万一别人发现它很有用(见里面的code以上的大评论)
推荐答案
这code是经过修改的,它的工作原理:
This code is the modified one and it works:
// Initialize intent
Intent intent = new Intent("com.android.camera.action.CROP");
// set data type to be sent
intent.setType("image/*");
// get croppers available in the phone
List<ResolveInfo> list = getPackageManager().queryIntentActivities( intent, 0 );
int size = list.size();
// handle the case if there's no cropper in the phone
if (size == 0) {
Toast.makeText(this, "Can not find image crop app", Toast.LENGTH_SHORT).show();
return;
} else {
// now this is the case if cropper exists
// initialize the Uri for the captures or gallery image
Uri imageUri = Uri.fromFile(new File(mCurrentPhotoPath));
// Send the Uri path to the cropper intent
intent.setData(imageUri);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("scale", true);
// Here's my attempt to ask the intent to save output data as file
File f = null;
// Here I initialize empty file
try{
// This returns the file created
f = setUpCroppedFile();
mCurrentThumbPath = f.getAbsolutePath();
intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(f));
intent.putExtra("output", Uri.fromFile(f));
}
catch(IOException e){
e.printStackTrace();
}
// --------------------------------------------------------------------
// -----------> When changing this to false it worked <----------------
// --------------------------------------------------------------------
intent.putExtra("return-data", false);
// --------------------------------------------------------------------
// --------------------------------------------------------------------
// If there's only 1 Cropper in the phone (e.g. Gallery )
if (size == 1) {
// get the cropper intent found
Intent i = new Intent(intent);
ResolveInfo res = list.get(0);
i.setComponent( new ComponentName(res.activityInfo.packageName, res.activityInfo.name));
startActivityForResult(i, CROPPED_PHOTO);
}
// Please ignore the case if there are more . . I know how to handle it
这篇关于如何保存返回种植数据的裁剪图像的机器人呢?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!