在Android Studio上共享音频



不要在我的应用中(在raw/suono.mp3中)在whatapp应用程序上共享mp3音频

    final Button pulsante2 =(Button) findViewById(R.id.pulsante2);
    pulsante2.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View v) {
            suono2=MediaPlayer.create(getApplicationContext(),R.raw.suono2);
            suono2.start();
        }
        });
     //tasto premuto piu a lungo
    pulsante2.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View v) {
            Intent share = new Intent(Intent.ACTION_SEND);
            share.setType("audio/*");
            Uri uri = Uri.parse("android.resource://" + getPackageName()
                    + "/raw/" + R.raw.suono2);
            share.putExtra(Intent.EXTRA_STREAM, uri);
            startActivity(Intent.createChooser(share, "Share Sound File"));

            return true;

        }
        });

什么应用说我无法加载文件,请重新加载

              InputStream inputStream;
                FileOutputStream fileOutputStream;
                try {
                    inputStream = getResources().openRawResource(R.raw.suono2);
                    fileOutputStream = new FileOutputStream(
                            new File(Environment.getExternalStorageDirectory(), "sound.mp3"));
                    byte[] buffer = new byte[1024];
                    int length;
                    while ((length = inputStream.read(buffer)) > 0) {
                        fileOutputStream.write(buffer, 0, length);
                    }
                    inputStream.close();
                    fileOutputStream.close();
            } catch (IOException e) {
                e.printStackTrace();
            }

最新更新