11

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?

Community
  • 1
  • 1
michoprogrammer
  • 1,159
  • 2
  • 18
  • 45
  • Comments are not for extended discussion; this conversation has been [moved to chat](http://chat.stackoverflow.com/rooms/133634/discussion-on-question-by-machoprogrammer-why-using-a-fileprovider-i-cant-open). – Bhargav Rao Jan 20 '17 at 11:45

1 Answers1

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);
deceze
  • 510,633
  • 85
  • 743
  • 889
michoprogrammer
  • 1,159
  • 2
  • 18
  • 45
  • 1
    What is the difference between the 2 ??? It should be exactly the same. – Hrk Nov 20 '17 at 13:54
  • 9
    https://developer.android.com/guide/components/intents-filters.html#Types _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. – michoprogrammer Nov 20 '17 at 16:02