【发布时间】:2022-01-05 11:14:47
【问题描述】:
我正在下载音频,我想在我的应用中播放下载的音频
从下面的代码,我正在下载音频
ContentValues contentValues = new ContentValues();
contentValues.put(MediaStore.Audio.Media.DISPLAY_NAME, finleName);
contentValues.put(MediaStore.Audio.Media.MIME_TYPE, "audio/mpeg");
contentValues.put(MediaStore.Audio.Media.RELATIVE_PATH, Environment.DIRECTORY_DOWNLOADS + sep + newFolder + sep);
contentValues.put(MediaStore.Audio.Media.IS_PENDING, true);
Uri uri = MediaStore.Downloads.getContentUri(MediaStore.VOLUME_EXTERNAL_PRIMARY);
Uri itemUri = getContentResolver().insert(uri, contentValues);
mediaFile = Environment.DIRECTORY_DOWNLOADS + sep + newFolder + sep + finleName;
Log.d("Path: ", mediaFile + "\nUri Path:" + uri.getPath() + "\nFile Path: " + itemUri.getPath());
OutputStream output = getContentResolver().openOutputStream(itemUri);
byte[] data = new byte[1024];
while ((count = input.read(data)) != -1) {
output.write(data, 0, count);
}
output.flush();
output.close();
contentValues.put(MediaStore.Audio.Media.IS_PENDING, false);
getContentResolver().update(itemUri, contentValues, null, null);
但是当我尝试访问这个下载的文件时,我得到了 File Not Found Exception 并且我无法播放音频
这里的音频已成功下载,但仍然无法访问。
【问题讨论】:
-
您是否尝试过“管理外部存储”权限?
-
是的,但 Playstore 拒绝了我启用此权限的构建
-
您需要通过不同的方式检查文件路径..您可以从这里获得帮助:github.com/iPaulPro/aFileChooser/blob/master/aFileChooser/src/…
-
But when i try to access this downloaded file显示您的代码,因为当您可以写入该 uri 时,您当然可以从中读取。
标签: android kotlin mediastore