2017-10-08 6 views
0

Problem 1: Ich habe eine pdf inden Austausch von Dokumenten über Intent/Fileprovider

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

gespeichert ich es bin das Senden normaler Art und Weise wie abgebildet mit:

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

Jetzt ist das Ergebnis

enter image description here

Das Problem ist Dateifreigabe ohne den Dateinamen.

Problem 2: Wenn ich versuche, die Datei-Provider die pdf zu verwenden ist mir nicht Sharing einen Fehler gibt:

Unable to share. Please try again

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

Manifest-Datei:

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

Was ist der bessere Weg?

Antwort

0

Ich würde persönlich # 2 bevorzugen. Lesen this Artikel auf, wie ShareCompat verwenden ShareIntents

In Bezug auf Ihr Problem mit Ansatz # 2 zu schaffen, ich glaube, du verpasst .setData oder .setDataAndUri:

Dies ist ein Beispiel-Schnipsel, die für den Austausch von Bildern funktioniert für mich über 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))); 
    } 
} 
Verwandte Themen