获取输出裁剪图像 URI
Posted
技术标签:
【中文标题】获取输出裁剪图像 URI【英文标题】:Get Output Cropped Image URI 【发布时间】:2017-12-13 11:51:59 【问题描述】:我已经在我的应用中使用 android intent 实现了图像裁剪,如下所示:
CropIntent = new Intent("com.android.camera.action.CROP");
我已收到裁剪后的图像并显示在我的 UI 上,但我想执行更多用例,例如上传裁剪后的图像 uri。问题是activity中没有返回uri。
这是我的裁剪图像意图 sn-p:
galleryFAB.setOnClickListener(new View.OnClickListener()
@Override
public void onClick(View view)
Log.d(TAG, "Gallery Btn Clicked");
selectFromGallery();
);
private void selectFromGallery()
galleryIntent = new Intent(Intent.ACTION_GET_CONTENT);
galleryIntent.setType("image/*");
startActivityForResult(Intent.createChooser(galleryIntent, "Choose From"), 2);
我在活动结果方法中的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data)
super.onActivityResult(requestCode, resultCode, data);
InputStream inputStream;
if (requestCode == 2 && resultCode == RESULT_OK)
if (data != null)
cropImageUri = data.getData();
userImage.setImageURI(cropImageUri);
try
originalBitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), cropImageUri);
inputStream = getContentResolver().openInputStream(data.getData());
originalBitmap = BitmapFactory.decodeStream(inputStream);
Log.d(TAG, "Original bitmap byte count is:\t" + originalBitmap.getByteCount());
resizedBitmap = getResizedBitmap(originalBitmap, 200);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
resizedBitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
Log.d(TAG, "Resized bitmap byte count is:\t" + resizedBitmap.getByteCount());
try
File file = saveBitmap("avatar_image");
upLoadFile(file);
catch (IOException ex)
ex.printStackTrace();
byte[] bytes = stream.toByteArray();
/**
* For Shared Preferences
* **/
encodedImage = Base64.encodeToString(bytes, Base64.DEFAULT);
imagePrefs = getSharedPreferences("ImagePrefs", Context.MODE_PRIVATE);
SharedPreferences.Editor editor = imagePrefs.edit();
editor.putString("image_user", encodedImage).commit();
upLoadBytes(bytes);
catch (IOException e)
e.printStackTrace();
cropImage();
else if (requestCode == PERM_CODE)
if (data != null)
//I want to get the cropped image uri here but unable to. Have tried converting the bitmap rcvd here to uri but in activity, ntn is returned. kindly guide me
Bundle bundle = data.getExtras();
originalBitmap = bundle.getParcelable("data");
userImage.setImageBitmap(resizedBitmap);
我的裁剪图像功能:
private void cropImage()
try
CropIntent = new Intent("com.android.camera.action.CROP");
CropIntent.setDataAndType(cropImageUri, "image/*");
CropIntent.putExtra("crop", true);
CropIntent.putExtra("outputX", 200);
CropIntent.putExtra("outputY", 200);
CropIntent.putExtra("aspectX", 1);
CropIntent.putExtra("aspectY", 1);
CropIntent.putExtra("scale", true);
CropIntent.putExtra("return-data", true);
startActivityForResult(CropIntent, PERM_CODE);
catch (ActivityNotFoundException ex)
谁能说出如何获取输出 uri?谢谢。
【问题讨论】:
Android 没有CROP
Intent
。有很多image-cropping libraries available for Android。请使用一个。
【参考方案1】:
如果裁剪后的图像显示在 ImageView 中,请按照this link 将其保存到设备存储,然后您可以上传文件。
【讨论】:
以上是关于获取输出裁剪图像 URI的主要内容,如果未能解决你的问题,请参考以下文章