2017-10-08 7 views
0

Problème 1: Je pdf stocké dansPartage de documents via l'intention/Fileprovider

Environment.getExternalStorageDirectory() --> /storage/emulated/0/appname/downloads/sample.pdf 

Je te l'envoie en utilisant normalement comme indiqué:

File iconsStoragePath = Environment.getExternalStorageDirectory(); 
final String selpath = iconsStoragePath.getAbsolutePath() + "/appname/downloads/"; 

Intent intent = new Intent(Intent.ACTION_SEND); 
Uri selectedUri = Uri.parse(selpath + "/" + item.getFilename()); 
String fileExtension = MimeTypeMap.getFileExtensionFromUrl(selectedUri.toString()); 
String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(fileExtension); 
intent.setType(mimeType); 
intent.putExtra(Intent.EXTRA_STREAM, selectedUri); 
startActivity(Intent.createChooser(intent, "Share File")); 

Maintenant, le résultat est

enter image description here

Le problème est le partage de fichiers sans le nom de fichier.

Problème 2: Quand je suis en train d'utiliser le fournisseur de fichiers PDF ne partage pas me donne une erreur:

Unable to share. Please try again

Environment.getFilesDir() --> /data/data/com.myapp.name/files/myapp/downloads/sample.pdf 

fichier Manifest:

<application> 
<provider 
      android:name="android.support.v4.content.FileProvider" 
      android:authorities="${applicationId}.fileprovider" 
      android:exported="false" 
      android:grantUriPermissions="true"> 
      <meta-data 
       android:name="android.support.FILE_PROVIDER_PATHS" 
       android:resource="@xml/nnf_provider_paths" /> 
     </provider> 
</application> 

xml/nnf_provider_paths .xml

<?xml version="1.0" encoding="utf-8"?> 
<files-path 
    name="root" 
    path="myapp/downloads" /> 

Code:

File path = new File(getFilesDir(), "downloads"); 
File file = new File(documentsPath + "/" + filename); 
        Uri contentUri = FileProvider.getUriForFile(getApplicationContext(), AUTHORITY, file); 
Intent intent = new Intent(Intent.ACTION_SEND); 
intent.putExtra(Intent.EXTRA_STREAM, contentUri); 
intent.setType("application/pdf"); 
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
startActivity(intent); 

Quelle est la meilleure façon de le faire?

Répondre

0

Je préférerais personnellement # 2. Lire this article sur la façon d'utiliser ShareCompat pour créer ShareIntents

En ce qui concerne votre problème avec l'approche # 2, je suppose que vous êtes absent .setData ou .setDataAndUri:

C'est un extrait de l'échantillon qui fonctionne pour moi pour partager des images via FileProviders:

public static void shareImage(Activity context) { 

    Log.d(TAG, "initializing share image"); 
    File imgPath = new File(context.getCacheDir(), DEFAULT_TEMP_DIR_NAME); 
    File newFile = new File(imgPath, DEFAULT_TEMP_FILENAME); 
    String authority = context.getPackageName() + ".fileprovider"; 
    Log.d(TAG, "authority: " + authority); 
    Uri contentUri = FileProvider.getUriForFile(context, authority, newFile); 

    if (contentUri != null) { 
     Intent shareIntent = ShareCompat.IntentBuilder.from(context) 
       .setType("image/png") 
       .setStream(contentUri) 
       .setSubject(context.getString(R.string.share_subject)) 
       .setText(context.getString(R.string.share_message)) 
       .setEmailTo(new String[]{"[email protected]"}) 
       .getIntent(); 
     shareIntent.setData(contentUri); 
     shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION); 
     context.startActivity(Intent.createChooser(
       shareIntent, context.getString(R.string.share_chooser_title))); 
    } 
}