如何在 Android 中获取私有文件夹的 Uri?

问题描述 投票:0回答:1

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

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

提前致谢。

    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;
    }
android uri image-gallery android-cursor photo-gallery
1个回答
0
投票

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

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
© www.soinside.com 2019 - 2024. All rights reserved.