【问题标题】:How can I get Uri of a private Folder in Android?如何在 Android 中获取私有文件夹的 Uri?
【发布时间】:2025-12-06 20:05:01
【问题描述】:

我正在尝试制作带有图片库的 android 相机应用程序。捕获的图像保存到一个私有目录:Android/data/com.example.newcamera/files/pictures。

每当我使用 INTERNAL_CONTENT_URI 或 EXTERNAL_CONTENT_URI 作为 Uri 时,该应用程序都会将我手机的所有公共图片带到私人目录中。但我只需要那些有私人目录的人。我怎么才能得到它?请帮我。我的代码sn-p如下:

提前致谢。

    protected String doInBackground(String... args) {
        String xml = "";

        String path = null;
        String album = null;
        String timestamp = null;
        String countPhoto = null;
        Uri uriInternal = MediaStore.Images.Media.INTERNAL_CONTENT_URI;
        Uri uriExternal = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
        Uri myUri = Uri.fromFile(new File(getApplicationContext().getFilesDir().getAbsolutePath()));


        String[] projection = { MediaStore.MediaColumns.DATA,
                MediaStore.Images.Media.BUCKET_DISPLAY_NAME, MediaStore.MediaColumns.DATE_MODIFIED };
        Cursor cursorExternal = getContentResolver().query(uriExternal, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
                null, null);
        Cursor cursorInternal = getContentResolver().query(uriInternal, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
                null, null);
        Cursor myCursor = getContentResolver().query(myUri, projection, "_data IS NOT NULL) GROUP BY (bucket_display_name",
                null, null);

        Cursor cursor = new MergeCursor(new Cursor[]{cursorExternal, cursorInternal, myCursor});

        while (cursor.moveToNext()) {

            path = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATA));
            album = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.BUCKET_DISPLAY_NAME));
            timestamp = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.MediaColumns.DATE_MODIFIED));
            countPhoto = Function.getCount(getApplicationContext(), album);

            albumList.add(Function.mappingInbox(album, path, timestamp, Function.converToTime(timestamp), countPhoto));
        }
        cursor.close();
        Collections.sort(albumList, new MapComparator(Function.KEY_TIMESTAMP, "dsc")); // Arranging photo album by timestamp decending
        return xml;
    }

【问题讨论】:

  • 您只需要获取 URI 还是您知道 URI 并想获取该目录的内容?
  • 我需要 Uri 以及该目录的内容来制作图片库。 @Tomas Jablonskis。

标签: android cursor uri image-gallery photo-gallery


【解决方案1】:

您可以通过以下方式从特定文件夹中获取文件:

File folder = new File(Environment.getExternalStorageDirectory().toString() + "/Folder Name/");
folder.mkdirs();
File[] allFiles = folder.listFiles(new FilenameFilter() {
    public boolean accept(File dir, String name) {
        return (name.endsWith(".jpg") || name.endsWith(".jpeg") || name.endsWith(".png"));
    }
});

您可以通过Uri.fromFile(YOUR FILE)将文件路径转换为Uri

【讨论】:

  • 现在请看我编辑的代码。我正在尝试使用 Uri 从 getContentResolver() 获取光标。然后我使用了一些其他功能从光标获取路径以制作我的图片库。我的整个代码都是基于 sn-p 的。在这种情况下,我从应用程序的私有目录中获取所有图像。你能帮我解决这个问题吗?我不知道我的代码有什么问题。