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?
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.
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.
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);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With