Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/185.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android-如何与其他应用程序共享声音文件_Android_Share_Whatsapp_Android External Storage - Fatal编程技术网

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

Android-如何与其他应用程序共享声音文件,android,share,whatsapp,android-external-storage,Android,Share,Whatsapp,Android External Storage,最近,我使用以下代码与其他应用程序(如Whatsapp)共享mp3文件,一切正常,但现在我总是收到“Error2”吐司,文件不会发送。 我读了很多关于这个话题的文章,但没有什么真正对我有帮助 MediaPlayer MP; public String ordnerpfad = Environment.getExternalStorageDirectory()+ "/Sounds"; public Str

最近,我使用以下代码与其他应用程序(如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"/>

在Android 6和7中,访问外部存储被视为“危险权限”,必须在运行时请求

您还可以在系统设置(应用程序>您的应用程序>权限)中授予该权限

shareIntent.setType(“音频/*”)


share.addFlags(Intent.FLAG\u GRANT\u READ\u URI\u权限)

我总是得到“Error”toast
哪个“Error toast”?在这里创建的toast:}catch(异常e){e.printStackTrace();toast.makeText(getApplicationContext(),“Error”,toast.LENGTH_SHORT).show();}在toast中加一个数字,如Error1,Error2。。。所以我们知道你犯了什么错误。它是Error2@saiful103aPrinting out“error”`不会告诉你多少
e.getMessage()
是一个更好的选择。