猿问

通过 whatsapp 共享 mp3(不支持此文件)错误

当我单击创建的共享按钮时,我获取了文件不支持错误我确实在stackoverflow中尝试了很多关于这个问题的答案,但我无法访问成功,非常感谢


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"));         


        }

    });




}

}


清单权限


<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>


宝慕林4294392
浏览 132回答 1
1回答

动漫人物

您需要做两件事。首先检查您是否已授予权限并检查您的原始数据是否在外部存储中。让我们举个例子:我们在主 xml 上有一个简单的按钮&nbsp; &nbsp; <?xml version="1.0" encoding="utf-8"?><RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"&nbsp; &nbsp; xmlns:app="http://schemas.android.com/apk/res-auto"&nbsp; &nbsp; xmlns:tools="http://schemas.android.com/tools"&nbsp; &nbsp; android:layout_width="match_parent"&nbsp; &nbsp; android:layout_height="match_parent"&nbsp; &nbsp; tools:context=".MainActivity">&nbsp; &nbsp; <Button&nbsp; &nbsp; &nbsp; &nbsp; android:id="@+id/button"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_width="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_height="wrap_content"&nbsp; &nbsp; &nbsp; &nbsp; android:layout_centerInParent="true"&nbsp; &nbsp; &nbsp; &nbsp; android:text="Paylas" /></RelativeLayout>检查您是否有许可:&nbsp;public&nbsp; boolean isStoragePermissionGranted() {&nbsp; &nbsp; &nbsp; &nbsp; if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if (checkSelfPermission(android.Manifest.permission.WRITE_EXTERNAL_STORAGE)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; == PackageManager.PERMISSION_GRANTED) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.v("","Permission is granted");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } else {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.v("","Permission is revoked");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, 1);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return false;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; else { //permission is automatically granted on sdk<23 upon installation&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.v("","Permission is granted");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; return true;&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }&nbsp; &nbsp; @Override&nbsp; &nbsp; public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {&nbsp; &nbsp; &nbsp; &nbsp; super.onRequestPermissionsResult(requestCode, permissions, grantResults);&nbsp; &nbsp; &nbsp; &nbsp; if(grantResults[0]== PackageManager.PERMISSION_GRANTED){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Log.v("","Permission: "+permissions[0]+ "was "+grantResults[0]);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; //resume tasks needing this permission&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; }将原始数据复制到外部存储中:&nbsp;private String copyFiletoExternalStorage(int resourceId, String resourceName){&nbsp; &nbsp; &nbsp; &nbsp; String pathSDCard = Environment.getExternalStorageDirectory() + "/Android/data/" + resourceName;&nbsp; &nbsp; &nbsp; &nbsp; try{&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; InputStream in = getResources().openRawResource(resourceId);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; FileOutputStream out = null;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out = new FileOutputStream(pathSDCard);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; byte[] buff = new byte[1024];&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; int read = 0;&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; while ((read = in.read(buff)) > 0) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.write(buff, 0, read);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; } finally {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; in.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; out.close();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; } catch (FileNotFoundException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; } catch (IOException e) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; e.printStackTrace();&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; return pathSDCard;&nbsp; &nbsp; }主要代码应该是这样的:private Button button;@Overrideprotected void onCreate(Bundle savedInstanceState) {&nbsp; &nbsp; super.onCreate(savedInstanceState);&nbsp; &nbsp; setContentView(R.layout.activity_main);&nbsp; &nbsp; button = findViewById(R.id.button);&nbsp; &nbsp; button.setOnClickListener(new View.OnClickListener() {&nbsp; &nbsp; &nbsp; &nbsp; @Override&nbsp; &nbsp; &nbsp; &nbsp; public void onClick(View v) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; if(isStoragePermissionGranted()) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; String rout = copyFiletoExternalStorage(R.raw.guitar,"guitar.mp3");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Uri uri = Uri.parse(rout);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Intent share = new Intent(Intent.ACTION_SEND);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; share.setType("audio/*");&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; share.putExtra(Intent.EXTRA_STREAM,uri);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; try {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; startActivity(share);&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }catch (android.content.ActivityNotFoundException ex){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Toast.makeText(getApplicationContext(),"Please, install Whatsapp", Toast.LENGTH_LONG).show();&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; });你也可以在这里查看完整的源代码是github
随时随地看视频慕课网APP

相关分类

Java
我要回答