如何将音频文件存储到安卓内部存储



我一直在谷歌上搜索很多解决方案,但它们不起作用。我还不太熟悉 Uri,路径被传递给函数和输入流输入/输出存储在存储文件中。

我的项目允许用户从外部存储附加音频文件,并获取其 Uri

从按钮设置单击时

acs.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(
lol, "Open Audio (mp3) file"), RQS_OPEN_AUDIO_MP3);

(这是来自onActivityResult 函数(

if (resultCode == RESULT_OK) {
if (requestCode == RQS_OPEN_AUDIO_FX) {
audioFxUri = data.getData();
}
}

之后我想做的是将音频存储到其内部存储中。

你能帮忙写一些在内部存储中存储音频文件的代码吗? 它还让它经历文件到字节数组然后字节数组到文件(带有内部存储位置(的过程?

我只是基于我目前所看到的。 希望你的代码对我有帮助。

您可以将其复制到应用程序文件 目录:getApplicationContext().getFilesDir().getAbsolutePath()

public Uri copyFile(Uri uri){
if (uri == null) {
return null;
}
String extension = MimeTypeMap.getSingleton().getExtensionFromMimeType(getApplicationContext().getContentResolver().getType(uri));
String fileName = "some_name" + "." + extension;
File tempFile = new File(getApplicationContext().getFilesDir().getAbsolutePath(), fileName);
try {
boolean fileCreated = tempFile.createNewFile();
if(!fileCreated){
Log.e(LOG_TAG,"error creating file");
}
InputStream inputStream = getApplicationContext().getContentResolver().openInputStream(uri);
if(inputStream != null){
IOUtils.copy(inputStream, new FileOutputStream(tempFile));
}
} catch (IOException | NullPointerException ex) {
Log.d(LOG_TAG, "Exception caught: " + ex.getMessage());
}
return Uri.fromFile(tempFile);
}

最新更新