首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何在Android中获取私有文件夹的Uri?

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

Stack Overflow用户
提问于 2018-08-01 13:03:48
回答 1查看 1.2K关注 0票数 2

我正在尝试制作一个具有图片库的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;
    }
EN

回答 1

Stack Overflow用户

发布于 2018-08-01 13:38:50

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

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

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51625655

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档