相关疑难解决方法(0)

使用FileProvider从内部存储共享图像

我有一些文件存储在我的应用程序的内部存储器中,我想在外部应用程序中打开(例如,将图像发送到图库以供查看).我已经设置了FileProvider这样做.

来自AndroidManifest.xml:

<application>
  ...
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="${packageName}"
        android:exported="false"
        android:grantUriPermissions="true">

        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/files"/>

    </provider>
</application>
Run Code Online (Sandbox Code Playgroud)

来自res/xml/files.xml:

<paths>
    <files-path name="internal_logs" path="logs/"/>
    <files-path name="shared_files" path="shared/"/>
</paths>
Run Code Online (Sandbox Code Playgroud)

Fragment我尝试打开文件的位置:

File sharedFolderPath = new File(getActivity().getFilesDir(), "shared");
File toOpen = new File(sharedFolderPath.getAbsolutePath(), sharedFile.getFileName());

if (toOpen.exists()) {
    Log.w("File exists: " + toOpen.getAbsolutePath());

    Uri fileUri = FileProvider.getUriForFile(getActivity(), BuildConfig.PACKAGE_NAME, toOpen);

    Log.w("Attempting to attach file " + fileUri.getEncodedPath() + " with mime type: " + URLConnection.guessContentTypeFromName(fileUri.toString()));

    Intent viewFile = new Intent(Intent.ACTION_VIEW);

    viewFile.setData(fileUri);
    viewFile.setType(URLConnection.guessContentTypeFromName(fileUri.toString())); …
Run Code Online (Sandbox Code Playgroud)

android android-contentprovider android-fileprovider

7
推荐指数
1
解决办法
4817
查看次数