无法使用FileProvider.getUriForFile()

时间:2018-06-05 15:13:09

标签: android android-fileprovider

我知道我已多次提出相关问题,但我认为我已经找到了一个紧密的解决方案。当我使用Uri.fromFile()时,它可以工作。但是,当我使用FileProvider.getUriForFile()时,它会打开选​​择页面(我想用它发送文件,如蓝牙,wifi-direct等),但当我的目标设备时,它失败了(显示一些消息后)或根本没有显示任何东西)。在蓝牙的情况下,在选择目标设备后,没有任何反应。在wifi-direct的情况下,在选择目标设备后,当该设备接受请求时,它会显示消息“无法共享内容。文件不包含数据或未保存在设备上”(请参见屏幕截图)...可以有谁请告诉我我做错了什么?请原谅我提出相关问题 (我正在使用lolipop)

for wifi direct-Toast for wifi direct-After opening failed notification

清单:

<?xml version="1.0" encoding="utf-8"?>

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
    android:allowBackup="true"
    android:icon="@mipmap/ic_launcher"
    android:label="@string/app_name"
    android:roundIcon="@mipmap/ic_launcher_round"
    android:supportsRtl="true"
    android:theme="@style/AppTheme">

    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.example.ragib.apkbackup.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/shareable_paths" />
    </provider>

    <activity
        android:name=".MainActivity"
        android:configChanges="orientation|screenSize|keyboardHidden|screenLayout">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
</application>

路径xml:

<?xml version="1.0" encoding="utf-8"?>
<paths>
<root-path name="system_app_path" path="/system/app/"/>
<root-path name="device_app_path" path="/data/app"/>
<root-path name="sd_card_app_path" path="/mnt/asec/"/>
</paths>

打开选择的代码:

menu.getItem(1).setOnMenuItemClickListener(new MenuItem.OnMenuItemClickListener() {
                    @Override
                    public boolean onMenuItemClick(MenuItem item) {
                        Uri uri=FileProvider.getUriForFile(MainActivity.this,
                                "com.example.ragib.apkbackup.fileprovider",new File(applicationInfo.sourceDir));
                        Intent intent=new Intent(Intent.ACTION_SEND);
                        intent.setType("application/vnd.android.package-archive");
                        intent.putExtra(Intent.EXTRA_STREAM,uri);
                        startActivity(Intent.createChooser(intent,"Share using"));
                        return true;
                    }
                });

0 个答案:

没有答案
相关问题