我创建了一个当用户按下按钮时将音频文件共享到whatsapp的应用程序。一切正常,除了我不能再直接从Whatsapp共享音频文件的事实。下面说明了此行为。 framework I'm working with并非如此。当我单击它时,WhatsApp重定向我Audio can be shared by clicking on 'Audio' which should redirect the user to a file like activity where they can select their audio.。在此屏幕上,我可以录制一些音频并共享该音频,但是我无法浏览我的文件。
我怀疑以下定义的意图导致了此问题,因为其他用户报告了相同的问题,而其他尚未使用此功能的用户则直接通过WhatsApp共享音频时没有问题。
public boolean onLongClick(View view) {
String permission = Manifest.permission.WRITE_EXTERNAL_STORAGE;
if (ContextCompat.checkSelfPermission(mContext, permission) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((MainActivity) mContext, new String[]{permission}, 2);
return false;
} else {
// Permission has been granted
try {
String file = "soundboard/" + person + "/" + getItem(getAdapterPosition());
file = copyFiletoExternalStorage(file, getItem(getAdapterPosition()));
Intent sendIntent = new Intent();
sendIntent.setAction(Intent.ACTION_SEND);
sendIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse(file));
sendIntent.setType("audio/*");
mContext.startActivity(Intent.createChooser(sendIntent, "Send your audio file"));
return true;
} catch (Exception e) {
Toast.makeText(mContext.getApplicationContext(), "Sharing has failed.", Toast.LENGTH_SHORT).show();
return false;
}
}
}
和一个帮助程序类,用于在发送意图之前将文件复制到sd存储。该代码已从to the audio recorder.中获取并修改。
private String copyFiletoExternalStorage(String file, String resourceName){
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) {
String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + mContext.getPackageName() + "/" + resourceName;
try {
InputStream in = mContext.getAssets().open(file);
FileOutputStream out;
out = new FileOutputStream(pathSDCard);
byte[] buff = new byte[1024];
int read;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
out.flush();
} finally {
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return pathSDCard;
}
else
return "";
}
我已经卸载了自己的应用程序,重新启动了手机,并重新安装了WhatsApp,但无济于事。我该如何解决WhatsApp以及如何防止这种情况再次发生?