0

我在试图通过蓝牙共享文件。我已经尝试了下面两种方法将文件名传递给ACTION_SEND打算。 share活动正在弹出,当我触摸连接的蓝牙设备时,我得到一个敬酒说Bluetooth share: File Unknown file not sent。这两种方法都失败了。通过蓝牙opp在Android上共享文件N

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:文件名:空

我已签android源代码,当文件名为空时出现这个错误。日志还说文件名是空的。但我无法弄清楚确切的原因。有人可以请帮我在这里,我的代码有什么问题。

回答

0

一些研究之后,我明白了问题。有两个问题 -

用于外部存储(/ sdcard /)目录的xml标记在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放在问题中。上述代码的两个问题:'java.lang.ClassCastException:java.util.ArrayList不能转换为android.os.Parcelable' - 通过parcellable不是arraylist。下一个'导致:android.os.FileUriExposedException:' - 我们不能使用'Uri.fromFile'。 :( – Rilwan