Why using a FileProvider I can't open file from INTERNAL STORAGE with external apps?
Asked Answered
P

1

12

I created an app which can import file in its internal storage. In order to open a file with an external app (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

the result of printing the newFile.getAbsolutePath() is

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

This code opens the chooser in which I can click on "Photos" and then it opens the "Photos app" without show me the imagetest.jpg but the folder in which there are all the pictures. If i try with a pdf file, it doesn't open the pdf and it appears a toast with the message "no media".

What's wrong with my code?

Photosynthesis answered 19/1, 2017 at 8:31 Comment(1)
Comments are not for extended discussion; this conversation has been moved to chat.Bewray
P
18

Thanks to @greenapps that is an android expert, I found that the problem was 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);
Photosynthesis answered 20/1, 2017 at 13:18 Comment(3)
What is the difference between the 2 ??? It should be exactly the same.Maury
developer.android.com/guide/components/… If you want to set both the URI and MIME type, do not call setData() and setType() because they each nullify the value of the other. Always use setDataAndType() to set both URI and MIME type. I don't know why but you can read that in the official doc.Photosynthesis
I have similar problem, can you help me? #77258033Bouley

© 2022 - 2024 — McMap. All rights reserved.