如何将声音保存到内部存储器的下载文件夹

时间:2019-05-28 12:09:55

标签: android

我有一个音板,其中Raw文件夹中包含许多声音。每个按钮都对应于单击时播放的声音。

我想长按一下声音,然后将.mp3文件保存到手机存储的“下载”文件夹中。我的手机不具备SD卡功能,因此只能用于内部存储器。

我现在有一些代码,我已经尝试过将它们保存到应用程序的位置,但是我不知道如何更改与下载相关的代码。

有人可以帮忙吗?如果需要,我在下面附加了一些代码。非常感谢。

@Override
        protected void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);

            setContentView(R.layout.activity_second);

            Button a;

            a = findViewById(R.id.button11);

           final String path = Environment.getExternalStorageDirectory().getPath()
                    + "/media/audio/";

            a.setOnLongClickListener(new View.OnLongClickListener() {
                public boolean onLongClick(View arg0) {

                    try {
                        copyRAWtoPhone(R.raw.addressmeassir, path);
                    } catch (IOException e) {
                        e.printStackTrace();
                    }

                    return true;
                }

                private void copyRAWtoPhone(int id, String path) throws IOException {

                    InputStream in = getResources().openRawResource(id);
                    FileOutputStream out = new FileOutputStream(path);
                    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();
                    }


                }
            });

1 个答案:

答案 0 :(得分:0)

保存到Download文件夹:

 val file = File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS), name)
  if (writeToFile_1(Objects.requireNonNull<ResponseBody>(response.body()), file)) {
   Toast.makeText(context.applicationContext, "File was downloaded.", Toast.LENGTH_SHORT).show()
   } else {
   Toast.makeText(context.applicationContext, "Error.", Toast.LENGTH_SHORT).show()
}

和文件创建:

private fun writeToFile_1(body: ResponseBody, file: File): Boolean {
        var inputStream: InputStream? = null
        var outputStream: OutputStream? = null
        try {
            inputStream = body.byteStream()
            outputStream = FileOutputStream(file)

            val buffer = ByteArray(4096)
            while (true) {
                val read = inputStream.read(buffer)
                if (read < 0) break
                outputStream.write(buffer, 0, read)
            }
            outputStream.close()
            return true
        } catch (e: IOException) {
            e.printStackTrace()
        } finally {
            if (inputStream != null) {
                try {
                    inputStream.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
            if (outputStream != null) {
                try {
                    outputStream.close()
                } catch (e: IOException) {
                    e.printStackTrace()
                }

            }
        }
        return false
    }

我使用此方法从服务器保存文件,但是您可以编写另一个byte[],希望您对我的方法有所了解。您必须先获得byte[]的声音,然后通过我的方法写入文件。祝你好运:)