Android 通过intent/Fileprovider共享文档

Android 通过intent/Fileprovider共享文档,android,pdf,whatsapp,sharing,android-fileprovider,Android,Pdf,Whatsapp,Sharing,Android Fileprovider,问题1:我有一个pdf存储在 Environment.getExternalStorageDirectory() --> /storage/emulated/0/appname/downloads/sample.pdf 我使用正常方式发送,如图所示: File iconsStoragePath = Environment.getExternalStorageDirectory(); final String selpath = iconsStoragePath.getAbsolutePa

问题1:我有一个pdf存储在

Environment.getExternalStorageDirectory() --> /storage/emulated/0/appname/downloads/sample.pdf
我使用正常方式发送,如图所示:

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"));
现在的结果是

问题是文件共享时没有文件名

问题2:当我尝试使用文件提供程序时,pdf未共享给我一个错误:

无法共享。请再试一次

清单文件:

<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>
哪种方法更好?

我个人更喜欢2。阅读有关如何使用
ShareCompat
创建
ShareIntents

关于方法2的问题,我猜您缺少
.setData
.setDataAndUri

这是一个示例片段,适用于我通过
文件提供程序
共享图像:

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[]{"example@example.com"})
                .getIntent();
        shareIntent.setData(contentUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(
                shareIntent, context.getString(R.string.share_chooser_title)));
    }
}
<?xml version="1.0" encoding="utf-8"?>
<files-path
    name="root"
    path="myapp/downloads" />
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);
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[]{"example@example.com"})
                .getIntent();
        shareIntent.setData(contentUri);
        shareIntent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        context.startActivity(Intent.createChooser(
                shareIntent, context.getString(R.string.share_chooser_title)));
    }
}