1

I want to get a absolute path from URI path using createChooser. What I have to do? If you know a solution that solve this problem, pls, inform me. by the way, URI path display follow path:

content://com.android.providers.media.documents/document/audio%3A38

Normal path display follow path:

/document/audio:38

Here is My Code:

 public void set_music_path(View view) {
    Intent intent = new Intent();
    intent.setType("audio/*");
    intent.setAction(Intent.ACTION_GET_CONTENT);
    startActivityForResult(Intent.createChooser(intent, "음악을 선택해주세요."), 1);
}

protected void onActivityResult(int req, int res, Intent data){
    super.onActivityResult(req, res, data);

    Uri uri = data.getData();
    Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show();
    Toast.makeText(this, uri.getPath().toString(), Toast.LENGTH_SHORT).show();
}

I'm a Korean student, so there are some grammar errors. if there are grammar errors, overlook my faults

박광유
  • 11
  • 1

1 Answers1

0

First, you need to realize that createChooser() has nothing to do with the format of the Uri that you get back from your startActivityForResult() call. Any ACTION_GET_CONTENT activity can hand back whatever valid Uri it wants to. You could get that sort of Uri from your startActivityForResult() call, even if you left off the createChooser() part.

Second, you need to realize that a Uri is not a file. ACTION_GET_CONTENT activities can return a Uri pointing to anything that the activities want.

If the scheme of the Uri happens to be file, then getPath() will be a filesystem path, pointing to a file that (presumably) you could access.

If the scheme is content — as is the case here — then the Uri can point to anything:

  • A file on external storage that you could access
  • A file on removable storage that you cannot access
  • A file on internal storage of another app, that you cannot access
  • An asset of another app, which is not a file
  • A value from a BLOB column of a database, which is not a file
  • A value that is generated on the fly, the way that this Web page is, which is not a file
  • Content that will be streamed from the Internet, which is not a file on the device
  • And so on

Use ContentResolver and openInputStream() (and perhaps openOutputStream()) to work with the content identified by the Uri.

If you happen to be working with some third-party library that requires a File, switch to a better library that can work with an InputStream.

If you cannot find a better library, you will have to use the InputStream to copy the bytes of the content to some file that you control (e.g., in getCacheDir()), then use that file.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491