[英]Why can't I upload a picture from gallery in my android app, if the picture was taken with the camera app from android?
I want to upload an image from gallery and show it on my Android app.我想从画廊上传一张图片并在我的 Android 应用程序上显示它。 This works fine if I select an image from the screenshot folder for example.
例如,如果我 select 来自屏幕截图文件夹的图像,这将正常工作。 But when I select an image from the folder where the pics, taken with the phone camera, are stored, it does not work.
但是,当我 select 存储用手机相机拍摄的照片的文件夹中的图像时,它不起作用。
public void UploadPicture(View v) {
Intent pickPhoto = new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(pickPhoto , 1);
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) {
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
switch (requestCode) {
case 1:
if (resultCode == RESULT_OK) {
Uri selectedImage = imageReturnedIntent.getData();
imageView.setImageURI(selectedImage);
}
break;
}
}
In my opinion, these problems can be solved by ImagePicker Libraries such as Ucrop.在我看来,这些问题可以通过 Ucrop 等 ImagePicker 库来解决。 Ucrop Library For Android
用于 Android 的 Ucrop 库
Make sure you have Runtime permission Handled.确保您已处理运行时权限。 For easier permission handling you can use Dexter Dexter
为了更轻松地处理权限,您可以使用 Dexter Dexter
Make sure u have External Storage Read permission in your manifest file.确保您在清单文件中具有外部存储读取权限。 You may also require both Read/Write permission if you want to take image from camera.
如果您想从相机拍摄图像,您可能还需要读/写权限。
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Note: for android version above Pie paste these attributes in application tag in manifest.xml:注意:对于上面的 android 版本 Pie 将这些属性粘贴到 manifest.xml 的应用程序标签中:
<application
android:preserveLegacyExternalStorage="true"
android:requestLegacyExternalStorage="true"
... >
...
...
</application>
Make sure you have give run time permission to choose image from gallery and permissions in the manifest.确保您已授予运行时从图库中选择图像的权限以及清单中的权限。
Use android:requestLegacyExternalStorage="true" in the manifest also.也在清单中使用android:requestLegacyExternalStorage="true" 。
Then for opening the gallery use this code:然后打开画廊使用这个代码:
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction("android.intent.action.PICK");
startActivityForResult(intent, PICK_IMAGE_REQUEST);
In onActivityResult:在 onActivityResult 中:
if (requestCode == PICK_IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null) {
mImageUri = data.getData();
//Use glide for better result
Glide.with(this).load(mImageUri).into(mImageView);
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.