Android-获取对SD卡上任何文件路径具有写访问权限的DocumentFile(已获得所有SD卡许可) [英] Android - get DocumentFile with write access for any file path on sd card (having allready gained sd card permission)

查看:717
本文介绍了Android-获取对SD卡上任何文件路径具有写访问权限的DocumentFile(已获得所有SD卡许可)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在我的应用中,我使用以下意图获得了SD卡的写许可.如果用户从系统文件浏览器中选择sd卡文件夹,则我具有sd卡写访问权限.

In my app I gain sd card write permission using the following intent. If the user selects the sd card folder from the system file explorer then I have sd card write access.

Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
intent.putExtra("android.content.extra.SHOW_ADVANCED", true);
startActivityForResult(intent, 42);

之后,我可以使用DocumentFile类来修改SD卡中的文件.但是我在获取随机文件路径的DocumentFile时遇到问题.

After that I am able to modify files in th sd card using the DocumentFile class. But I'm having problem getting a DocumentFile for a random file path.

Document.fromFile(new File(path));
Document.fromSingleUri(Uri.fromFile(new File(path)));

都返回一个DocumentFile对象,该对象在.canWrite()上返回false.即使我已经拥有sd卡许可.

both return a DocumentFile object that returns false on .canWrite(). Even though I allready have sd card permission.

因此,我编写了问题末尾发布的方法,以获得在.canWrite()上返回true的DocumentFile.但这很慢...而且感觉很不对劲!必须有一个更好的方法来做到这一点.我还写了一个方法,该方法返回与

So I wrote the method posted in the end of my question to get a DocumentFile that returns true on .canWrite(). But this is slow...And also feels very wrong! There has to be a better way to do this. I also wrote a method that returns the same string as

String docFileUriString = docFile.getUri().toString(); 

对于任何文件,其中docFile是以下方法返回的DocumentFile.但是

for any file, where docFile is the DocumentFile that is returned by the method below. But

DocumentFile.fromTreeUri(Uri.parse(docFileUriString ));

返回指向SD卡根目录的DocumentFile而不是DocumentFile路径.真是奇怪.有人可以提出更优雅的解决方案吗?

returns a DocumentFile that points to the root of the sd card instead of the DocumentFile path. Which is just weird. Can someone suggest a more elegant solution?

public static DocumentFile getDocumentFileIfAllowedToWrite(File file, Context con){ 

    List<UriPermission> permissionUris = con.getContentResolver().getPersistedUriPermissions();

    for(UriPermission permissionUri:permissionUris){

        Uri treeUri = permissionUri.getUri();
        DocumentFile rootDocFile = DocumentFile.fromTreeUri(con, treeUri);
        String rootDocFilePath = FileUtil.getFullPathFromTreeUri(treeUri, con);

        if(file.getAbsolutePath().startsWith(rootDocFilePath)){

            ArrayList<String> pathInRootDocParts = new ArrayList<String>();
            while(!rootDocFilePath.equals(file.getAbsolutePath())){
                pathInRootDocParts.add(file.getName());
                file = file.getParentFile();
            } 

            DocumentFile docFile = null;  

            if(pathInRootDocParts.size()==0){ 
                docFile = DocumentFile.fromTreeUri(con, rootDocFile.getUri()); 
            }
            else{
                for(int i=pathInRootDocParts.size()-1;i>=0;i--){
                    if(docFile==null){docFile = rootDocFile.findFile(pathInRootDocParts.get(i));}
                    else{docFile = docFile.findFile(pathInRootDocParts.get(i)); }  
                }
            }
            if(docFile!=null && docFile.canWrite()){ 
                return docFile; 
            }else{
                return null;
            }

        }
    }
    return null; 
}

推荐答案

响应@AndiMar(3月21日),我只是检查它是否存在于内部存储中,是否无需担心.

to respond to @AndiMar (21 Mar), I just check if it exists on internal storage, if it does no need to worry.

       try {
        if (sourcefile.toString().contains(Environment.getExternalStorageDirectory().toString())) {
            if (sourcefile.exists()) {
                sourcefile.delete();
            }
        } else {
            FileUtilities fio = new FileUtilities();
            DocumentFile filetodelete = fio.getDocumentFileIfAllowedToWrite(sourcefile, context);
            if (filetodelete.exists()) {
                filetodelete.delete();
            }
        }

    } catch (Exception e) {
        e.printStackTrace();

    }

这篇关于Android-获取对SD卡上任何文件路径具有写访问权限的DocumentFile(已获得所有SD卡许可)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