私は、外部ストレージにプレゼンテーションされているオーディオファイルを共有できるようにするアプリケーションを作成しています。FileProviderを使用するときにFileの代わりに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などのアイテムをクリックすると、「TO」フィールドにURIが表示されます。
また、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>
を持っている、これを事前に感謝を助けてください。
この回答に感謝し、私はまだいくつかの問題があります。問題は、私のオーディオファイルが共有するときに切り取られてしまうことです。オーディオファイルは非常に小さく、オーディオレコーディングですが、各送信で最後の1〜2語が切り取られています。 – Raddix
@Raddix: 'FileProvider'はそれらを切り取るつもりはありません。おそらく、ファイルそのものはあなたが思っているよりも短くなっています。 – CommonsWare