Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert a content Uri into a File

Tags:

file

android

uri

I know there are a ton of questions about this exact topic, but after spending two days reading and trying them, none seamed to fix my problem.

This is my code:

I launch the ACTION_GET_CONTENT in my onCreate()

Intent selectIntent = new Intent(Intent.ACTION_GET_CONTENT);
        selectIntent.setType("audio/*");
        startActivityForResult(selectIntent, AUDIO_REQUEST_CODE);

retrieve the Uri in onActivityResult()

@Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == AUDIO_REQUEST_CODE && resultCode == Activity.RESULT_OK) {
            if ((data != null) && (data.getData() != null)) {
                audio = data.getData();
            }
        }
    }

pass the Uri to another activity and retrieve it

Intent debugIntent = new Intent(this, Debug.class);
            Bundle bundle = new Bundle();
            bundle.putString("audio", audio.toString());
            debugIntent.putExtras(bundle);
            startActivity(debugIntent);

Intent intent = this.getIntent();
        Bundle bundle = intent.getExtras();
        audio = Uri.parse((String) bundle.get("audio"));

The I have implemented this method based on another SO answer. To get the actual Path of the Uri

public static String getRealPathFromUri(Activity activity, Uri contentUri) {
        String[] proj = { MediaStore.Audio.Media.DATA };
        Cursor cursor = activity.managedQuery(contentUri, proj, null, null, null);
        int column_index = cursor.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA);
        cursor.moveToFirst();
        return cursor.getString(column_index);
    }

and in the Debug activity's onCreate() I try to generate the file:

File audioFile = new File(getRealPathFromUri(this, audio));

This is how the error looks like:

Caused by: java.lang.NullPointerException at java.io.File.(File.java:262) at com.dancam.lietome.Debug.onCreate(Debug.java:35)

When I run the app I get a NPE on this last line. The audio Uri, isn't NULL though so I don't understand from what it is caused.

I'd really appreciate if you helped me out.

This is the library I'm trying to work with.

Note: I know exactly what NPE is, but even debugging I couldn't figure out from what it is caused in this specific case.

like image 838
Daniele Avatar asked Aug 12 '17 09:08

Daniele


1 Answers

pass the Uri to another activity and retrieve it

Your other activity does not necessarily have rights to work with the content identified by the Uri. Add FLAG_GRANT_READ_URI_PERMISSION to the Intent used to start that activity, and pass the Uri via the "data" facet of the Intent (setData()), not an extra.

To get the actual Path of the Uri

First, there is no requirement that the Uri that you get back be from the MediaStore.

Second, managedQuery() has been deprecated for six years.

Third, there is no requirement that the path that MediaStore has be one that you can use. For example, the audio file might be on removable storage, and while MediaStore can access it, you cannot.

How to convert a content Uri into a File

On a background thread:

  • Get a ContentResolver by calling getContentResolver() on a Context
  • Call openInputStream() on the ContentResolver, passing in the Uri that you obtained from ACTION_GET_CONTENT, to get an InputStream on the content identified by the Uri
  • Create a FileOutputStream on some File, where you want the content to be stored
  • Use Java I/O to copy the content from the InputStream to the FileOutputStream, closing both streams when you are done
like image 196
CommonsWare Avatar answered Sep 28 '22 05:09

CommonsWare