You are here

Using GET_CONTENT does not return a file URI anymore

The default behavior changed, in accordance with the Android documentation. GET_CONTENT will return a "content:" URI that you can query for the file content. You have to modify your application accordingly.

If you want to obtain a "file:" URI, use the PICK_FILE intent:

More information

According to the Android documentation, the previous behavior of OI File Manager as well as other file managers that return the "file:" URI, is wrong:
"Output: The URI of the item that was picked. This must be a content: URI so that any receiver can access it."

This is also the behavior that Google's GMail application expects when you attach a file. The idea is that you can directly access the returned "content:" URI by opening a stream on that URI.

If you query the content provider on the uri returned, you can:
* use getContentResolver().query(uri, null, null, null, null) to access the columns "Images.Media.DATA" and "Images.Media.MIME_TYPE" to obtain file name and MIME type,
* call getContentResolver().getType(uri) directly to obtain the MIME type,
* use getContentResolver().getFileDescriptor(uri, "r") to access the file content directly.

How to be backward / forward compatible:

Use the following code in onActivityResult():

// obtain the filename
Uri uri = intent.getData();
String filePath = null;
if (uri != null) {
	if (uri.startsWith("file:")) {
		filePath = uri.getPath();
	} else { // uri.startsWith("content:")
		Cursor c = getContentResolver().query(uri, null, null, null, null);
		if (c != null && c.moveToFirst()) {
			int id = c.getColumnIndex(Images.Media.DATA);
			if (id != -1) {
				filePath = c.getString(id);
if (filePath != null) {
	// filePath contains path to file.