Getting the Absolute File Path from Content URI for searched images
If you absolutely need a local copy of the file, you are going to need to open the InputStream
copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy
is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream
from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
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));
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();
}
}
}
Get the path of an image stored in android phone gallery
Pass the uri that you are getting in onActivityResult and use the returned String.
public String getPathFromURI(Uri ContentUri) {
String res = null;
String[] proj = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver()
.query(ContentUri, proj, null, null, null);
if (cursor != null) {
cursor.moveToFirst();
res = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
cursor.close();
}
return res;
}
Get content uri from file path in android
Try with:
ImageView.setImageURI(Uri.fromFile(new File("/sdcard/cats.jpg")));
Or with:
ImageView.setImageURI(Uri.parse(new File("/sdcard/cats.jpg").toString()));
Related Topics
Capture Image from Camera and Display in Activity
How to Determine When Fragment Becomes Visible in Viewpager
How to Draw a Path on a Map Using Kml File
Repeat a Task With a Time Delay
Horizontal Listview in Android
Android Broadcastreceiver on Startup - Keep Running When Activity Is in Background
How to Implement Onfragmentinteractionlistener
How to Get Complete Address from Latitude and Longitude
How to Check the Current Status of the Gps Receiver
"Cannot Resolve Symbol R" in Android Studio
How to Pick an Image from Gallery (Sd Card) For My App
Android: Check Whether the Phone Is Dual Sim
Adding a Library/Jar to an Eclipse Android Project
"Default Activity Not Found" on Android Studio Upgrade
How to Create Our Own Listener Interface in Android