首页 > Java > java教程 > 正文

Android Scoped Storage: 如何读取外部存储特定文件夹的文件

聖光之護
发布: 2025-08-14 18:42:25
原创
541人浏览过

android scoped storage: 如何读取外部存储特定文件夹的文件

本文档旨在指导开发者如何在Android Scoped Storage环境下,使用Storage Access Framework (SAF) 读取外部存储特定文件夹中的文件。我们将提供详细的代码示例和步骤,帮助你理解SAF的工作原理,并安全高效地访问所需的文件。

在Android 10 (API level 29) 及更高版本中,引入了Scoped Storage,旨在提高用户隐私和数据安全。Scoped Storage限制了应用直接访问外部存储的能力,应用只能访问自己的应用特定目录以及用户通过系统文件选择器授予访问权限的目录。为了在Scoped Storage环境下访问外部存储,我们需要使用Storage Access Framework (SAF)。

Storage Access Framework (SAF) 概述

SAF允许用户选择一个目录,并授权应用访问该目录及其子目录。用户授权后,应用可以读取和写入该目录中的文件,而无需请求READ_EXTERNAL_STORAGE 或 WRITE_EXTERNAL_STORAGE 权限。

百宝箱
百宝箱

百宝箱是支付宝推出的一站式AI原生应用开发平台,无需任何代码基础,只需三步即可完成AI应用的创建与发布。

百宝箱 911
查看详情 百宝箱

实现步骤

以下步骤展示了如何使用SAF读取特定文件夹中的文件:

  1. 请求用户选择目录: 使用ACTION_OPEN_DOCUMENT_TREE Intent启动系统文件选择器,让用户选择需要访问的目录。
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.storage.StorageManager;
import android.util.Log;

public class Check {
    Context context;
    private String TAG="SOMNATH";

    public Check(Context context) {
        this.context = context;
    }

    public boolean  ch(){
        if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.Q) {
            StorageManager sm = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE);

            Intent intent = sm.getPrimaryStorageVolume().createOpenDocumentTreeIntent();
            //String startDir = "Android";
            //String startDir = "Download"; // Not choosable on an Android 11 device
            //String startDir = "DCIM";
            //String startDir = "DCIM/Camera";  // replace "/", "%2F"
            //String startDir = "DCIM%2FCamera";
            String startDir = "Documents";

            Uri uri = intent.getParcelableExtra("android.provider.extra.INITIAL_URI");

            String scheme = uri.toString();

            Log.d(TAG, "INITIAL_URI scheme: " + scheme);

            scheme = scheme.replace("/root/", "/document/");

            scheme += "%3A" + startDir;

            uri = Uri.parse(scheme);

            intent.putExtra("android.provider.extra.INITIAL_URI", uri);

            Log.d(TAG, "uri: " + uri.toString());

            ((Activity) context).startActivityForResult(intent, 12123);

            return true;
        }
        else{
            return false;
        }
    }



}
登录后复制
  1. 处理ActivityResult: 在onActivityResult方法中,接收用户选择的目录URI,并持久化存储该URI。
package com.axanor.saf_sample

import android.app.Activity
import android.content.Intent
import android.net.Uri
import android.provider.DocumentsContract
import android.util.Log
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.documentfile.provider.DocumentFile
import java.io.FileNotFoundException
import java.io.FileOutputStream
import java.io.IOException

class StorageAccess {
    val activity:Activity;
    val LOGTAG = "SOMNATH"
    val REQUEST_CODE = 12123
    constructor(activity: Activity) {
        this.activity = activity
    }




    public fun openDocumentTree() {
        val check = Check(activity)
        val uriString = SpUtil.getString(SpUtil.FOLDER_URI, "")
        when {
            uriString == "" -> {
                Log.w(LOGTAG, "uri not stored")


                if (!check.ch()){
                    askPermission()
                }

            }
            arePermissionsGranted(uriString) -> {
                makeDoc(Uri.parse(uriString))
            }
            else -> {
                Log.w(LOGTAG, "uri permission not stored")
                if (!check.ch()){
                    askPermission()
                }
            }
        }
    }

