簡體   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