【问题标题】:Sharing documents via intent/Fileprovider通过意图/文件提供者共享文档
【发布时间】:2017-10-08 17:00:25
【问题描述】:

问题 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 未共享给我一个错误:

无法分享。请重试

Environment.getFilesDir() --> /data/data/com.myapp.name/files/myapp/downloads/sample.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>

xml/nnf_provider_paths.xml

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

哪种方法更好?

【问题讨论】:

    标签: android pdf whatsapp sharing android-fileprovider


    【解决方案1】:

    我个人更喜欢#2。阅读this文章,了解如何使用ShareCompat创建ShareIntents

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

    这是一个示例 sn-p,它适用于我通过 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[]{"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)));
        }
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-08-14
      • 1970-01-01
      • 2022-09-28
      • 1970-01-01
      相关资源
      最近更新 更多