通过在Android中传递图像文件路径来裁剪图像
我已经尝试了下面的代码。但是,它总是生成160*160维的图像通过在Android中传递图像文件路径来裁剪图像,android,image,crop,Android,Image,Crop,我已经尝试了下面的代码。但是,它总是生成160*160维的图像 try { //call the standard crop action intent (the user device may not support it) Intent cropIntent = new Intent("com.android.camera.action.CROP"); //indicate image type and Uri cropIntent.setDataAnd
try {
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(Uri.fromFile(pictureFile), "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 100);
cropIntent.putExtra("aspectY", 100);
cropIntent.putExtra("scale", true);
//indicate output X and Y
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 500);
//retrieve data on return
cropIntent.putExtra("return-data", true);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);
} catch(ActivityNotFoundException anfe) {
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
}
我想通过传递图像的路径来裁剪图像。
我不想从默认相机应用程序或图库中捕获/拾取。请在这方面帮助我。通过这种方式,您可以缩放图像:
Bitmap.createScaledBitmap(bitmap,50,50,true);
我已经解决了这个问题,在调用Intent之前创建了一个新文件,并通过Intent传递这个文件路径来存储裁剪后的图像。这是解决这个问题的办法
private Uri mCropImagedUri;
private final int CROP_IMAGE = 100;//unique request code number. Which is used to identify the request result in onActivityResult()
/**Crop the image
* @return returns <tt>true</tt> if crop supports by the device,otherwise false*/
private boolean performCropImage(){
try {
if(mFinalImageUri!=null){
//call the standard crop action intent (the user device may not support it)
Intent cropIntent = new Intent("com.android.camera.action.CROP");
//indicate image type and Uri
cropIntent.setDataAndType(mFinalImageUri, "image/*");
//set crop properties
cropIntent.putExtra("crop", "true");
//indicate aspect of desired crop
cropIntent.putExtra("aspectX", 1);
cropIntent.putExtra("aspectY", 1);
cropIntent.putExtra("scale", true);
//indicate output X and Y
cropIntent.putExtra("outputX", 500);
cropIntent.putExtra("outputY", 500);
//retrieve data on return
cropIntent.putExtra("return-data", false);
File f = createNewFile("CROP_");
try {
f.createNewFile();
} catch (IOException ex) {
VLLog.e("io", ex.getMessage());
}
mCropImagedUri = Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, mCropImagedUri);
//start the activity - we handle returning in onActivityResult
startActivityForResult(cropIntent, CROP_IMAGE);
return true;
}
}
catch(ActivityNotFoundException anfe){
//display an error message
String errorMessage = "Whoops - your device doesn't support the crop action!";
Toast toast = Toast.makeText(this, errorMessage, Toast.LENGTH_SHORT);
toast.show();
return false;
}
return false;
}
private File createNewFile(String prefix){
if(prefix==null || "".equalsIgnoreCase(prefix)){
prefix="IMG_";
}
File newDirectory = new File(Environment.getExternalStorageDirectory()+"/mypics/");
if(!newDirectory.exists()){
if(newDirectory.mkdir()){
VLLog.d(mContext.getClass().getName(), newDirectory.getAbsolutePath()+" directory created");
}
}
File file = new File(newDirectory,(prefix+System.currentTimeMillis()+".jpg"));
if(file.exists()){
//this wont be executed
file.delete();
try {
file.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
}
return file;
}
private-Uri-mCropImagedUri;
私有最终整数裁剪图像=100//唯一的请求代码。用于标识onActivityResult()中的请求结果
/**裁剪图像
*@return如果设备支持裁剪,则返回true,否则返回false*/
私有布尔performCropImage(){
试一试{
if(mfinalmageuri!=null){
//调用标准裁剪操作意图(用户设备可能不支持)
Intent-cropIntent=newintent(“com.android.camera.action.CROP”);
//指示图像类型和Uri
setDataAndType(mFinalImageUri,“image/*”);
//设置作物属性
cropIntent.putExtra(“作物”、“真实”);
//表示所需作物的外观
cropIntent.putExtra(“aspectX”,1);
cropIntent.putExtra(“aspectY”,1);
cropIntent.putExtra(“比例”,真实);
//指示输出X和Y
cropIntent.putExtra(“输出”,500);
cropIntent.putExtra(“输出”,500);
//返回时检索数据
cropIntent.putExtra(“返回数据”,false);
文件f=createNewFile(“CROP_”);
试一试{
f、 createNewFile();
}捕获(IOEX异常){
VLLog.e(“io”,例如getMessage());
}
mCropImagedUri=Uri.fromFile(f);
cropIntent.putExtra(MediaStore.EXTRA_输出,mCropImagedUri);
//启动活动-我们在onActivityResult中处理返回
startActivityForResult(cropIntent,CROP_图像);
返回true;
}
}
捕获(ActivityNotFoundException anfe){
//显示错误消息
String errorMessage=“哇-您的设备不支持裁剪操作!”;
Toast Toast=Toast.makeText(this,errorMessage,Toast.LENGTH\u SHORT);
toast.show();
返回false;
}
返回false;
}
私有文件createNewFile(字符串前缀){
if(前缀==null | |“”.equalsIgnoreCase(前缀)){
prefix=“IMG”;
}
File newDirectory=新文件(Environment.getExternalStorageDirectory()+“/mypics/”;
如果(!newDirectory.exists()){
if(newDirectory.mkdir()){
VLLog.d(mContext.getClass().getName(),newDirectory.getAbsolutePath()+“已创建目录”);
}
}
File File=新文件(newDirectory,(前缀+System.currentTimeMillis()+“.jpg”);
if(file.exists()){
//这不会被执行
delete();
试一试{
createNewFile();
}捕获(IOE异常){
e、 printStackTrace();
}
}
返回文件;
}
因此,在这里,我们不应该担心activityResult()方法中的数据
以下是有关裁剪图像的完整信息。我用这个来解决这个问题。
Android不支持内置的裁剪意图。你不应该假设它是可用的
相反,您应该使用自己的解决方案,或者使用第三方库,例如:
请在此处阅读更多信息:
我必须根据用户选择裁剪图像,然后我发布了答案。@HarshParikh:实际上还有一个用于此目的的SO徽章(),这是一种鼓励行为。为什么你投票否决了我。因此,如果这是你首选的解决方案,我写了这句话,tnx,在经过几天的努力终于奏效之后,请将其标记为这样并结束提问。多么愚蠢的虫子!>:这里的“mFinalImageUri”指的是什么?