【问题标题】:Save file to folder in external storage and share将文件保存到外部存储中的文件夹并共享
【发布时间】:2015-11-02 21:19:42
【问题描述】:

我正在制作一个音板应用程序,我需要在长按 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 等)共享该声音。 )?

【问题讨论】:

标签: android audio share


【解决方案1】:

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

<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 ....

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

Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_MUSIC)

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

【讨论】:

  • 只是为了补充答案:将这些音频文件保存到公共音频文件夹允许任何应用程序都可以访问它!因此,如果一个应用程序允许发布或上传任何类型的音频文件,一旦您将它们保存在此文件夹中,它们就可以访问它们。
  • 我认为它们不应该存储在音乐文件夹中,因为它们的声音非常短(1-6 秒)
猜你喜欢
  • 1970-01-01
  • 2015-12-22
  • 2011-12-14
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-11-10
  • 1970-01-01
相关资源
最近更新 更多