0

私が試みているのは、Bluetooth経由でファイルを共有することです。私はACTION_SEND意図にファイル名を渡す以下の2つの方法を試しました。 shareアクティビティがポップアップしています。接続したBluetoothデバイスに触れると、Bluetooth share: File Unknown file not sentというトーストを受け取ります。どちらの方法も失敗します。Android NのBluetooth対応のファイルを共有する

public void pushFileOverOpp(String filename) { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.setPackage("com.android.bluetooth"); 
    intent.setType("audio/mp3"); 
    File f = new File(Environment.getExternalStorageDirectory(), "images"); 
    File sample = new File(f, "sample.mp3"); 
    Uri u = Uri.parse(sample.toString()); 
    intent.putExtra(Intent.EXTRA_STREAM, u); 
    mContext.startActivity(intent); 
} 

エラー、ログ -

OppService:URI:/storage/emulated/0/images/sample.mp3 OppService:ヒント:ヌル OppService:FILENAME:ヌル OppService:MIMETYPE:オーディオ/ mp3

File f = new File(mContext.getFilesDir(), "images"); 
File sample = new File(f, "sample.mp3"); 
Uri u = FileProvider.getUriForFile(mContext, 
      BuildConfig.APPLICATION_ID + ".provider", sample); 
intent.putExtra(Intent.EXTRA_STREAM, u); 

エラー、ログ -

OppService:URI:内容://com.example.com.test.provider/tester/images/sample.mp3 OppService:ヒント:ヌル OppService:FILENAME:ヌル

私がチェックしていますアンドロイドのソースコード、このエラーはfilenameがnullの場合に発生します。ログには、filenameがnullであることも示されます。しかし、私は正確な理由を理解することができませんでした。誰かがここで私を助けてください、私のコードで何が間違っていますか?

答えて

0

でファイルを共有するため、私は問題を理解しました。

xmlファイルに外部ストレージ(/ sdcard /)ディレクトリのxmlタグが間違っていました。

以下のように変更しました。

<root-path 
    name="root" 
    path="/" /> 

URI許可は上記のコード行を変更した後

mContext.grantUriPermission("com.android.bluetooth", u, 
      Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); 

を与えられていなかった、ファイルの共有が働いています!

public boolean pushFileOverOpp(String filename) { 
    Intent intent = new Intent(); 
    intent.setAction(Intent.ACTION_SEND); 
    intent.setType("*/*"); // supports all mime types 
    intent.setPackage("com.android.bluetooth"); //bluetooth package name, default opp 

    File folder = new File(Environment.getExternalStorageDirectory(), "images"); 
    File file = new File(folder, filename); 
    if (!file.exists()) { 
     Logger.e("No such file " + filename + " exists!"); 
     return false; 
    } 
    Uri u = FileProvider.getUriForFile(mContext, mContext.getPackageName() + ".provider", file); 
    intent.putExtra(Intent.EXTRA_STREAM, u); 

    mContext.grantUriPermission("com.android.bluetooth", u, 
      Intent.FLAG_GRANT_WRITE_URI_PERMISSION | Intent.FLAG_GRANT_READ_URI_PERMISSION); 

    Logger.d("Sharing file over bluetooth " + folder.toString()); 
    mContext.startActivity(intent); 
    return true; 
} 

おかげコード -

完全な作業。

0

このコードを参照してください、それは動作し、createChooserメソッドを使用してファイルを共有します。

  ArrayList<Uri> arrayList2 = new ArrayList<>(); 

      String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() + 
        "/NewCallLogs/audio.mp3"); 

      File files = new File(MEDIA_PATH); 
      Uri u = Uri.fromFile(files); 
      arrayList2.add(u); 

      Intent share = new Intent(android.content.Intent.ACTION_SEND_MULTIPLE); 
      share.setData(Uri.parse("mailto:")); 
      share.setType("audio/mpeg"); 
      share.putExtra(android.content.Intent.EXTRA_STREAM, arrayList2); 
      try { 
       startActivity(Intent.createChooser(share, "Share...")); 
       // getActivity().finish(); 
       Log.i("Finished sharing.", ""); 
      } catch (android.content.ActivityNotFoundException ex) { 
       Toast.makeText(getActivity(), "nothing shared.", Toast.LENGTH_SHORT).show(); 
      } 

は、いくつかの研究の後だけブルートゥース

ArrayList<Uri> arrayList2 = new ArrayList<>(); 

      String MEDIA_PATH = new String(Environment.getExternalStorageDirectory() + 
        "/NewCallLogs/audio.mp3"); 

      File files = new File(MEDIA_PATH); 
      Uri u = Uri.fromFile(files); 
      arrayList2.add(u); 

      Intent share = new Intent(android.content.Intent.ACTION_SEND); 
      share.setData(Uri.parse("mailto:")); 
      share.setType("audio/mpeg"); 
      share.setPackage("com.android.bluetooth"); 
      share.putExtra(android.content.Intent.EXTRA_STREAM, arrayList2); 
      startActivity(share); 
+0

私に疑問があれば私に通知してください。 –

+0

ありがとう、しかし、これはSDKのバージョン24以上で動作しません。だから私はAndroid Nをその質問に入れているのです。上記のコードの2つの問題 - java.lang.ClassCastException:java.util.ArrayListをandroid.os.Parcelableにキャストできません。 - arraylistのparcellable isnteadを渡してください。次の '原因:android.os.FileUriExposedException:' - Uri.fromFile'を使用できません。 :( – Rilwan

関連する問題