在Oreo中共享文件不起作用 [英] Sharing file in Oreo not working

查看:103
本文介绍了在Oreo中共享文件不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试在Oreo中共享音频文件.如果该文件位于设备的内部存储中,则可以正常运行,但是如果该文件位于外部存储中,则会崩溃,并给出此异常-android.os.FileUriExposedException.

I'm trying to share an audio file in Oreo. If the file is in internal storage of the device, it runs fine but if the file is present on the external storage it crashes giving this exception - android.os.FileUriExposedException.

如何解决此问题:

public void shareSong(SongInfoModel songInfoModel){

    Uri uri = Uri.parse("");
    File f = new File(songInfoModel.getData());
    if(Build.VERSION.SDK_INT<=Build.VERSION_CODES.N_MR1) {
          uri = Uri.parse("file://" + f.getAbsolutePath());
    }else {
         uri = FileProvider.getUriForFile(context, BuildConfig.APPLICATION_ID + ".provider", f);
    }
    Intent share = new Intent(Intent.ACTION_SEND);
    share.putExtra(Intent.EXTRA_STREAM, uri);
    share.setType("audio/*");
    share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    context.startActivity(Intent.createChooser(share, "Share audio File"));

}

清单:

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

推荐答案

如果您有一个使用Uri与其他应用共享文件的应用,则可能在API 24+上遇到了此错误.

If you have an app that shares files with other apps using a Uri, you may have encountered this error on API 24+.

第1步

将提供程序添加到清单文件

add provider to your manifest file

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

第2步

创建XML文件res/xml/provider_paths.xml

Create XML file res/xml/provider_paths.xml

 <?xml version="1.0" encoding="utf-8"?>
<paths xmlns:android="http://schemas.android.com/apk/res/android">
   <external-path name="external_files" path="."/>
</paths>

第3步

添加新代码

    File file ; // your code
    Intent install = new Intent(Intent.ACTION_VIEW);
    install.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
    // Old Approach
    install.setDataAndType(Uri.fromFile(file), mimeType);
    // End Old approach
    // New Approach
    Uri apkURI = FileProvider.getUriForFile(
            context,
            context.getApplicationContext()
                    .getPackageName() + ".provider", file);
    install.setDataAndType(apkURI, mimeType);
    install.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
    // End New Approach
    context.startActivity(install);

这篇关于在Oreo中共享文件不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