繁体   English   中英

使用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上分享它。 当我选择一个用户共享时,什么也没有发生,我又回到了我的应用程序。

我的清单是:

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

请对此提供帮助,在此先感谢。

我尝试使用此代码,但无法正常工作。

那不是您使用ACTION_SEND 引用文档

输入:getType()是要发送的数据的MIME类型。 get * Extra可以具有EXTRA_TEXT或EXTRA_STREAM字段,其中包含要发送的数据。 如果使用EXTRA_TEXT,则MIME类型应为“ text / plain”; 否则,它应该是EXTRA_STREAM中数据的MIME类型。

因此,请替换:

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

(尽管直接将MIME类型放入setType()比使用getContentResolver().getType(contentUri)更为有效,因为希望您知道此文件是什么以及其MIME类型是什么)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM