How to get data from the file which is opened from other applications? For example, I open a file from default file manager and open with
my application, and then how to get the data from the file that I open?
I am guessing that your app has an activity with an ACTION_VIEW
<intent-filter>
.
If so, you can call getData()
on the Intent
that was used to start your activity to get the Uri
pointing to the content that the user wishes to view. You can call getContentResolver()
on your Activity
or other Context
to get a ContentResolver
object. Call openInputStream()
on the ContentResolver
to get an InputStream
on the content. Ideally, do that work on a background thread.
This sample app and this sample app (both from this book show how to use a ContentResolver
to get the InputStream
for the content identified by a Uri
. In my case, I am using ACTION_OPEN_DOCUMENT
rather than responding to ACTION_VIEW
, but the rest is the same.
Note that your viewing activity has two sources of Intent
objects:
When the activity is started initially, and so an instance is created, the Intent
used for that is available via getIntent()
If the user opens another piece of content, and an existing instance of your activity is reused, that activity will be called with onNewIntent()
, and you are passed the Intent
that was used for this particular request