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

相关内容

热门资讯

透视透视!wepoker怎么开... 透视透视!wepoker怎么开辅助(透视)佛手大菠萝13道挂哪里,教程攻略(真是有挂)-哔哩哔哩1、...
透视透视!hhpoker真的有... 透视透视!hhpoker真的有透视吗(透视)pokermaster破解版,教程策略(确实有挂)-哔哩...
透视揭露!wepoker能不能... 透视揭露!wepoker能不能透视(透视)wepoker免费透视脚本,教程窍要(有挂详细)-哔哩哔哩...
透视解迷!wpk刷入池率脚本(... 透视解迷!wpk刷入池率脚本(透视)wepoker高级辅助,教程技法(真的有挂)-哔哩哔哩1、玩家可...
透视推荐!wepoker亲友圈... 透视推荐!wepoker亲友圈有用吗(透视)wepoker透视有没有,教程教材(有挂详细)-哔哩哔哩...
透视分享!hhpoker智能辅... 透视分享!hhpoker智能辅助插件(透视)有没有人wepoker,教程教程书(真实有挂)-哔哩哔哩...
透视推荐!pokermaste... 透视推荐!pokermaster脚本(透视)佛手在线大菠萝技巧,教程手段(有挂规律)-哔哩哔哩1、很...
透视分享!哈糖大菠萝开挂(透视... 透视分享!哈糖大菠萝开挂(透视)wpk私人局辅助是真的吗,教程练习(有挂技术)-哔哩哔哩1、这是跨平...
透视关于!newpoker怎么... 透视关于!newpoker怎么安装脚本(透视)大菠萝免费辅助,教程烘培(有挂详细)-哔哩哔哩1、超多...
透视详细!werplan透视挂... 透视详细!werplan透视挂(透视)hhpoker开挂教程,教程大纲(新版有挂)-哔哩哔哩1、在h...