Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get file URI when opening file with intent.action.VIEW?

User click on .txt file in file explorer and get dialog "Open with..." and there is listed my app. When I try to open file I want to get its absolute path. All I get is content:// URI

I/ActivityManager: START u0 {act=android.intent.action.VIEW dat=content://com.android.providers.downloads.documents/document/1031 typ=text/plain

If I want to retrieve path to my file I need to get file:// URI. How can I get only file:// URI instead of content:// ?

Here is AndroidManifest

        <intent-filter >
            <action android:name="android.intent.action.VIEW" />
            <category android:name="android.intent.category.DEFAULT" />    
            <data android:mimeType="text/*"/>
        </intent-filter>

Here is how I try to handle file:

Intent i = getIntent();
String action = i.getAction();
String type = i.getType();

if (Intent.ACTION_VIEW.equals(action) && type != null) {
    Uri uri = i.getData();
    if (uri != null) {
        Toast.makeText(this, uri.toString(), Toast.LENGTH_SHORT).show();
    }                 
}
like image 841
Personal Jesus Avatar asked Jan 19 '26 10:01

Personal Jesus


1 Answers

Here is AndroidManifest

Your <intent-filter> is saying that you can handle text/* content no matter where it comes from.

When I try to open file I want to get its absolute path

Then you need to add <data android:scheme="file"> to your <intent-filter>, to declare that you only work with file schemes. Your app will no longer appear as an option in apps that do not use file Uri values, such as the "file manager" that you are using. And, since file Uri values are effectively banned on Android 7.0+, your app will be less useful over time. By 2020 or so, your app will be useless.

Or, you could get rid of the "want to get its absolute path" requirement. If you want to retrieve the text content from a file or a content Uri, you can use openInputStream() on a ContentResolver, for example.

like image 83
CommonsWare Avatar answered Jan 21 '26 01:01

CommonsWare



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!