How to get the Full file path from URI
Use:
String path = yourAndroidURI.uri.getPath() // "/mnt/sdcard/FileName.mp3"
File file = new File(new URI(path));
or
String path = yourAndroidURI.uri.toString() // "file:///mnt/sdcard/FileName.mp3"
File file = new File(new URI(path));
Convert content:// URI to actual path for Android using React-Native
I took the function submitted by @Madhukar Hebbar and made it into a React Native Node Module.
You can find it here: react-native-get-real-path
Thus to achieve what you want, you can use the above module in conjunction with react-native-fs
You can then do something like this when you want to upload the selected image from Camera Roll:
RNGRP.getRealPathFromURI(this.state.selectedImageUri).then(path =>
RNFS.readFile(path, 'base64').then(imageBase64 =>
this.props.actions.sendImageAsBase64(imageBase64)
)
)
Get real path from URI, Android KitKat new storage access framework
Note: This answer addresses part of the problem. For a complete solution (in the form of a library), look at Paul Burke's answer.
You could use the URI to obtain document id
, and then query either MediaStore.Images.Media.EXTERNAL_CONTENT_URI
or MediaStore.Images.Media.INTERNAL_CONTENT_URI
(depending on the SD card situation).
To get document id:
// Will return "image:x*"
String wholeID = DocumentsContract.getDocumentId(uriThatYouCurrentlyHave);
// Split at colon, use second item in the array
String id = wholeID.split(":")[1];
String[] column = { MediaStore.Images.Media.DATA };
// where id is equal to
String sel = MediaStore.Images.Media._ID + "=?";
Cursor cursor = getContentResolver().
query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
column, sel, new String[]{ id }, null);
String filePath = "";
int columnIndex = cursor.getColumnIndex(column[0]);
if (cursor.moveToFirst()) {
filePath = cursor.getString(columnIndex);
}
cursor.close();
Reference: I'm not able to find the post that this solution is taken from. I wanted to ask the original poster to contribute here. Will look some more tonight.
Get filename and path from URI from mediastore
Below API 19 use this code to get File Path from URI:
public String getRealPathFromURI(Context context, Uri contentUri) {
Cursor cursor = null;
try {
String[] proj = { MediaStore.Images.Media.DATA };
cursor = context.getContentResolver().query(contentUri, proj, null, null, null);
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
cursor.moveToFirst();
return cursor.getString(column_index);
} finally {
if (cursor != null) {
cursor.close();
}
}
}
android get real path by Uri.getPath()
Is it really necessary for you to get a physical path?
For example, ImageView.setImageURI()
and ContentResolver.openInputStream()
allow you to access the contents of a file without knowing its real path.
Related Topics
How to Create a Custom Home-Screen Replacement Application for Android
Permission from Manifest Doesn't Work in Android 6
How to Tell If Android App Is Running in the Foreground
Reading an Image File into Bitmap from Sdcard, Why am I Getting a Nullpointerexception
How to Get a Color from Hexadecimal Color String
How to Add a Fragment Inside a Viewpager Using Nested Fragment (Android 4.2)
Convert a Bitmap to Grayscale in Android
How to Create a Notification with Notificationcompat.Builder
Playing a Video in Videoview in Android
Get Bitmap Attached to Imageview
Android Layout Replacing a View with Another View on Run Time
Detecting When Screen Is Locked
Android Device Not Shown with Adb
Storing Dimensions in Xml File in Android
Android: How to Make a Clickable Map Image with Each Country Producing a Different Action