Android: Getting a file URI from a content URI?

177,405

Solution 1

Just use getContentResolver().openInputStream(uri) to get an InputStream from a URI.

http://developer.android.com/reference/android/content/ContentResolver.html#openInputStream(android.net.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;
}
Share:
177,405

Related videos on Youtube

JMRboosties
Author by

JMRboosties

Updated on April 29, 2022

Comments

  • JMRboosties
    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 a file:// 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
      Phil Lello about 13 years
      What calls are you using that don't like content URIs?
    • Mooing Duck
      Mooing Duck about 8 years
      There 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
    JMRboosties about 13 years
    Would this conflict with a user who uses Astro to get a file Uri?
  • Jason LeBrun
    Jason LeBrun about 13 years
    Check 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
    JMRboosties about 13 years
    Great 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
    Jason LeBrun about 13 years
    Actually, 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
    Jason LeBrun about 13 years
    Why do you need to create a file, exactly? You can open audio files via content URIs as well, using the Android MediaPlayer object.
  • JMRboosties
    JMRboosties about 13 years
    Ah, good point. I suppose I could do a simple uri.toString(); call on the content Uri, saved that in the SharedPrefs, then do a Uri.parse(uriString); to load the player... that should work right?
  • AlikElzin-kilaka
    AlikElzin-kilaka over 12 years
    Is there a way to get the File instead of the InputStream (from content:...)?
  • ldam
    ldam about 11 years
    Thanks, this worked perfectly. I couldn't use an InputStream like the accepted answer suggests.
  • Danyal Aytekin
    Danyal Aytekin about 10 years
    Works for most things, except Google Drive. Any idea how to deal with Drive? (FileNotFoundException)
  • Danyal Aytekin
    Danyal Aytekin about 10 years
    @kilaka You can get the file path but it's painful. See stackoverflow.com/a/20559418/294855
  • Faux Pas
    Faux Pas over 9 years
    Thanks a ton! Works for Kitkat.
  • Mahantesh M Ambi
    Mahantesh M Ambi about 9 years
    How can i get other column values like MediaStore.Audio.Media.TITLE from Uri of type "file://" ?
  • 7heViking
    7heViking about 9 years
    Works great on samsung galaxy nexus with 4.3
  • paulgavrikov
    paulgavrikov almost 9 years
    This works only for local files, eg it does not work for Google Drive
  • Calvin
    Calvin almost 9 years
    how about getting same thing for audio
  • monkey0506
    monkey0506 over 8 years
    This 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
    Reza Mohammadi over 8 years
    Sometimes works, sometimes returns file:///storage/emulated/0/... which doesn't exists.
  • Edward Falk
    Edward Falk about 8 years
    Is the column "_data" (android.provider.MediaStore.Images.ImageColumns.DATA) always guaranteed to exist if the scheme is content://?
  • Kimi Chiu
    Kimi Chiu about 8 years
    But sometimes we only need the path. We don't really have to load the file into memory.
  • user1643723
    user1643723 over 7 years
    This 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 on content:// uris, this is the official approach, encouraged by Google engineers.
  • user1643723
    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 with File 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
    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
    mrid over 6 years
    this gives me a FileNotFoundException
  • Bondax
    Bondax about 6 years
    i don't think that's the right way to do it in an application. sadly i'm using it in a quicky project
  • Thracian
    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
    Thracian about 6 years
    And 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
    KRK over 4 years
    I know it is not related to the question, but how would you then use the byte[] videoBytes;? Most answers only show how to use InputStream with an image.
  • Mofor Emmanuel
    Mofor Emmanuel over 4 years
    this method needs an update, android.provider.MediaStore.Images.ImageColumns.DATA is deprecated as of now
  • justdan0227
    justdan0227 about 4 years
    Thanks, been looking at this for a week. Don't like having to copy a file for this but it does work.
  • star4z
    star4z almost 4 years
  • Sourav Kannantha B
    Sourav Kannantha B over 3 years
    ImageColumns.DATA is deprecated now. What to do.
  • Rowan Gontier
    Rowan Gontier over 3 years
    Lifesaver. Only thing that has worked for me in regard to pdfs.
  • iamkdblue
    iamkdblue about 3 years
    Thanks, dude once again your answer help me! I hope i can upvote again ;)
  • Ümañg ßürmån
    Ümañg ßürmån almost 3 years
    Ahh, Thank you so much :)
  • Up2Marius
    Up2Marius over 2 years
    The best answer here.
  • jagadishlakkurcom jagadishlakk
    jagadishlakkurcom jagadishlakk over 2 years
    Thanks its working
  • KJEjava48
    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??