android - 如何使用 buildChildDocumentsUriUsingTree 过滤查询结果

标签 android file save sd-card

我想将文件保存到 SD 卡文件夹中。

而且我不能在我的项目中使用 V4 支持。

所以我调用:

Intent itent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
itent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
itent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
startActivityForResult(itent, requestCodeTree);

然后在 onActivityResult 上,我有:

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
    super.onActivityResult(requestCode, resultCode, intent);

    if (resultCode == RESULT_OK) {
        switch(requestCode) {
            case requestCodeTree:
                saveFile(intent.getData());
                break;
        }
    }
}

保存文件的代码是:

   private void saveFile(Uri data) {
        ContentResolver contentResolver = context.getContentResolver();

        InputStream in = null;
        OutputStream out = null;

        try {

            // Problems start here ************************
            Uri toUriFile= getUriBackupFile(context, data);
            // ********************************************

            if (toUriFile==null) {
                Uri toUriFolder = DocumentsContract.buildDocumentUriUsingTree(data, DocumentsContract.getTreeDocumentId(data));
                toUriFile = DocumentsContract.createDocument(contentResolver, toUriFolder, "", backupName);
            }

            out = contentResolver.openOutputStream(toUriFile);
            in = new FileInputStream(fromFile);

            byte[] buffer = new byte[1024];
            int read;
            while ((read = in.read(buffer)) != -1) {
                out.write(buffer, 0, read);
            }
            in.close();
            // write the output file (the file is now copied)
            out.flush();
            out.close();

        } catch (FileNotFoundException e) {
            Log.e(TAG, "Failed", e);
        } catch (Exception e) {
            Log.e(TAG, "Failed", e);
        }
    }

到目前为止一切顺利。

当我调用 getUriBackupFile 获取目标文件的 uri 时,问题开始了。

为此,我使用 buildChildDocumentsUriUsingTree 查询 ContentResolver 并尝试过滤 DocumentsContract.Document.COLUMN_DISPLAY_NAME 与我的文件显示匹配的结果名称,像这样:

private static Uri getUriBackupFile(Context context, Uri treeUri) {
        final ContentResolver resolver = context.getContentResolver();

        final Uri childrenUri = DocumentsContract.buildChildDocumentsUriUsingTree(
                treeUri,
                DocumentsContract.getTreeDocumentId(treeUri));

        Cursor c = null;
        try {
            String[] projections = new String[] {
                    DocumentsContract.Document.COLUMN_DOCUMENT_ID,
                    DocumentsContract.Document.COLUMN_DISPLAY_NAME};

            // this line doesn't seem to have any effect !
        String selection = DocumentsContract.Document.COLUMN_DISPLAY_NAME + " = '" + backupName + "' ";
            // *************************************************************************

            c = resolver.query(childrenUri, projections, selection, null, null);

            if (c!=null && c.moveToFirst()) {
            // Here I expect to have c.getCount() == 1 or == 0
            // But actually c.getCount() == [Number of children in the treeUri] regardless of the selection
                String documentId = c.getString(0);
                Uri documentUri = DocumentsContract.buildDocumentUriUsingTree(treeUri,
                        documentId);
                return documentUri;
            }

        } catch (Exception e) {
            Log.w(TAG, "Failed query: " + e);
        } finally {
            if (c!=null) c.close();
        }

        return null;
    }

但查询总是返回 treeUri 的所有子项,无论选择如何。因此,选择似乎没有效果。

我总是可以循环遍历所有结果,但如果所选文件夹包含大量文件,则对性能不利。

所以我的问题是:

  1. 如何过滤查询结果?
  2. 这是将文件保存到 SD 卡路径的正确方法吗?

最佳答案

关于android - 如何使用 buildChildDocumentsUriUsingTree 过滤查询结果,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52770188/

相关文章:

android - 设置一个 spannable 字符串不适用于简单的 Textview

file - 减少 FORTRAN 中输出文件的大小

linux - 文件系统 : Kernel calls vs. 缓存

delphi - 我自己的存储和检索使用 Delphi 文本 DFM 格式的生命周期很长

Python 创建保存按钮,将编辑后的版本保存到同一文件(不是另存为)

java - android应用程序 Activity 中的保存按钮?

java - 为什么我的 JsonObjectRequest 不工作?

java - 应用程序崩溃引发权限拒绝异常 :reading com. android.providers.media.MediaProvider

java - IO文件属性

android - 将 Android App Bundle 上传到 Google Play 控制台 - key 签名错误