我正在創建一個應用程序,讓我們讓用戶共享在外部存儲上呈現的音頻文件。使用FileProvider時獲取URI而不是文件
我嘗試使用此代碼,但它不起作用。
//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>
在此請幫幫忙,在此先感謝。
感謝您的答案,它的工作但我仍然有一些問題。問題是我的音頻文件在分享時正在修剪。音頻文件非常小,並且是錄音,但是在每次發送時,最後一個或兩個單詞都會被裁剪掉。 – Raddix
@Raddix:'FileProvider'不會裁剪它們。也許文件本身比你想象的要短。 – CommonsWare