I have a content URI in the form of "content://media/external/images/media/175". I am trying to find the absolute path of the image in question in order to send the file in a request. Is there any way to accomplish this?
I am trying to find the absolute path of the image
A Uri is not a file. You cannot convert arbitrary Uri values to some filesystem path that you can access. Those Uri values might point to:
A file on removable storage
A file on internal storage of some other app
A file that has not yet been downloaded, but will be downloaded on the fly by the ContentProvider
A file that has been encrypted and will be decrypted on the fly by the ContentProvider
A BLOB in a database
Content that will be generated dynamically, the way this Web page is
And so on
in order to send the file in a request
Use a ContentResolver and openInputStream() to get an InputStream to use in your request. If whatever API you are using does not support an InputStream, either:
Switch to a better API, or
Use that InputStream to make a local file copy on your own internal storage, then use that file
Related
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
While sharing search images from chrome browser, app receives intent with a Content URI like this:
content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it.
I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).
Tried looking everywhere, without much help.
Can someone suggest how to work with these Content URIs?
If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?
You don't, as there does not have to be a file at all behind the Uri, let alone one that you can access. That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
All other apps like Facebook, Google+ are doing it
No, they are not. They are using ContentResolver and:
openInputStream() to read in the bytes associated with the content
getType() to get the MIME type associated with the content
query() and the OpenableColumns to get the size and display name associated with the content
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
While sharing search images from chrome browser, app receives intent with a Content URI like this:
content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it.
I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).
Tried looking everywhere, without much help.
Can someone suggest how to work with these Content URIs?
If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?
You don't, as there does not have to be a file at all behind the Uri, let alone one that you can access. That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
All other apps like Facebook, Google+ are doing it
No, they are not. They are using ContentResolver and:
openInputStream() to read in the bytes associated with the content
getType() to get the MIME type associated with the content
query() and the OpenableColumns to get the size and display name associated with the content
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
While sharing search images from chrome browser, app receives intent with a Content URI like this:
content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it.
I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).
Tried looking everywhere, without much help.
Can someone suggest how to work with these Content URIs?
If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?
You don't, as there does not have to be a file at all behind the Uri, let alone one that you can access. That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
All other apps like Facebook, Google+ are doing it
No, they are not. They are using ContentResolver and:
openInputStream() to read in the bytes associated with the content
getType() to get the MIME type associated with the content
query() and the OpenableColumns to get the size and display name associated with the content
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
While sharing search images from chrome browser, app receives intent with a Content URI like this:
content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it.
I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).
Tried looking everywhere, without much help.
Can someone suggest how to work with these Content URIs?
If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?
You don't, as there does not have to be a file at all behind the Uri, let alone one that you can access. That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
All other apps like Facebook, Google+ are doing it
No, they are not. They are using ContentResolver and:
openInputStream() to read in the bytes associated with the content
getType() to get the MIME type associated with the content
query() and the OpenableColumns to get the size and display name associated with the content
I am trying to share images from other applications to my application using implicit intent ACTION_SEND.
While sharing search images from chrome browser, app receives intent with a Content URI like this:
content://com.android.chrome.FileProvider/images/screenshot/1457448067808912906311.jpg
How can I fetch file path from this type of Content URI? All other apps like Facebook, Google+ are doing it.
I am using FileChooser for getting file path of other types of Content URIs (eg. from Gallery).
Tried looking everywhere, without much help.
Can someone suggest how to work with these Content URIs?
If you absolutely need a local copy of the file, you are going to need to open the InputStream copy the contents to a local file that you know the path to and then go from there. Sidenote: Guava's ByteStreams#copy is an easy way to accomplish this.
Of course this file is no longer backed by the original Uri source, so I don't think this is what you want. Instead, you should work with the Uri's intended API. Take a look at the Storage Access Framework
Edit
Here is how you can get an InputStream from your Uri
InputStream inputStream = getContentResolver().openInputStream(uri);
How can I fetch file path from this type of Content URI?
You don't, as there does not have to be a file at all behind the Uri, let alone one that you can access. That Uri might point to:
A local file on external storage
A local file on internal storage for the other app
A local file on removable storage
A local file that is encrypted and needs to be decrypted on the fly
A stream of bytes held in a BLOB column in a database
A piece of content that needs to be downloaded by the other app first
...and so on
All other apps like Facebook, Google+ are doing it
No, they are not. They are using ContentResolver and:
openInputStream() to read in the bytes associated with the content
getType() to get the MIME type associated with the content
query() and the OpenableColumns to get the size and display name associated with the content