Android - 如何与其他应用共享声音文件

时间:2016-11-22 16:11:27

标签: android share whatsapp android-external-storage

在最近的时间里,我使用以下代码与Whatsapp等其他应用程序共享mp3文件,一切正常,但现在我总是得到" Error2"吐司和文件不会发送。 我读过很多关于这个主题的文章,但没有真正帮助我。

MediaPlayer MP;





 public String ordnerpfad = Environment.getExternalStorageDirectory()+   "/Sounds";                                      
 public String soundpfad = ordnerpfad + "/sound.mp3";
 public File ordnerfile = new File(ordnerpfad);
 public File soundfile = new File(soundpfad);
 public Uri urisound = Uri.parse(soundpfad);
 public byte[] byte1 = new byte [1024];
 public int zwischenspeicher = 0;
 public InputStream is1;
 public FileOutputStream fos;
 public Intent shareintent;


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);



    //0

    Button button = (Button) this.findViewById(R.id.button);
    if (button != null) {
        button.setOnClickListener(new View.OnClickListener() {


            @Override
            public void onClick(View v) {


                stopPlaying();
                MP= MediaPlayer.create(MainActivity.this, R.raw.sound1);
                MP.start();

            }


        });
    }
    button.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            if( ! ordnerfile.exists()) {

                try {
                   ordnerfile.mkdir();
                } catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error1", Toast.LENGTH_SHORT).show();
            }




            }

            try {
                is1 = getResources().openRawResource(R.raw.sound1);
                fos = new FileOutputStream(soundfile);

                while ((zwischenspeicher = is1.read(byte1)) >0){


                    fos.write(byte1, 0, zwischenspeicher);
                }

                is1.close();
                fos.close();


            }catch (Exception e){
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Error2", Toast.LENGTH_SHORT).show();

            }

            shareintent = new Intent(Intent.ACTION_SEND);
            shareintent .setType("audio/*");
            shareintent .putExtra(Intent.EXTRA_STREAM, urisound);
            startActivity(Intent.createChooser(shareintent , "Share sound..."));





            return true;
        }

    });



    //1

    Button button1 = (Button) this.findViewById(R.id.button2);
    if (button1 != null) {
        button1.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {


                stopPlaying();
                MP= MediaPlayer.create(MainActivity.this, R.raw.sound2);
                MP.start();



            }
        });
    }
    button1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {

            if( ! ordnerfile.exists()) {

                try {
                    ordnerfile.mkdir();
                } catch (Exception e){
                    e.printStackTrace();
                    Toast.makeText(getApplicationContext(), "Error1", Toast.LENGTH_SHORT).show();
                }




            }

            try {
                is1 = getResources().openRawResource(R.raw.sound2);
                fos = new FileOutputStream(soundfile);

                while ((zwischenspeicher = is1.read(byte1)) >0){


                    fos.write(byte1, 0, zwischenspeicher);
                }

                is1.close();
                fos.close();


            }catch (Exception e){
                e.printStackTrace();
                Toast.makeText(getApplicationContext(), "Error2", Toast.LENGTH_SHORT).show();

            }

            shareintent= new Intent(Intent.ACTION_SEND);
            shareintent.setType("audio/*");
            shareintent.putExtra(Intent.EXTRA_STREAM, urisound);
            startActivity(Intent.createChooser(shareintent, "Share sound..."));

            return true;
        }
    });

清单:

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

2 个答案:

答案 0 :(得分:0)

在Android 6和7中,访问外部存储空间被视为危险权限&#39;必须在运行时请求。

https://developer.android.com/training/permissions/requesting.html

您还可以在系统设置中授予该权限(应用&gt;您的应用&gt;权限)

答案 1 :(得分:0)

shareIntent.setType("audio/*");

DO

share.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);