在我的应用程序中,我想从gallary中获取图像并裁剪该图像并将裁剪图像设置为imageview。使用以下代码从gallary中获取图像
Intent photoPickerIntent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 0);
使用follaowing代码将onActivityResult方法发送到数据之后。然后使用默认应用程序裁剪图像。
Uri selectedImageUri = data.getData();
selectedImagePath = getPath(selectedImageUri);
startCrop(selectedImagePath);
这是获取路径方法
public String getPath(Uri uri)
{
try{
String[] projection = { MediaStore.Images.Media.DATA,MediaStore.Images.ImageColumns.ORIENTATION };
Cursor cursor = managedQuery(uri, projection, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
if(cursor.getString(column_index)==null)
{
Toast.makeText(EmailRegistrationActivity.this, "Please Select Image from Gallery",Toast.LENGTH_SHORT).show();
return "";
}else
{
Log.e("return string======",cursor.getString(column_index));
return cursor.getString(column_index);
}
}catch(Exception e)
{
e.printStackTrace();
Toast.makeText(EmailRegistrationActivity.this, "Please Select Image from Gallery",Toast.LENGTH_SHORT).show();
return "";
}
}
这是裁剪方法
public void startCrop(String m_Path)
{
Intent m_cropIntent = new Intent("com.android.camera.action.CROP");
// indicate image type and Uri
m_cropIntent.setDataAndType(Uri.fromFile(new File(m_Path)), "image/*");
// set crop properties
m_cropIntent.putExtra("crop", "true");
// indicate aspect of desired crop
m_cropIntent.putExtra("aspectX", 1);
m_cropIntent.putExtra("aspectY", 1);
// indicate output X and Y
m_cropIntent.putExtra("outputX", 256);
m_cropIntent.putExtra("outputY", 256);
Uri cropUri = getTempFile();
strImagePath_Crop=cropUri.getPath();
m_cropIntent.putExtra(MediaStore.EXTRA_OUTPUT, cropUri);
// retrieve data on return
m_cropIntent.putExtra("return-data", true);
// start the activity - we handle returning in onActivityResult
startActivityForResult(m_cropIntent, 535);
}
之后在onactivityresult方法中,我通过以下代码获取来自另一个开关盒的数据
try{
Log.e("strImagePath_Crop======",strImagePath_Crop);
Bitmap bitmap=getCorrectBitmap(strImagePath_Crop);
profilepic.setBackgroundDrawable(new BitmapDrawable(new RoundedImageView(getApplicationContext()).getCroppedBitmap(bitmap, 350)));
}catch(Exception e){
e.printStackTrace();
}
这段代码也在samsungs4和galaxy nexus中工作。但是在 htc one 它没有用。它在裁剪图像后返回null值。解决方法是什么。请指导我。在此先感谢..
答案 0 :(得分:0)
删除m_cropIntent.putExtra("return-data", true);