【问题标题】:File not found Exception while accessing in Android 11 - Target SDK 30 - Android在 Android 11 中访问时找不到文件异常 - 目标 SDK 30 - Android
【发布时间】: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


【解决方案1】:

您是否确定并确认该文件在所述路径上确实可用? 尝试使用 adb root 访问。找到它并验证/确认音频文件是否存在。

请同时在 AndroidManifest.xml 中添加 android.permission.WRITE_EXTERNAL_STORAGE

【讨论】:

  • WRITE_EXTERNAL_STORAGE 在 Android 11 上被完全忽略,因为您必须改用范围存储。
猜你喜欢
  • 2021-06-30
  • 1970-01-01
  • 2020-08-05
  • 1970-01-01
  • 2013-08-02
  • 1970-01-01
  • 1970-01-01
  • 2021-04-14
  • 2023-03-26
相关资源
最近更新 更多