SAF-尝试再次创建现有文件夹/文件会产生一个新的文件夹/文件 [英] SAF - Attempting to create again an existing folder/file yields a new folder/file

查看:360
本文介绍了SAF-尝试再次创建现有文件夹/文件会产生一个新的文件夹/文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

SAF是否打算在将来取代外部目录中的文件处理?

Is SAF intended to replace File handling in external directories, in a certain future?

我希望最终不是这样,但是我正在预先测试SAF本身.出于其他原因,它也会很有用.

I hope it is not so in the end, but I am testing SAF itself in advance. It would be useful for other reasons too.

假设我正在使用此方法:

Let's say I am using this method:

static public DocumentFile createFolderInFolder(Activity activity,String            parentFolderUriString,String folderName)
{
DocumentFile result=null;
ContentResolver contentResolver;
contentResolver = activity.getContentResolver();
Uri parentFolderUri=null;
Uri oldParentUri = Uri.parse(parentFolderUriString);
String id = DocumentsContract.getTreeDocumentId(oldParentUri);
parentFolderUri= DocumentsContract.buildChildDocumentsUriUsingTree(oldParentUri,id);
DocumentFile parentFolder = DocumentFile.fromTreeUri(activity, parentFolderUri);
result=parentFolder.createDirectory(folderName);
return result;
}

它将在另一个目录中创建一个目录.

It creates a directory inside another directory.

尝试创建现有文件夹(例如"folderName")时,将创建一个具有类似名称"folderName(1)"的新文件夹.

When attempting to create an existing folder, for instance "folderName", a new folder is created with a similar name "folderName (1)".

是否有避免这种情况的方法,所以如果存在,则不会创建新文件夹? (与文件相同)

Is there a way to avoid this, so that a new folder is not created if existing? (And the same for files)

推荐答案

parentFolderUri =文件合同 .buildChildDocumentsUriUsingTree(oldParentUri,id);

parentFolderUri= DocumentsContract .buildChildDocumentsUriUsingTree(oldParentUri,id);

用于在正确的目录中创建,该目录应为 .buildChildDocumentsUriUsingTree(oldParentUri,id +"/" + folderName);

For creation in the right directory that should be .buildChildDocumentsUriUsingTree(oldParentUri,id + "/" + folderName);

此外,您应该开始编写两个功能.一个用于检查目录是否存在,一个用于检查文件是否存在.

Further you should start programming two functions. One to check if a directory exists and one for file existence.

要检查目录是否存在,可以查询(directory + dirName)uri的内容提供程序.如果出现异常,则目录不存在.

To check if a directory exists you would query() the content provider for the (directory+dirName) uri. If you get exceptions the directory does not exist.

对于文件存在,请尝试在文件uri上使用内容解析器的OpenInputStream.如果打开,则文件存在.如果出现异常,则文件不存在.

For file existence try to use the OpenInputStream of the content resolver on the file uri. If it opens the file exists. If you get exceptions the file does not exist.

更新: 如果要检查通过ACTION_OPEN_DOCUMENT_TREE获得的路径中是否存在文件夹,可以使用:

Update: If you want to check if a folder exists in a path you obtained with ACTION_OPEN_DOCUMENT_TREE you can use:

static public boolean folderExists(Activity activity, String rootPath, String folderName)
{
    return DocumentFile.fromTreeUri(activity, Uri.parse(rootPath)).findFile(folderName)!=null;

}

这篇关于SAF-尝试再次创建现有文件夹/文件会产生一个新的文件夹/文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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