    // this will present the user with folder browser to select a folder for our data
    public fun askPermission() {
        val intent = Intent(Intent.ACTION_OPEN_DOCUMENT_TREE)
        activity.startActivityForResult(intent, REQUEST_CODE)
    }



    public fun makeDoc(dirUri: Uri) {
        val dir = DocumentFile.fromTreeUri(activity, dirUri)
        if (dir == null || !dir.exists()) {
            //the folder was probably deleted
            Log.e(LOGTAG, "no Dir")
            //according to Commonsware blog, the number of persisted uri permissions is limited
            //so we should release those we cannot use anymore
            //https://commonsware.com/blog/2020/06/13/count-your-saf-uri-permission-grants.html
            releasePermissions(dirUri)
            //ask user to choose another folder
            Toast.makeText(activity,"Folder deleted, please choose another!", Toast.LENGTH_SHORT).show()
            openDocumentTree()
        } else {
            val file = dir.createFile("*/txt", "test.txt")
            if (file != null && file.canWrite()) {
                Log.d(LOGTAG, "file.uri = ${file.uri.toString()}")
                alterDocument(file.uri)
            } else {
                Log.d(LOGTAG, "no file or cannot write")
                //consider showing some more appropriate error message
                Toast.makeText(activity,"Write error!", Toast.LENGTH_SHORT).show()

            }
        }
    }


    public fun releasePermissions(uri: Uri) {
        val flags: Int = Intent.FLAG_GRANT_READ_URI_PERMISSION or
                Intent.FLAG_GRANT_WRITE_URI_PERMISSION
        activity.contentResolver.releasePersistableUriPermission(uri,flags)
        //we should remove this uri from our shared prefs, so we can start over again next time
        SpUtil.storeString(SpUtil.FOLDER_URI, "")
    }


    //Just a test function to write something into a file, from https://developer.android.com
    //Please note, that all file IO MUST be done on a background thread. It is not so in this
    //sample - for the sake of brevity.
    public fun alterDocument(uri: Uri) {
        try {

            activity.contentResolver.openFileDescriptor(uri, "w")?.use { parcelFileDescriptor ->
                FileOutputStream(parcelFileDescriptor.fileDescriptor).use {
                    it.write(
                        ("String written at ${System.currentTimeMillis()}\n")
                            .toByteArray()
                    )
                    Toast.makeText(activity,"File Write OK!", Toast.LENGTH_SHORT).show()


                    val intent = Intent(Intent.ACTION_OPEN_DOCUMENT)
                    intent.addCategory(Intent.CATEGORY_OPENABLE)
                    intent.type = "application/pdf"

                    // Optionally, specify a URI for the file that should appear in the
                    // system file picker when it loads.

                    // Optionally, specify a URI for the file that should appear in the
                    // system file picker when it loads.
                    intent.putExtra(DocumentsContract.EXTRA_INITIAL_URI, uri)

                    activity.startActivityForResult(intent, 2)



                }
            }
        } catch (e: FileNotFoundException) {
            e.printStackTrace()
        } catch (e: IOException) {
            e.printStackTrace()
        }
    }


