简体   繁体   English

如果图片是使用 android 的相机应用程序拍摄的,为什么我无法在我的 android 应用程序中上传图片库中的图片?

[英]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.

相关问题 在Android中使用摄像头:当照片是由摄像头拍摄而未从图库中选择时,为什么Cursor为null? - Working with camera in Android: Why is Cursor null when the picture is taken by camera and not selected from gallery? 如何将图片从Android画廊附加到我的笔记应用程序中? - How to attach a picture from android gallery into my note app? 在我的应用程序中,我无法将我从相机拍摄的照片传输到应用程序 - In my app I can't transfer the picture I took from the Camera to the app android-从图库/相机拍照的ANR - android - ANR on taking picture from gallery/camera Android - 如何从相机或图库拍摄照片 - Android - How to take a picture from camera or gallery Android:相机和图库运行时异常中的图片 - Android:picture from Camera and gallery runtime exception 在重新启动手机之前,Android自定义相机应用图片不会显示在图库中 - Android Custom Camera app picture not appearing in gallery until I reboot my phone 在Android应用中显示拍摄的照片 - Display taken picture in android app Android:保存从App拍摄的照片并将其附加到电子邮件 - Android: Saving a picture taken from an App and attaching it to email 在imageview中使用时从相机或图库拍摄的照片,其方向发生变化,有时在Android中垂直拉伸 - Picture taken from camera or gallery when using in imageview its orientation getting changed, and sometimes vertically stretched in Android
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM