Android11上访问下载文件返回空
创始人
2024-08-20 11:30:08
0

Android 11 引入了Scoped Storage,限制了应用程序对设备存储的访问权限。因此,访问下载文件时需要使用特殊的访问方式。以下是一个示例代码,用于在 Android 11 上从下载文件夹中检索文件列表:

private List getDownloadedFiles() {
    List downloadedFiles = new ArrayList<>();
    // Get downloads folder URI using DownloadManager
    String downloadsDirectoryPath = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath();
    Uri downloadsUri = Uri.parse(downloadsDirectoryPath);
    // Query downloads folder with proper permission
    Cursor cursor = getContentResolver().query(downloadsUri, null, null, null, null);
    if (cursor != null && cursor.moveToFirst()) {
        int fileNameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_TITLE);
        do {
            String fileName = cursor.getString(fileNameIndex);
            File downloadedFile = new File(downloadsDirectoryPath + File.separator + fileName);
            downloadedFiles.add(downloadedFile);
        } while (cursor.moveToNext());
        cursor.close();
    }
    return downloadedFiles;
}

这段代码使用了DownloadManager.COLUMN_TITLE来获取文件名,但是您可以使用其他列来获取URI、大小、MIME类型等。 请注意,此示例代码需要访问设备存储的权限。 您需要在AndroidManifest.xml文件中添加以下权限标签:



相关内容

热门资讯

截至目前!wepoker底牌透... 截至目前!wepoker底牌透视脚本,红龙poker辅助,操作教程(存在有挂)-哔哩哔哩1、不需要A...
据通报!wepoker黑侠辅助... 据通报!wepoker黑侠辅助器正版下载(透视)一直是有挂的(今日头条开挂辅助插件)-哔哩哔哩wep...
这一现象值得深思!wepoke... 这一现象值得深思!wepoker私人定制透视(透视)一贯确实有挂(有挂详细开挂辅助下载)-哔哩哔哩1...
据玩家消息!pokemmo脚本... 据玩家消息!pokemmo脚本辅助,hhpoker真的假的,步骤教程(有挂分析)-哔哩哔哩1、pok...
围绕透视问题!wepoker怎... 围绕透视问题!wepoker怎么设置房间,wepoker安装教程,要领教程(有挂技术)-哔哩哔哩1)...
有玩家发现!wepoker私人... 有玩家发现!wepoker私人局辅助器怎么用(透视)果然真的是有挂(真是有挂开挂辅助下载)-哔哩哔哩...
经核实!hhpoker有透视功... 经核实!hhpoker有透视功能吗(透视)其实真的有挂(有挂秘诀开挂辅助软件)-哔哩哔哩1、完成hh...
长期以来!poker红龙辅助,... 长期以来!poker红龙辅助,pokemmo脚本辅助,烘培教程(有挂方略)-哔哩哔哩1、上手简单,内...
近日!pokemmo辅助器手机... 近日!pokemmo辅助器手机版下载(透视)其实是有挂的(的确有挂开挂辅助软件)-哔哩哔哩1、pok...
围绕透视问题!wepoker私... 围绕透视问题!wepoker私局代打,hhpoker真的假的,方式教程(存在有挂)-哔哩哔哩1、围绕...