Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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?

like image 925
michoprogrammer Avatar asked Jan 19 '17 08:01

michoprogrammer


People also ask

How do you allow other apps to access files stored in this directory within internal storage?

To allow other apps to access files stored in this directory within internal storage, use a FileProvider with the FLAG_GRANT_READ_URI_PERMISSION attribute.

How can you access data from external storage in Android?

The data files saved over external storage devices are publicly accessible on shared external storage using USB mass storage transfer. Data files stored over external storage using a FileOutputStream object and can be read using a FileInputStream object.


1 Answers

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);
like image 126
michoprogrammer Avatar answered Oct 13 '22 08:10

michoprogrammer