5

J'ai créé une application qui peut importer un fichier dans son stockage interne. Afin d'ouvrir un fichier avec une application externe (par exemple PF viewer ou Photos) j'ai essayé de suivre ces guides: the official guide, topic1, topic2, topic3 et topic4 mais sans succès.Pourquoi utiliser un FileProvider Je ne peux pas ouvrir le fichier de INTERNAL STORAGE avec des applications externes?

Voici mon code:

dans mon manifeste

<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> 

ma valeur du package: package="com.myapp.catcher"

mon file_paths.xml

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

mon 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); 

J'ai créé une hiérarchie comme ceci:

PRIVATE -> shelf1 -> my files 

     -> shelf2 -> my files 

     -> shelfN -> my files 

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

est

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

le résultat de l'impression du newFile.getAbsolutePath() Ce code ouvre le sélecteur dans que je peux cliquer sur "Photos" puis il ouvre l'application "Photos" sans me montrer l'imagetest.jpg mais le dossier dans lequel il y a toutes les photos. Si j'essaie avec un fichier pdf, il n'ouvre pas le pdf et il apparaît un toast avec le message "aucun média".

Quel est le problème avec mon code?

+0

commentaires ne sont pas pour la discussion élargie; cette conversation a été [déplacée pour discuter] (http://chat.stackoverflow.com/rooms/133634/discussion-on-question-by-machoprogrammer-why-using-a-fileprovider-i-cant-open). –

Répondre

4

Grâce à @greenapps qui est un expert android, j'ai trouvé que le problème n'était pas dans le fournisseur mais dans l'intention.

Au lieu de cela:

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); 

que je dois faire ceci:

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); 
+0

Mon problème n'est pas résolu avec ça. – SimpleCoder

+0

Quelle est la différence entre le 2 ??? Ça devrait être exactement pareil. – Hrk

+0

https://developer.android.com/guide/components/intents-filters.html#Types _Si vous voulez définir à la fois le type URI et MIME, n'appelez pas setData() et setType() car ils annulent chacun la valeur de l'autre. Utilisez toujours setDataAndType() pour définir le type URI et MIME._ Je ne sais pas pourquoi mais vous pouvez le lire dans le document officiel. – michoprogrammer