kcpaul
kcpaul

Reputation: 41

How can I get Uri of a private Folder in Android?

I am trying to make an android camera app with image gallery. The images captured are saved to a private directory: Android/data/com.example.newcamera/files/pictures.

Whenever I am using INTERNAL_CONTENT_URI or, EXTERNAL_CONTENT_URI as Uri, The app is bringing all the public pictures of my phone but not the one in the private directory. But I need only those with private directory. How can I get it? Please help me. My code snippet is as follows:

Thanks in advance.

    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;
    }

Upvotes: 4

Views: 1785

Answers (1)

user3737328
user3737328

Reputation: 19

You can fetch your files from particular folder by:

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"));
    }
});

You can convert file path to Uri by Uri.fromFile(YOUR FILE)

Upvotes: -1

Related Questions