3

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(); } } 

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

14 Comments

But I want not only read file, I also want to edit file and save.
@PersonalJesus: There is openOutputStream(), also on ContentResolver. IOW, the same things that you would do with an absolute path and FileInputStream/FileOutputStream are things that you can do with a content Uri and openInputStream()/openOutputStream().
@CommonsWare LibVLC Player Library for Android doesn't support Content Uri, it needs absolute path, because it's a native library (c++, c) and it doesn't know nothing about that stuff!
@user25: Then only work with files. Use a file chooser library instead of something that gives you a content Uri.
@CommonsWare a file chooser? We're talking about ACTION_VIEW. I have an app - Video Player. I want to open a video file in this app from some other app - Explorer app, e.g. play.google.com/store/apps/…
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.