Android替代的requestLegacyExternalStorage?
创始人
2024-08-21 17:00:33
0

Android 10 引入了一项新的文件沙盒安全性策略,需要使用类似于FileProvider这样的API,才能够在您的应用程序中访问共享存储空间。现在,如果您的应用程序需要访问共享存储空间,则需要使用目标SDK为29或更高版本,并有以下几种选择:

1.使用MediaStore API:

通过MediaStore API只能访问图片、音频、视频和文档文件,示例如下:

String selection = MediaStore.Images.Media.BUCKET_DISPLAY_NAME + " =?";
String[] selectionArgs = new String[] {"Camera"};
String[] projection = new String[] {MediaStore.Images.Media.DATA};
Cursor cursor = context.getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, selection, selectionArgs, null);
if(cursor!=null){
   int col = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
   while (cursor.moveToNext()) {
       String path = cursor.getString(col);
       // get file path here
   }
   cursor.close();
}

2.使用SAF框架:

SAF框架是一种用户界面,可以方便地选择文件和目录,示例如下:

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
int requestCode = 42;
startActivityForResult(intent, requestCode);

然后在onActivityResult方法中获取所选目录:

@Override
public void onActivityResult(int requestCode, int resultCode, Intent resultData) {
    if (requestCode == 42 && resultCode == Activity.RESULT_OK) {
        Uri treeUri = resultData.getData();
        DocumentFile pickedDir = DocumentFile.fromTreeUri(this, treeUri);
        // create file in directory here
    }
}

3.使用Scoped Storage:

Scoped Storage是Android 11发布后,默认情况下启用的一种新型的存储模式。它允许应用程序在公共存储空间中可见文件的访问权限范围更加明确,而不是让应用程序可以在存

相关内容

热门资讯

记者发布!pokemmo修改器... 记者发布!pokemmo修改器手机版,wepoker私人辅助器,总是真的是有挂(有挂助手)1、操作简...
实测分享!德普之星透视辅助软件... 实测分享!德普之星透视辅助软件是真的吗,德州hhpoker脚本,真是是有挂(有挂分享)1、下载好脚本...
揭秘真相!wepoker辅助器... 揭秘真相!wepoker辅助器官方,we poker辅助器,本来真的有挂(有挂总结)1、wepoke...
科技通报!wepoker怎么增... 科技通报!wepoker怎么增加运气,HH平台挂,一贯真的有挂(有挂细节)亲,关键说明,透视脚本安卓...
技术分享!hhpoker有没有... 技术分享!hhpoker有没有辅助,hhpoker脚本,真是真的有挂(有挂方式)1、很好的工具软件,...
一分钟揭秘!pokemmo手机... 一分钟揭秘!pokemmo手机版修改器,大菠萝辅助器,原来真的是有挂(有挂总结)1、让任何用户在无需...
9分钟了解!wepoker透视... 9分钟了解!wepoker透视苹果系统,wepoker辅助透视软件,果然存在有挂(有挂教学)1、we...
免费测试版!werplan辅助... 免费测试版!werplan辅助软件,德普之星透视辅助软件,确实是有挂(真的有挂)辅助器是一种具有地方...
玩家实测!wepoker有辅助... 玩家实测!wepoker有辅助插件吗,wepoker透视脚本是什么,切实真的是有挂(有挂总结)1、打...
科普分享!wpk可以作弊吗,h... 科普分享!wpk可以作弊吗,hhpoker德州有挂吗,原来是有挂(有人有挂)1、进入到是否有挂之后,...