185

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.

ROMANIA_engineer
  • 54,432
  • 29
  • 203
  • 199
JMRboosties
  • 15,500
  • 20
  • 76
  • 116

10 Answers10

188

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)

AlikElzin-kilaka
  • 34,335
  • 35
  • 194
  • 277
Jason LeBrun
  • 13,037
  • 3
  • 46
  • 42
  • 1
    Would this conflict with a user who uses Astro to get a file Uri? – JMRboosties Apr 14 '11 at 01:22
  • 16
    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. – Jason LeBrun Apr 14 '11 at 01:37
  • 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? – JMRboosties Apr 14 '11 at 01:41
  • 25
    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 Apr 14 '11 at 01:50
  • Why do you need to create a file, exactly? You can open audio files via content URIs as well, using the Android MediaPlayer object. – Jason LeBrun Apr 14 '11 at 01:50
  • 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? – JMRboosties Apr 14 '11 at 02:01
  • 80
    Is there a way to get the File instead of the InputStream (from content:...)? – AlikElzin-kilaka Mar 15 '12 at 11:47
  • Works for most things, except Google Drive. Any idea how to deal with Drive? (FileNotFoundException) – Danyal Aytekin May 22 '14 at 11:20
  • 4
    @kilaka You can get the file path but it's painful. See http://stackoverflow.com/a/20559418/294855 – Danyal Aytekin May 22 '14 at 11:21
  • 12
    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). – monkey0506 Nov 20 '15 at 12:45
  • 1
    @JasonLeBrun I am able to read from it using getContentResolver().openInputStream(uri) BUT I need a way to write into that..kindly help – eRaisedToX Feb 09 '17 at 17:54
  • 3
    this gives me a `FileNotFoundException` – mrid Oct 04 '17 at 13:15
  • Updated link for androidx: https://developer.android.com/reference/androidx/exifinterface/media/ExifInterface – star4z Aug 07 '20 at 15:12
  • @DanyalAytekin how can i get file from google drive and upload it to my server, do u have any idea now?? – KJEjava48 Feb 01 '22 at 13:38
52

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);
Rafael Nobre
  • 5,062
  • 40
  • 40
25

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)
    }
}
Shaon
  • 2,496
  • 26
  • 27
14

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
fdermishin
  • 3,519
  • 3
  • 24
  • 45
Thracian
  • 43,021
  • 16
  • 133
  • 222
  • 1
    i don't think that's the right way to do it in an application. sadly i'm using it in a quicky project – Bondax Jun 22 '18 at 21:45
  • @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 Jun 23 '18 at 05:32
  • 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 – Thracian Jun 23 '18 at 05:36
14

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;
}
9

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? https://stackoverflow.com/a/29141800/3205334

Community
  • 1
  • 1
Darth Raven
  • 189
  • 4
  • 3
  • But sometimes we only need the path. We don't really have to load the file into memory. – Kimi Chiu Apr 02 '16 at 10:09
  • 4
    "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. – user1643723 Jan 10 '17 at 11:51
3

Well I am bit late to answer,but my code is tested

check scheme from uri:

 byte[] videoBytes;

if (uri.getScheme().equals("content")){
        InputStream iStream =   context.getContentResolver().openInputStream(uri);
            videoBytes = getBytes(iStream);
        }else{
            File file = new File(uri.getPath());
            FileInputStream fileInputStream = new FileInputStream(file);     
            videoBytes = getBytes(fileInputStream);
        }

In the above answer I converted the video uri to bytes array , but that's not related to question, I just copied my full code to show the usage of FileInputStream and InputStream as both are working same in my code.

I used the variable context which is getActivity() in my Fragment and in Activity it simply be ActivityName.this

context=getActivity(); //in Fragment

context=ActivityName.this;// in activity

Umar Ata
  • 4,170
  • 3
  • 23
  • 35
  • 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. – KRK Oct 01 '19 at 04:44
1

You can use the following android package which can be easier a bit for you

https://github.com/Blankj/AndroidUtilCode

Using the above package the code can be like

To Import use below Line

import com.blankj.utilcode.util.UriUtils;

Your code can be like

File f = UriUtils.uri2File(result);

Thanks

Mahesh Mahi
  • 41
  • 1
  • 5
0

If you're absolutely sure that your content: Uri points to a real file, you can do the following trick:

context.contentResolver.openFileDescriptor(uri, "r").use { fileDescriptor ->
    if (fileDescriptor == null) {
        throw NullPointerException("ParcelFileDescriptor from $uri was null")
    }
    val path = "/proc/${android.os.Process.myPid()}/fd/${fileDescriptor.fd}"
    val fileUri = Uri.fromFile(File(path).canonicalFile)
}

Here we open the file using ContentResolver, construct a path to an underlying Linux file descriptor and resolve a real file path by getting canonicalFile.

solru
  • 111
  • 1
  • 8
  • Does not work with samsung, it seems that they have restricted these calls ```java.lang.SecurityException: MediaProvider: User 11518 does not have read permission on file:///proc/8285/fd/25``` – TheD3t0 Jun 20 '23 at 17:02
-2

you can get filename by uri with simple way

Retrieving file information

fun get_filename_by_uri(uri : Uri) : String{
    contentResolver.query(uri, null, null, null, null).use { cursor ->
        cursor?.let {
            val nameIndex = it.getColumnIndex(OpenableColumns.DISPLAY_NAME)
            it.moveToFirst()
            return it.getString(nameIndex)
        }
    }
    return ""
}

and easy to read it by using

contentResolver.openInputStream(uri)
andermirik
  • 40
  • 4