    public fun arePermissionsGranted(uriString: String): Boolean {
        // list of all persisted permissions for our app
        val list = activity.contentResolver.persistedUriPermissions
        for (i in list.indices) {
            val persistedUriString = list[i].uri.toString()
            //Log.d(LOGTAG, "comparing $persistedUriString and $uriString")
            if (persistedUriString == uriString && list[i].isWritePermission && list[i].isReadPermission) {
                //Log.d(LOGTAG, "permission ok")
                return true
            }
        }
        return false
    }







}
登录后复制
StorageAccess access = new StorageAccess(this);
access.openDocumentTree();
登录后复制
  1. 使用DocumentFile访问文件: 使用DocumentFile.fromTreeUri()方法从目录URI创建一个DocumentFile对象。然后,可以使用DocumentFile对象遍历目录中的文件和子目录。
    public fun makeDoc(dirUri: Uri) {
        val dir = DocumentFile.fromTreeUri(activity, dirUri)
        if (dir == null || !dir.exists()) {
            //the folder was probably deleted
            Log.e(LOGTAG, "no Dir")
            //according to Commonsware blog, the number of persisted uri permissions is limited
            //so we should release those we cannot use anymore
            //https://commonsware.com/blog/2020/06/13/count-your-saf-uri-permission-grants.html
            releasePermissions(dirUri)
            //ask user to choose another folder
            Toast.makeText(activity,"Folder deleted, please choose another!", Toast.LENGTH_SHORT).show()
            openDocumentTree()
        } else {
            val file = dir.createFile("*/txt", "test.txt")
            if (file != null && file.canWrite()) {
                Log.d(LOGTAG, "file.uri = ${file.uri.toString()}")
                alterDocument(file.uri)
            } else {
                Log.d(LOGTAG, "no file or cannot write")
                //consider showing some more appropriate error message
                Toast.makeText(activity,"Write error!", Toast.LENGTH_SHORT).show()

            }
        }
    }
登录后复制
  1. 检查权限: 在使用SAF访问文件之前,始终检查应用是否具有访问该文件的权限。可以使用arePermissionsGranted方法来检查是否已授予URI权限。
    public fun arePermissionsGranted(uriString: String): Boolean {
        // list of all persisted permissions for our app
        val list = activity.contentResolver.persistedUriPermissions
        for (i in list.indices) {
            val persistedUriString = list[i].uri.toString()
            //Log.d(LOGTAG, "comparing $persistedUriString and $uriString")
            if (persistedUriString == uriString && list[i].isWritePermission && list[i].isReadPermission) {
                //Log.d(LOGTAG, "permission ok")
                return true
            }
        }
        return false
    }
登录后复制

注意事项

  • 用户体验: 尽可能清晰地向用户解释为什么你的应用需要访问外部存储的特定目录。
  • 权限管理: 妥善管理用户授予的目录访问权限。如果不再需要访问某个目录,请释放相应的权限。
  • 错误处理: 处理可能发生的异常,例如用户拒绝授权、目录不存在等。
  • 性能: 避免在主线程中执行耗时的文件操作。

总结

通过使用Storage Access Framework (SAF),你可以安全高效地在Android Scoped Storage环境下访问外部存储的特定文件夹。记住,尊重用户隐私是至关重要的,始终以用户为中心设计你的应用。本教程提供了一个基本的框架,你可以根据自己的需求进行扩展和定制。

以上就是Android Scoped Storage: 如何读取外部存储特定文件夹的文件的详细内容,更多请关注php中文网其它相关文章!

最佳 Windows 性能的顶级免费优化软件
最佳 Windows 性能的顶级免费优化软件

每个人都需要一台速度更快、更稳定的 PC。随着时间的推移,垃圾文件、旧注册表数据和不必要的后台进程会占用资源并降低性能。幸运的是,许多工具可以让 Windows 保持平稳运行。

下载
来源:php中文网
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn
最新问题
开源免费商场系统广告
热门教程
更多>
最新下载
更多>
网站特效
网站源码
网站素材
前端模板
关于我们 免责申明 举报中心 意见反馈 讲师合作 广告合作 最新更新 English
php中文网:公益在线php培训,帮助PHP学习者快速成长!
关注服务号 技术交流群
PHP中文网订阅号
每天精选资源文章推送
PHP中文网APP
随时随地碎片化学习

Copyright 2014-2025 https://www.php.cn/ All Rights Reserved | php.cn | 湘ICP备2023035733号