首页 文章

将文件保存到外部存储中的文件夹并共享

提问于
浏览
1

我正在制作一个音板应用程序,我需要在长按button1时共享sound1 . 我可以使用此代码进行共享菜单:

Button button1;


    button1 = (Button) v.findViewById(R.id.button1);
    button1.setLongClickable(true);

    button1.setOnLongClickListener(new View.OnLongClickListener() {

        @Override
        public boolean onLongClick(View arg0) {

            Intent shareIntent = new Intent(Intent.ACTION_SEND);
            shareIntent.setType("audio/ogg");

            Uri.parse("android.resource://test.testapp/" + R.raw.sound1);
            shareIntent.putExtra(Intent.EXTRA_STREAM, Uri.parse("android.resource://test.testapp/" + R.raw.sound1));
            startActivity(Intent.createChooser(shareIntent, "Share Sound"));

            return true;
        }

    });


    return v;
}

我可以与whatsapp和Google Drive完美地共享音频文件,但其他应用程序无法正常工作 . 我已经读过你必须将文件复制到外部存储器,并从那里共享它们 . 我现在已经搜索了近两天,但我找不到办法做到这一点 . Stack上的其他文章也没有帮助我:/

如何在外部存储中创建目录,将文件(sound1.ogg)从我的/ raw文件夹复制到该文件夹,然后与另一个应用程序(Gmail,Google Drive,Whatsapp,Skype等)共享该声音?

1 回答

  • 0

    将内容保存到外部存储非常简单 . 首先,您需要将其添加到清单文件以允许外部存储写入:

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

    然后在你的活动/片段中:

    String root = Environment.getExternalStorageDirectory().toString();
     File myDir = new File(root + "/saved_images");  
     myDir.mkdirs();  
     File file = new File (myDir, "FILE_NAME");
     ... however you write the file through output stream ....
    

    由于这些是音频文件,您应该将它们存储在每个用户Android手机的公共音频库中 . 您可以像这样访问它:

    Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)
    

    我建议查看this article了解更多信息!

相关问题