我想分享whatsapp,gmail等的声音

cuxqih21  于 2021-06-29  发布在  Java
关注(0)|答案(0)|浏览(212)

我想分享whatsapp,gmail等的声音,我已经添加了我的java代码。应用程序没有任何ERORR。但当我想分享一个声音时,我选择whatsapp,然后屏幕上会显示“文件格式不受支持”。你能帮助我吗。此代码在api 25以下运行良好,但在api 25以上运行不正常。我添加了@/xml/provider\u路径,但它不起作用。我的密码正确吗?你能帮帮我吗。公共类mainactivity扩展了appcompatactivity{mediaplayer-mediaplayer;

private boolean ctrl = false;
private void checkPermission(){
    ActivityCompat.requestPermissions(e6.this,
            new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE},
            1);
}
private String copyFiletoExternalStorage(int resourceId, String resourceName){
    String folder = "com.example.beyazfutbolsesleri";
    String baseDir = Environment.getExternalStorageDirectory()+ "/Android/data/" ;
    File f = new File(baseDir, folder);
    if (!f.exists()) {
        f.mkdirs();
    }

    String pathEX = baseDir  +  folder + "/" + resourceName;
    try{
        InputStream in = getResources().openRawResource(resourceId);
        FileOutputStream out = null;
        out = new FileOutputStream(pathEX);
        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 pathEX;
}

private void sendAudio1(){
    try {
        // We copied the file to the memory card
        String mediaPath = copyFiletoExternalStorage(R.raw.aaa1,"aaa1.mp3");
        Intent shareMedia = new Intent(Intent.ACTION_SEND);
        shareMedia.setType("audio/*");
        shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(mediaPath));
        startActivity(Intent.createChooser(shareMedia, "Share sound "));
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show();
 }
 }

private void sendAudio2(){
    try {
       // We copied the file to the memory card
        String mediaPath = copyFiletoExternalStorage(R.raw.aaa2,"aaa2.mp3");
        Intent shareMedia = new Intent(Intent.ACTION_SEND);
        shareMedia.setType("audio/*");
        shareMedia.putExtra(Intent.EXTRA_STREAM, Uri.parse(mediaPath));
        startActivity(Intent.createChooser(shareMedia, "Share sound "));
    } catch (Exception e) {
        Toast.makeText(getApplicationContext(), "Failed", Toast.LENGTH_LONG).show(); 
}
}

@Override
public void onRequestPermissionsResult(int requestCode,
                                       String permissions[], int[] grantResults) {
    switch (requestCode) {
        case 1: {
            //Eğer izin verildiyse
            if (grantResults.length > 0
                    && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
                ctrl = true;
                //izin verilmediyse
            } else {
           Toast.makeText(e6.this,"Access to external memory was denied", Toast.LENGTH_LONG).show();
            }
            return;
        }
    }
}

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

  checkPermission();

    Button b1 = (Button) findViewById(R.id.button_soundaaa1);
    Button b2 = (Button) findViewById(R.id.button_soundaaa2);

    b1.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            sendAudio1();
            return false;
        }
    });

    b2.setOnLongClickListener(new View.OnLongClickListener() {
        @Override
        public boolean onLongClick(View view) {
            sendAudio2();
            return false;
        }
    });

//This codes for run sound when click the button.
    mediaPlayer = null;
}

public void music(View view) {
    switch (view.getId()) {
        case R.id.button_soundaaa1:
            if (mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(this, R.raw.aaa1);
            }
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    stopMusic();
                }
            });
            mediaPlayer.start();
            if (mediaPlayer != null) {
                mediaPlayer.pause();
            }
            mediaPlayer.start();
            break;

        case R.id.button_soundaaa2:
            if (mediaPlayer == null) {
                mediaPlayer = MediaPlayer.create(this, R.raw.aaa2);
            }
            mediaPlayer.setOnCompletionListener(new MediaPlayer.OnCompletionListener() {
                @Override
                public void onCompletion(MediaPlayer mediaPlayer) {
                    stopMusic();
                }
            });
            mediaPlayer.start();
            if (mediaPlayer != null) {
                mediaPlayer.pause();
            }
            mediaPlayer.start();
            break;

    }
}

private void stopMusic() {
    if (mediaPlayer!=null){
    mediaPlayer.release();
    mediaPlayer=null;

    }

}

@Override
protected void onStop() {
    super.onStop();
    stopMusic();
}
}

暂无答案!

目前还没有任何答案,快来回答吧!

相关问题