使用 FileProvider 时获取 URI 而不是 File

Getting URI instead of File when using FileProvider

我正在创建一个应用程序,让用户可以在其中共享外部存储中存在的音频文件。

我尝试使用此代码,但它不起作用。

            //This is getting the absolute path of the parent folder where the
            //file is situated 
            File mFolder= new File(m_sdcard+m_confi);

            // This take us to folder where the file is situated
            File mAbFolder = new File(mFolder, "temp");

            // Taking the files in an array
            File[] mAllFiles= mAbFolder.listFiles();

            //Getting the URI 
            Uri contentUri = FileProvider.getUriForFile(mContext, "com.androidpackagename.fileprovider", mImageFiles[1]);

            Intent intent = new Intent(Intent.ACTION_SEND);
            intent.setDataAndType(contentUri, mContext.getContentResolver().getType(contentUri));
            intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
            mContext.startActivity(intent);

现在当我 运行 这段代码时,它会显示共享菜单。但是当我点击 Gmail 等任何项目时,它会将 uri 放入 "TO" 字段。

我也尝试在 Whatsapp 上分享它。当我 select 一个用户分享这个时,没有任何反应,我回到我的应用程序。

我的清单是:

<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.androidpackagename">

<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:supportsRtl="true"
    android:theme="@style/AppTheme">
    <activity android:name=".MainActivity">
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
    </activity>
    <provider
        android:name="android.support.v4.content.FileProvider"
        android:authorities="com.androidpackagename.fileprovider"
        android:grantUriPermissions="true"
        android:exported="false">
        <meta-data
            android:name="android.support.FILE_PROVIDER_PATHS"
            android:resource="@xml/filepath" />
    </provider>
</application>

</manifest>

我的文件路径 xml 有

<external-path
    name="external"
    path="Android/data/com.androidpackagename/temp/" />
</paths>

请帮忙解决这个问题,先谢谢了。

I try to use this code but it is not working.

这不是您使用 ACTION_SEND 的方式。引用 the documentation:

Input: getType() is the MIME type of the data being sent. get*Extra can have either a EXTRA_TEXT or EXTRA_STREAM field, containing the data to be sent. If using EXTRA_TEXT, the MIME type should be "text/plain"; otherwise it should be the MIME type of the data in EXTRA_STREAM.

所以,替换:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setDataAndType(contentUri, mContext.getContentResolver().getType(contentUri));

与:

Intent intent = new Intent(Intent.ACTION_SEND);
intent.setType(mContext.getContentResolver().getType(contentUri));
intent.putExtra(Intent.EXTRA_STREAM, contentUri);

(尽管直接在 setType() 中输入 MIME 类型比使用 getContentResolver().getType(contentUri) 效率更高,因为希望您知道这个文件是什么以及它的内容MIME 类型是)