[英]How to handle picked images from gallery and camera in different devices?
I have these codes to get an image from gallery or take photo with camera and after cropping it i want to show it in a bitmap imageview
. 我有这些代码可以从图库中获取图像或使用相机拍照,并在裁剪后想imageview
图imageview
显示它。
My problem is they work good in some devices and don't work in others! 我的问题是,它们在某些设备上运行良好,而在其他设备上却无法运行! please tell me which part of my code is making the problem ! 请告诉我代码的哪一部分导致了问题! Thank you 谢谢
For example: On LG G4 i not able to crop , but the photo from gallery and camera shows on image view On Samsung Galaxy Alpha i can crop , image from camera shows good but i cant choose from gallery! 例如:在LG G4上,我无法裁剪,但是图库和相机中的照片显示在图像视图上。在三星Galaxy Alpha上,我可以裁剪,相机中的图像显示良好,但是我不能从图库中选择!
What should i do to work fine in all devices? 我应该怎么做才能在所有设备上正常工作?
private void showFileChooser() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("cropped-rect", "true");
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,
"Complete action using"), PICK_FROM_GALLERY);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Select Picture"), PICK_IMAGE_REQUEST);
}
private void captureWithCamera() {
// call android default camera
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT,
MediaStore.Images.Media.EXTERNAL_CONTENT_URI.toString());
// ******** code for crop image
intent.putExtra("crop", "true");
intent.putExtra("aspectX", 1);
intent.putExtra("aspectY", 1);
intent.putExtra("outputX", 200);
intent.putExtra("outputY", 200);
intent.putExtra("cropped-rect", "true");
try {
intent.putExtra("return-data", true);
startActivityForResult(Intent.createChooser(intent,"Complete action using"), PICK_FROM_CAMERA);
} catch (ActivityNotFoundException e) {
// Do nothing for now
}
}
I think the problem must be in how i handle the returned data in onActivityResult method! 我认为问题一定出在我如何处理onActivityResult方法中返回的数据! Please take a look at my code: 请看一下我的代码:
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_FROM_GALLERY && resultCode == RESULT_OK && data != null && data.getData() != null) {
Uri filePath = data.getData();
try {
//Getting the Bitmap from Gallery
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(), filePath);
//Setting the Bitmap to ImageView
imageView.setImageBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
if (requestCode == PICK_FROM_CAMERA) {
Bundle extras = data.getExtras();
if (extras != null) {
//Bitmap photo = extras.getParcelable("data");
bitmap = extras.getParcelable("data");
imageView.setImageBitmap(bitmap);
}
}
}
Possible solutions would be to put uri
with imageChooser
's intent. 可能的解决方案是将uri
与imageChooser
的意图放在一起。 Like this; 像这样;
private void showCamera() {
try {
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), "DCIM");
if (!file.exists()) {
file.mkdirs();
}
File localFile2 = new File(file + File.separator + "IMG_" + String.valueOf(System.currentTimeMillis()) + ".jpg");
imageUri = Uri.fromFile(localFile2);
Intent cameraIntent = new Intent("android.media.action.IMAGE_CAPTURE");
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
cameraIntent.setClipData(ClipData.newRawUri(null, Uri.fromFile(localFile2)));
}
startActivityForResult(cameraIntent, REQUEST_CAMERA);
} catch (Exception localException) {
Toast.makeText(ActivityAddMemory.this, "Exception:" + localException, Toast.LENGTH_SHORT).show();
}
}
uri
by accessing intent.getData()
or thumbnail Bitmap
using intent.getExtras().get("data")
. 您可以通过使用intent.getExtras().get("data")
访问intent.getData()
或缩略图Bitmap
来使用uri
。 Don't use imageUri
that you passed with that intent. 不要使用通过该意图传递的imageUri
。 On capturing from camera, in some devices intent is not null, but it's data is null. 从相机捕获时,在某些设备中,intent不为空,但其数据为空。 And in some devices, intent is null. 在某些设备中,intent为空。 Check both in onActivityResult
. 同时检查onActivityResult
。
Uri uri; if (intent == null || intent.getData() == null) { uri = this.imageUri; } else { uri = intent.getData(); }
Use this uri
to display/upload your image. 使用此uri
显示/上传图像。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.