2017-02-11 1 views
0

私は、外部ストレージにプレゼンテーションされているオーディオファイルを共有できるようにするアプリケーションを作成しています。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> 

を持っている、これを事前に感謝を助けてください。

答えて

0

このコードを使用しようとしましたが、動作しません。

ACTION_SENDを使用する方法ではありません。引用符:the documentation

入力: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); 

(あなただけsetType()に直接MIMEタイプに置くのではなく、使用することがはるかに効率的であるにもかかわらずgetContentResolver().getType(contentUri)、このファイルが何で、そのMIMEタイプが何であるかがわかっているので、

+0

この回答に感謝し、私はまだいくつかの問題があります。問題は、私のオーディオファイルが共有するときに切り取られてしまうことです。オーディオファイルは非常に小さく、オーディオレコーディングですが、各送信で最後の1〜2語が切り取られています。 – Raddix

+0

@Raddix: 'FileProvider'はそれらを切り取るつもりはありません。おそらく、ファイルそのものはあなたが思っているよりも短くなっています。 – CommonsWare

関連する問題