Android:无法通过文本或其他应用程序从原始文件夹发送音频文件

时间:2018-05-07 06:55:39

标签: android audio android-intent

我正在制作一个音板应用程序,我有一个AlertDialog,弹出询问用户是否想通过任何应用程序(文本,gmail等)将原始文件夹中的特定音频文件发送给朋友,但是当我尝试要做到这一点,我得到“无法附加文件”

这是我的代码:

            CharSequence choices[] = new CharSequence[] {"Send to friend", "Set as notification sound"};

            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Options");
            builder.setItems(choices, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(which){
                        case 0:
                            Intent share = new Intent(Intent.ACTION_SEND);
                            share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + getContext().getPackageName() + "/" + myAudio[position]));
                            share.setType("audio/*");
                            startActivity(Intent.createChooser(share, "Share"));
                            break;
                        case 1:
                            break;
                    }
                }
            });
            builder.show();

myAudio是一个包含raw文件夹中的媒体的数组,

myAudio = new int[]{ R.raw.audioone, R.raw.audiotwo, R.raw.audiothree};

有人能在这里发现问题吗?谢谢你的帮助!

编辑:这是我在被告知首先保存到存储时尝试的内容。它仍然不起作用:(

            CharSequence choices[] = new CharSequence[] {"Send to friend", "Set as notification sound"};

            AlertDialog.Builder builder = new AlertDialog.Builder(getContext());
            builder.setTitle("Options");
            builder.setItems(choices, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    switch(which){
                        case 0:
                            /*
                            Intent share = new Intent(Intent.ACTION_SEND);
                            share.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://" + getContext().getPackageName() + "/R.raw.hkwheresthelambsauce"));
                            share.setType("audio/*");
                            startActivity(Intent.createChooser(share, "Share")); */
                            InputStream inputStream;
                            FileOutputStream fileOutputStream;
                            try {
                                inputStream = getResources().openRawResource(myAudio[position]);
                                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();
                            }

                            Intent intent = new Intent(Intent.ACTION_SEND);
                            intent.putExtra(Intent.EXTRA_STREAM,
                                    Uri.parse("file://" + Environment.getExternalStorageDirectory() + "/sound.mp3" ));
                            intent.setType("audio/*");
                            startActivity(Intent.createChooser(intent, "Share sound"));
                            break;
                        case 1:
                            break;
                    }
                }
            });
            builder.show();

0 个答案:

没有答案