Android: Getting a file URI from a content URI?
Solution 1
Just use getContentResolver().openInputStream(uri)
to get an InputStream
from a URI.
Solution 2
This is an old answer with deprecated and hacky way of overcoming some specific content resolver pain points. Take it with some huge grains of salt and use the proper openInputStream API if at all possible.
You can use the Content Resolver to get a file://
path from the content://
URI:
String filePath = null;
Uri _uri = data.getData();
Log.d("","URI = "+ _uri);
if (_uri != null && "content".equals(_uri.getScheme())) {
Cursor cursor = this.getContentResolver().query(_uri, new String[] { android.provider.MediaStore.Images.ImageColumns.DATA }, null, null, null);
cursor.moveToFirst();
filePath = cursor.getString(0);
cursor.close();
} else {
filePath = _uri.getPath();
}
Log.d("","Chosen path = "+ filePath);
Solution 3
Try this....
get File from a content uri
fun fileFromContentUri(context: Context, contentUri: Uri): File {
// Preparing Temp file name
val fileExtension = getFileExtension(context, contentUri)
val fileName = "temp_file" + if (fileExtension != null) ".$fileExtension" else ""
// Creating Temp file
val tempFile = File(context.cacheDir, fileName)
tempFile.createNewFile()
try {
val oStream = FileOutputStream(tempFile)
val inputStream = context.contentResolver.openInputStream(contentUri)
inputStream?.let {
copy(inputStream, oStream)
}
oStream.flush()
} catch (e: Exception) {
e.printStackTrace()
}
return tempFile
}
private fun getFileExtension(context: Context, uri: Uri): String? {
val fileType: String? = context.contentResolver.getType(uri)
return MimeTypeMap.getSingleton().getExtensionFromMimeType(fileType)
}
@Throws(IOException::class)
private fun copy(source: InputStream, target: OutputStream) {
val buf = ByteArray(8192)
var length: Int
while (source.read(buf).also { length = it } > 0) {
target.write(buf, 0, length)
}
}
Solution 4
If you have a content Uri with content://com.externalstorage...
you can use this method to get absolute path of a folder or file on Android 19 or above.
public static String getPath(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
System.out.println("getPath() uri: " + uri.toString());
System.out.println("getPath() uri authority: " + uri.getAuthority());
System.out.println("getPath() uri path: " + uri.getPath());
// ExternalStorageProvider
if ("com.android.externalstorage.documents".equals(uri.getAuthority())) {
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
System.out.println("getPath() docId: " + docId + ", split: " + split.length + ", type: " + type);
// This is for checking Main Memory
if ("primary".equalsIgnoreCase(type)) {
if (split.length > 1) {
return Environment.getExternalStorageDirectory() + "/" + split[1] + "/";
} else {
return Environment.getExternalStorageDirectory() + "/";
}
// This is for checking SD Card
} else {
return "storage" + "/" + docId.replace(":", "/");
}
}
}
return null;
}
You can check each part of Uri using println
. Returned values for my SD card and device main memory are listed below. You can access and delete if file is on memory, but I wasn't able to delete file from SD card using this method, only read or opened image using this absolute path. If you find a solution to delete using this method, please share.
SD CARD
getPath() uri: content://com.android.externalstorage.documents/tree/612E-B7BF%3A/document/612E-B7BF%3A
getPath() uri authority: com.android.externalstorage.documents
getPath() uri path: /tree/612E-B7BF:/document/612E-B7BF:
getPath() docId: 612E-B7BF:, split: 1, type: 612E-B7BF
MAIN MEMORY
getPath() uri: content://com.android.externalstorage.documents/tree/primary%3A/document/primary%3A
getPath() uri authority: com.android.externalstorage.documents
getPath() uri path: /tree/primary:/document/primary:
getPath() docId: primary:, split: 1, type: primary
If you wish to get Uri with file:///
after getting path use
DocumentFile documentFile = DocumentFile.fromFile(new File(path));
documentFile.getUri() // will return a Uri with file Uri
Solution 5
Inspired answers are Jason LaBrun & Darth Raven. Trying already answered approaches led me to below solution which may mostly cover cursor null cases & conversion from content:// to file://
To convert file, read&write the file from gained uri
public static Uri getFilePathFromUri(Uri uri) throws IOException {
String fileName = getFileName(uri);
File file = new File(myContext.getExternalCacheDir(), fileName);
file.createNewFile();
try (OutputStream outputStream = new FileOutputStream(file);
InputStream inputStream = myContext.getContentResolver().openInputStream(uri)) {
FileUtil.copyStream(inputStream, outputStream); //Simply reads input to output stream
outputStream.flush();
}
return Uri.fromFile(file);
}
To get filename use, it will cover cursor null case
public static String getFileName(Uri uri) {
String fileName = getFileNameFromCursor(uri);
if (fileName == null) {
String fileExtension = getFileExtension(uri);
fileName = "temp_file" + (fileExtension != null ? "." + fileExtension : "");
} else if (!fileName.contains(".")) {
String fileExtension = getFileExtension(uri);
fileName = fileName + "." + fileExtension;
}
return fileName;
}
There is good option to converting from mime type to file extention
public static String getFileExtension(Uri uri) {
String fileType = myContext.getContentResolver().getType(uri);
return MimeTypeMap.getSingleton().getExtensionFromMimeType(fileType);
}
Cursor to obtain name of file
public static String getFileNameFromCursor(Uri uri) {
Cursor fileCursor = myContext.getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null);
String fileName = null;
if (fileCursor != null && fileCursor.moveToFirst()) {
int cIndex = fileCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
if (cIndex != -1) {
fileName = fileCursor.getString(cIndex);
}
}
return fileName;
}
Related videos on Youtube
JMRboosties
Updated on April 29, 2022Comments
-
JMRboosties about 2 years
In my app the user is to select an audio file which the app then handles. The problem is that in order for the app to do what I want it to do with the audio files, I need the URI to be in file format. When I use Android's native music player to browse for the audio file in the app, the URI is a content URI, which looks like this:
content://media/external/audio/media/710
However, using the popular file manager application Astro, I get the following:
file:///sdcard/media/audio/ringtones/GetupGetOut.mp3
The latter is much more accessible for me to work with, but of course I want the app to have functionality with the audio file the user chooses regardless of the program they use to browse their collection. So my question is, is there a way to convert the
content://
style URI into afile://
URI? Otherwise, what would you recommend for me to solve this problem? Here is the code which calls up the chooser, for reference:Intent ringIntent = new Intent(); ringIntent.setType("audio/mp3"); ringIntent.setAction(Intent.ACTION_GET_CONTENT); ringIntent.addCategory(Intent.CATEGORY_OPENABLE); startActivityForResult(Intent.createChooser(ringIntent, "Select Ringtone"), SELECT_RINGTONE);
I do the following with the content URI:
m_ringerPath = m_ringtoneUri.getPath(); File file = new File(m_ringerPath);
Then do some FileInputStream stuff with said file.
-
Phil Lello about 13 yearsWhat calls are you using that don't like content URIs?
-
Mooing Duck about 8 yearsThere are a lot of content Uris where you cannot get the file path, because not all content Uris have filepaths. Don't use filepaths.
-
-
JMRboosties about 13 yearsWould this conflict with a user who uses Astro to get a file Uri?
-
Jason LeBrun about 13 yearsCheck the scheme of the URI returned to you from the chooser activity. If if uri.getScheme.equals("content"), open it with a content resolver. If the uri.Scheme.equals("file"), open it using normal file methods. Either way, you'll end up with an InputStream that you can process using common code.
-
JMRboosties about 13 yearsGreat thanks a ton! One more thing... I put the File path of the audio file the user chooses in a SharedPrefs field for the app so the user can preview the selected audio file at any time. When the scheme is file, this works fine. However with content this has some issues. Is there a way to successfully create a file with a content Uri?
-
Jason LeBrun about 13 yearsActually, I just re-read the docs for getContentResolver().openInputStream(), and it works automatically for schemes of "content" or "file", so you don't need to check the scheme... if you can safely assume that it's always going to be content:// or file:// then openInputStream() will always work.
-
Jason LeBrun about 13 yearsWhy do you need to create a file, exactly? You can open audio files via content URIs as well, using the Android MediaPlayer object.
-
JMRboosties about 13 yearsAh, good point. I suppose I could do a simple
uri.toString();
call on the content Uri, saved that in the SharedPrefs, then do aUri.parse(uriString);
to load the player... that should work right? -
AlikElzin-kilaka over 12 yearsIs there a way to get the File instead of the InputStream (from content:...)?
-
ldam about 11 yearsThanks, this worked perfectly. I couldn't use an InputStream like the accepted answer suggests.
-
Danyal Aytekin about 10 yearsWorks for most things, except Google Drive. Any idea how to deal with Drive? (FileNotFoundException)
-
Danyal Aytekin about 10 years@kilaka You can get the file path but it's painful. See stackoverflow.com/a/20559418/294855
-
Faux Pas over 9 yearsThanks a ton! Works for Kitkat.
-
Mahantesh M Ambi about 9 yearsHow can i get other column values like
MediaStore.Audio.Media.TITLE
from Uri of type "file://" ? -
7heViking about 9 yearsWorks great on samsung galaxy nexus with 4.3
-
paulgavrikov almost 9 yearsThis works only for local files, eg it does not work for Google Drive
-
Calvin almost 9 yearshow about getting same thing for audio
-
monkey0506 over 8 yearsThis answer is insufficient for someone who is using a closed-source API that relies on Files rather than FileStreams, but yet wants to use the OS to allow the user to select the file. The answer @DanyalAytekin referenced was exactly what I needed (and in fact, I was able to trim a lot of the fat because I know exactly what kinds of files I'm working with).
-
Reza Mohammadi over 8 yearsSometimes works, sometimes returns file:///storage/emulated/0/... which doesn't exists.
-
Edward Falk about 8 yearsIs the column "_data" (
android.provider.MediaStore.Images.ImageColumns.DATA
) always guaranteed to exist if the scheme iscontent://
? -
Kimi Chiu about 8 yearsBut sometimes we only need the path. We don't really have to load the file into memory.
-
user1643723 over 7 yearsThis is a major anti-pattern. Some ContentProviders do provide that column, but you are not guaranteed to have read/write access to
File
when you try to bypass ContentResolver. Use ContentResolver methods to operate oncontent://
uris, this is the official approach, encouraged by Google engineers. -
user1643723 over 7 years"The path" is useless if you don't have access rights for it. For example, if an application gives you a
content://
uri, corresponding to file in it's private internal directory, you will not be able to use that uri withFile
APIs in new Android versions. ContentResolver is designed to overcome this kind of security limitations. If you got the uri from ContentResolver, you can expect it to just work. -
eRaisedToX over 7 years@JasonLeBrun I am able to read from it using getContentResolver().openInputStream(uri) BUT I need a way to write into that..kindly help
-
mrid over 6 yearsthis gives me a
FileNotFoundException
-
Bondax about 6 yearsi don't think that's the right way to do it in an application. sadly i'm using it in a quicky project
-
Thracian about 6 years@Bondax Yes, you should work with Content Uris instead file paths or File Uris. That's way storage access framework is introduced. But, if you wish to get file uri this is the most correct way of other answers since you use DocumentsContract class. If you check out Google samples in Github, you will see that they also use to this class to get sub folders of a folder.
-
Thracian about 6 yearsAnd DocumentFile class also new addition in api 19 and how you use uris from SAF. The correct way is to use a standard path for you app and ask user to give permission for a folder through SAF ui, save Uri string to shared preferences, and when it's needed access to folder with DocumentFile objects
-
KRK over 4 yearsI know it is not related to the question, but how would you then use the
byte[] videoBytes;
? Most answers only show how to useInputStream
with an image. -
Mofor Emmanuel over 4 yearsthis method needs an update, android.provider.MediaStore.Images.ImageColumns.DATA is deprecated as of now
-
justdan0227 about 4 yearsThanks, been looking at this for a week. Don't like having to copy a file for this but it does work.
-
star4z almost 4 yearsUpdated link for androidx: developer.android.com/reference/androidx/exifinterface/media/…
-
Sourav Kannantha B over 3 years
ImageColumns.DATA
is deprecated now. What to do. -
Rowan Gontier over 3 yearsLifesaver. Only thing that has worked for me in regard to pdfs.
-
iamkdblue about 3 yearsThanks, dude once again your answer help me! I hope i can upvote again ;)
-
Ümañg ßürmån almost 3 yearsAhh, Thank you so much :)
-
Up2Marius over 2 yearsThe best answer here.
-
jagadishlakkurcom jagadishlakk over 2 yearsThanks its working
-
KJEjava48 over 2 years@DanyalAytekin how can i get file from google drive and upload it to my server, do u have any idea now??