Quantcast
Channel: Active questions tagged react-native+android - Stack Overflow
Viewing all articles
Browse latest Browse all 30356

How to get real path from URI of a file in sdcard?

$
0
0

I'm using react-native-document-picker in order to pick files and send them to the server. After picking the file, I use RNFetchBlob.fs.stat(uri) from rn-fetch-blob package to get the real path from the URI that the picker gives me. It works fine with internal storage files but when I choose an image from "SD card" section the stat function throws an exception which says something like this:

failed to stat path null for it doesn't exist or it's not a folder

Surprisingly, when I choose "Images" section and go to the SD card folder and choose the very same image it works just fine!! I have the same problem with "Downloads" section, too.

enter image description here

The URI that the picker returns is this when I choose the image from "Images" section:

content://com.android.providers.media.documents/document/image%3A76431

and when I choose from "SD card" section: content://com.android.externalstorage.documents/document/F673-1818%3Abbb.png

Right now I'm using this code but it's not the right solution because it's somehow dirty and it doesn't work on all devices.

if(uri.substr(0,57) === "content://com.android.externalstorage.documents/document/"){
    let filePath = decodeURIComponent(uri.substr(57));
    let subUri = filePath.replace(/:\s*/g, "/");
    filePath = "/storage/" + subUri;
    return `file://${filePath}`
 }

Is there a nicer way to get the real path of a file in SD card from it's URI? I appreciate any idea.

OS: Android


Viewing all articles
Browse latest Browse all 30356

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>