Android: Getting a file URI from a content URI?

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:


However, using the popular file manager application Astro, I get the following:

  • How to get timestamp of incoming xmpp message?
  • Creating a directory in /sdcard fails
  • Security Exception only on Android 6
  • Is it possible to capture/receive IR signals in an Android application?
  • Backup/restore sqlite db in android
  • Gradle script to autoversion and include the commit hash in Android
  • 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();
    startActivityForResult(Intent.createChooser(ringIntent, "Select Ringtone"), SELECT_RINGTONE);

    Thank you very much! Please let me know if I can provide more code to help with answering.

    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.

  • Android: Clear Cache of All Apps?
  • What is “”?
  • Error when selecting text from Textview (java.lang.IndexOutOfBoundsException: setSpan (-1 … -1) starts before 0)
  • Unique ID of Android device
  • How to animate floating action button using android activity transition?
  • Space between keyboard and EditText in Android
  • 3 Solutions collect form web for “Android: Getting a file URI from a content URI?”

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

    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);
        filePath = cursor.getString(0);
    } else {
        filePath = _uri.getPath();
    Log.d("","Chosen path = "+ filePath);

    Trying to handle the URI with content:// scheme by calling ContentResolver.query()is not a good solution. On HTC Desire running 4.2.2 you could get NULL as a query result.

    Why not to use ContentResolver instead?

    Android Babe is a Google Android Fan, All about Android Phones, Android Wear, Android Dev and Android Games Apps and so on.