React Native - Get file name from android content uri

david890ch


I need to get file name from android content uri, for example:
content://com.android.providers.downloads.documents/document/15

I try to used react-native-fs stat but it return error of "File does not exist".

How can I do it?

david890ch

I not found pure solution in react native, so I created native package in android and created module in react native from so I can use it.
This the the android code:

@ReactMethod
    public void getFileNameFromUri(String filepath, Promise promise) {
        Uri uri = Uri.parse(filepath);
        String fileName = "";

        if (uri.getScheme().equals("content")) {
            try {
                Cursor cursor = reactContext.getContentResolver().query(uri, null, null, null, null);

                if (cursor.moveToFirst()) {
                    fileName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
                    Log.d("FS", "Real File name: " + fileName);
                }
                cursor.close();
                promise.resolve(fileName);
            } catch (IllegalArgumentException ignored) {
                promise.reject("1", "Can not get cursor");
            }
        } else {
            promise.resolve(fileName);
        }
    }

This is how to module in react native from android native code: react-native-modules-android

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Get content uri from file path in android

Get a Content URI from a File URI?

Android: Getting a file URI from a content URI?

Convert content:// uri to file:// uri using react native

React Native [Android] URI from Google Drive w/ 'React-Native-Document-Picker' & 'react-native-get-real-path'

Create File object from Android content URI

Android get EXIF data from content URI

get Path name from Uri Android Lollipop

Convert content:// URI to actual path for Android using React-Native

Get URI file path from inconsistent Android native file pick returned URIs

Android Kotlin (beginner) - using File() with Uri returned from ACTION_GET_CONTENT

Read content Uri from File from SharedPreferences and get persistent permission on it

Is it possible to Get exact file path from content URI?

How to get image height and width from uri on React Native?

React-Native-Camera get height and width from uri

How to open a file from Android content URI in NativeScript?

Android: How to convert File Uri to Content Uri

React Native file name

Get file name from URI string in C#

react native access content:// uri fails

get android apk from react-native

Android: Get path of a text file from URI brings a weird path

How to Get File Path from URI in Android Oreo (8.1) or above

How to get file path from URI in Android N

Get real path from uri file and upload it android

How to get file name from content-disposition

How to get country code from country name in React Native?

React: Get meta content by name

React Native local images with URI on Android