有 Java 编程相关的问题?

你可以在下面搜索框中键入要查询的问题!

java如何在Android Studio中使用Uri访问“Android/data/”中的文件夹

我的问题是,我无法访问“Android/data/”中的文件夹,但我确实可以访问“Android/data/”文件夹,方法是向用户请求一个Uri来确认它

这是代码,无论我尝试什么,它总是将文件保存到“Android/data/”而不是“Android/data/com.tencent.ig/”

public void copy(File src, Uri dst) throws IOException {

    Uri treeUri = getPersistedUriPermissions().getUri();
    Uri subDir = DocumentsContract.buildDocumentUriUsingTree(treeUri,DocumentsContract.getTreeDocumentId(treeUri)+"/com.tencent.ig/");
    Toast.makeText(MainActivity.this,subDir.toString(),Toast.LENGTH_LONG).show();
    DocumentFile file = DocumentFile.fromTreeUri(this,subDir);


    try (InputStream in = new FileInputStream(src)) {
        try (OutputStream out = getContentResolver().openOutputStream(file.createFile("","Active.sav").getUri(),"w")){
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0){
                out.write(buf,0,len);
            }
        }
    }
}

共 (0) 个答案