简体   繁体   English

Android Photo Gallery无法从OnActivityResult中的数据返回正确的Uri

[英]Android Photo Gallery does not return correct Uri from data in OnActivityResult

I am using android studio. 我正在使用android studio。 I tried to process a photo that is in the gallery. 我试图处理图库中的照片。

I call startActivityForResult(photoPickerIntent, REQUEST_IMAGE_Load); 我叫startActivityForResult(photoPickerIntent, REQUEST_IMAGE_Load);

In protected void onActivityResult(int requestCode, int resultCode, Intent data) 在受保护的void onActivityResult(int requestCode, int resultCode, Intent data)

the Uri returns from data.getData() is not correct. Uridata.getData()返回的值不正确。 like 喜欢

content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F128/ORIGINAL/NONE/1980269994 content://com.google.android.apps.photos.contentprovider/-1/1/content%3A%2F%2Fmedia%2Fexternal%2Fimages%2Fmedia%2F128/ORIGINAL/NONE/1980269994

That has no image name. 没有图像名称。

How to get around it? 如何解决呢? Thanks. 谢谢。

If You are Using 如果您正在使用

requirdUri = Uri.fromFile(theSrcPath);

Your Device Is Above Nauget you Should use this code from get URI from Camera and Gallery 您的设备在Nauget以上,您应该使用从Camera and Gallery获取URI中的代码

you Should Create Provider to user below Method. 您应该在“方法”下面为用户创建提供者。 for This You Have To Create One Folder inside 为此,您必须在其中创建一个文件夹

res - > Xml --> and Add One File Named File_path_public.xml. res-> Xml->并添加一个名为File_path_public.xml的文件。 Note : You Can Choose Any File Name 注意:您可以选择任何文件名

and Inside That File Put Code 并在该文件放置代码内部

<?xml version="1.0" encoding="UTF-8"?>

-<paths xmlns:android="http://schemas.android.com/apk/res/android">

<external-path path="." name="external_files"/>

</paths>

And In Your Manifest File Inside Application Tag Create One Provider Like: 并在您的清单文件中的应用程序标记内创建一个提供程序,例如:

<!-- this is For Access External file Storage -->
<provider
    android:name="android.support.v4.content.FileProvider"
    android:authorities="com.example.appName.provider"
    android:exported="false"
    android:grantUriPermissions="true">
    <meta-data
        android:name="android.support.FILE_PROVIDER_PATHS"
        android:resource="@xml/file_paths_public" />
</provider>

and Then Create This Function Below: 然后在下面创建此函数:

public static Uri getUriFromFile(Context theCtx, File theSrcPath) {
        Uri requirdUri = null;

        // Above Compile SDKversion: 25 -- Uri.fromFile Not working
        // So we have to use Provider
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
            requirdUri = FileProvider.getUriForFile(theCtx,
                    theCtx.getApplicationContext().getPackageName() + CommonUtils.PROVIDER_FILE_EXTENSION,
                    theSrcPath);
        } else {
            requirdUri = Uri.fromFile(theSrcPath);
        }

        return requirdUri;
    }

Note That You Should Have Permission For External Storage In Manifest. 请注意,清单上应该具有外部存储的权限。

If you want image name then you should create a file then get a name from that.for example: 如果要使用图像名称,则应创建一个文件,然后从中获取一个名称。例如:

File f = new File(uri.getPath());
String imageName = f.getName();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM