以下代码用于在我的应用程序中共享音频,但我收到来自 8+ android 用户的反馈,当单击它以共享应用程序日期时
这就是我称之为我的分享课的地方。
lv.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, final int position, long l) {
AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this, AlertDialog.THEME_DEVICE_DEFAULT_LIGHT);
builder.setTitle("Olá, Marilene!");
builder.setItems(MainActivity.this.Nomes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int which) {
switch (which) {
case 0:
isStoragePermissionGranted();
sendWhatsAppAudio(position);
return;
case 1:
propaganda();
return;
default:
}
}
});
builder.show();
return true;
}
});
}
这是我在 whatsapp 中从我的应用程序中共享音频的代码
private void sendWhatsAppAudio(int songIndex){
InputStream inputStream;
FileOutputStream fileOutputStream;
try {
inputStream = getResources().openRawResource(arrayAdapter.getItem(songIndex).getResId());
fileOutputStream = new FileOutputStream(
new File(Environment.getExternalStorageDirectory(), arrayAdapter.getItem(songIndex) +".mp3"));
byte[] buffer = new byte[1024];
int length;
while ((length = inputStream.read(buffer)) > 0) {
fileOutputStream.write(buffer, 0, length);
}
inputStream.close();
fileOutputStream.close();
} catch (IOException e) {
e.printStackTrace();
}
Intent intent = new Intent(Intent.ACTION_SEND);
intent.putExtra(Intent.EXTRA_STREAM,
Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/" + arrayAdapter.getItem(songIndex) +".mp3"));
intent.setType("audio/*");
startActivity(Intent.createChooser(intent, "Quer compartilhar onde pika de foice?"));
}
android 8+ 手机上的 firebase 控制台中显示的错误日志
android.os.FileUriExposedException:
at android.os.StrictMode.onFileUriExposed (StrictMode.java:2235)
at android.net.Uri.checkFileUriExposed (Uri.java:2348)
at android.content.ClipData.prepareToLeaveProcess (ClipData.java:941)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9742)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9748)
at android.content.Intent.prepareToLeaveProcess (Intent.java:9727)
at android.app.Instrumentation.execStartActivity (Instrumentation.java:1611)
at android.app.Activity.startActivityForResult (Activity.java:4513)
at android.support.v4.app.BaseFragmentActivityApi16.startActivityForResult (BaseFragmentActivityApi16.java:54)
at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:67)
at android.app.Activity.startActivityForResult (Activity.java:4471)
at android.support.v4.app.FragmentActivity.startActivityForResult (FragmentActivity.java:732)
at android.app.Activity.startActivity (Activity.java:4832)
at android.app.Activity.startActivity (Activity.java:4800)
at meme.welyson.brasil.brasilmemes.MainActivity.sendWhatsAppAudio (MainActivity.java:1062)
at meme.welyson.brasil.brasilmemes.MainActivity.access$000 (MainActivity.java:38)
at meme.welyson.brasil.brasilmemes.MainActivity$2$1.onClick (MainActivity.java:995)
at com.android.internal.app.AlertController$AlertParams$3.onItemClick (AlertController.java:1176)
at android.widget.AdapterView.performItemClick (AdapterView.java:318)
at android.widget.AbsListView.performItemClick (AbsListView.java:1188)
at android.widget.AbsListView$PerformClick.run (AbsListView.java:3157)
at android.widget.AbsListView$3.run (AbsListView.java:4110)
at android.os.Handler.handleCallback (Handler.java:789)
at android.os.Handler.dispatchMessage (Handler.java:98)
at android.os.Looper.loop (Looper.java:169)
at android.app.ActivityThread.main (ActivityThread.java:6595)
at java.lang.reflect.Method.invoke (Native Method)
at com.android.internal.os.Zygote$MethodAndArgsCaller.run (Zygote.java:240)
at com.android.internal.os.ZygoteInit.main (ZygoteInit.java:767)