[英]mp3 share via whatsapp (this file is not supported) error
when i click created share button i took the file is not supported error i did try a lot of answers about this issue in stackoverflow but i cannot access success thank you so much aldready 当我单击“创建共享”按钮时,出现文件不支持错误,我在stackoverflow中确实尝试了很多有关此问题的答案,但我无法获得成功,非常感谢aldready
Button btnShare;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
btnShare=(Button) findViewById(R.id.button);
btnShare.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
File f=new File("R.raw.sound3");
Uri uri = Uri.parse("file://"+f.getAbsolutePath());
Intent share = new Intent(Intent.ACTION_SEND);
share.putExtra(Intent.EXTRA_STREAM, uri);
share.setType("audio/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(Intent.createChooser(share, "Share audio File"));
}
});
}
} }
manifest permissions 清单权限
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
There are two things you need to do. 您需要做两件事。 Firstly check if you have granted permission and check if your raw data in external storage.Let's give example : 首先检查您是否已授予许可,并检查您的原始数据是否在外部存储中。
We have a simple button on main xml 我们在主xml上有一个简单的按钮
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity">
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:text="Paylas" />
</RelativeLayout>
Check if you have permisson : 检查您是否允许:
public boolean isStoragePermissionGranted() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED) {
Log.v("","Permission is granted");
return true;
} else {
Log.v("","Permission is revoked");
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);
return false;
}
}
else { //permission is automatically granted on sdk<23 upon installation
Log.v("","Permission is granted");
return true;
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if(grantResults[0]== PackageManager.PERMISSION_GRANTED){
Log.v("","Permission: "+permissions[0]+ "was "+grantResults[0]);
//resume tasks needing this permission
}
}
Copy your raw data into external storage : 将您的原始数据复制到外部存储中:
private String copyFiletoExternalStorage(int resourceId, String resourceName){
String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;
try{
InputStream in = getResources().openRawResource(resourceId);
FileOutputStream out = null;
out = new FileOutputStream(pathSDCard);
byte[] buff = new byte[1024];
int read = 0;
try {
while ((read = in.read(buff)) > 0) {
out.write(buff, 0, read);
}
} finally {
in.close();
out.close();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
return pathSDCard;
}
and the main code should be like this : 并且主要代码应如下所示:
private Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(isStoragePermissionGranted()) {
String rout = copyFiletoExternalStorage(R.raw.guitar,"guitar.mp3");
Uri uri = Uri.parse(rout);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/*");
share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
share.putExtra(Intent.EXTRA_STREAM,uri);
try {
startActivity(share);
}catch (android.content.ActivityNotFoundException ex){
Toast.makeText(getApplicationContext(),"Please, install Whatsapp", Toast.LENGTH_LONG).show();
}
}
}
});
Also may you want to check full source code here is github 也可能您想查看完整的源代码,这里是github
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.