Why use FileProvider? Can't I open a file from INTERNAL STORAGE using external applications?

I created an application that can import a file in internal storage. To open a file with an external application (for example, PF viewer or Photos), I tried to follow these guides: the official guide , topic1 , topic2 , topic3 ​​and topic4 , but without success.

Here is my code:

in my manifest

<provider android:name="android.support.v4.content.FileProvider" android:authorities="com.myapp.chatcher" android:exported="false" android:grantUriPermissions="true"> <meta-data android:name="android.support.FILE_PROVIDER_PATHS" android:resource="@xml/file_paths" /> </provider> 

my package value: package="com.myapp.catcher"

my file_paths.xml

 <paths xmlns:android="http://schemas.android.com/apk/res/android"> <files-path name="projection" path="." /> </paths> 

my code

 String fileName = path.substring(path.lastIndexOf("/") + 1); String shelf = path.substring(path.lastIndexOf("PRIVATE") + 8, path.lastIndexOf("/")); File filePath = new File(mContext.getFilesDir(), "PRIVATE".concat("/").concat(shelf).concat("/")); File newFile = new File(filePath, fileName); Uri contentUri = FileProvider.getUriForFile(mContext, "com.myapp.chatcher", newFile); Intent myIntent = new Intent(); myIntent.setAction(Intent.ACTION_VIEW); myIntent.setData(contentUri); myIntent.setType(mimeType); myIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); mContext.startActivity(myIntent); 

I created a hierarchy like this:

 PRIVATE -> shelf1 -> my files -> shelf2 -> my files -> shelfN -> my files 

for example: data/user/0/com.myapp.chatcher/files/PRIVATE/testshelf/Screenshot_2017-01-04-09-45-13.png

print result newFile.getAbsolutePath () -

 /data/user/0/com.myapp.chatcher/files/PRIVATE/bogl/imagetest.jpg 

This code opens a choice in which I can click "Photos" and then it will open the "Application for photos", not showing me imagetest.jpg, but in a folder that has all the images. If I try to use a pdf file, it does not open the PDF file and it looks like a toast with the message "no media".

What is wrong with my code?

+5
source share
1 answer

Thanks to @greenapps, who is an Android specialist, I found that the problem is not in the provider, but in the intent.

Instead of this:

 Intent myIntent = new Intent(); myIntent.setAction(Intent.ACTION_VIEW); myIntent.setData(contentUri); myIntent.setType(mimeType); myIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); mContext.startActivity(myIntent); 

I need to do this:

 Intent myIntent = new Intent(Intent.ACTION_VIEW); myIntent.setDataAndType(contentUri, mimeType); myIntent.setFlags(FLAG_GRANT_READ_URI_PERMISSION | FLAG_GRANT_WRITE_URI_PERMISSION); mContext.startActivity(myIntent); 
+5
source

Source: https://habr.com/ru/post/1263043/


All Articles