为了账号安全,请及时绑定邮箱和手机立即绑定

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

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

宝慕林4294392 2022-07-20 10:58:44
当我单击创建的共享按钮时,我获取了文件不支持错误我确实在stackoverflow中尝试了很多关于这个问题的答案,但我无法访问成功,非常感谢Button btnShare;@Overrideprotected 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"/>
查看完整描述

1 回答

?
动漫人物

TA贡献1815条经验 获得超10个赞

您需要做两件事。首先检查您是否已授予权限并检查您的原始数据是否在外部存储中。让我们举个例子:


我们在主 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>

检查您是否有许可:


 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

        }

    }

将原始数据复制到外部存储中:


 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;

    }

主要代码应该是这样的:


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

                }

            }

        }

    });

你也可以在这里查看完整的源代码是github


查看完整回答
反对 回复 2022-07-20
  • 1 回答
  • 0 关注
  • 131 浏览

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号