简体   繁体   中英

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

I am using android studio. I tried to process a photo that is in the gallery.

I call startActivityForResult(photoPickerIntent, REQUEST_IMAGE_Load);

In protected void onActivityResult(int requestCode, int resultCode, Intent data)

the Uri returns from data.getData() is not correct. like

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

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. 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();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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