4

So i have this code for getting images with mediastore :

int dataIndex = mMediaStoreCursor.getColumnIndex(MediaStore.Files.FileColumns.DATA );

mMediaStoreCursor.moveToPosition(position);
String dataString = mMediaStoreCursor.getString(dataIndex);
Uri mediaUri = Uri.parse("file://" + dataString);
return mediaUri;

This good gets all the images in the pictures folder, i would like to change that to get all the images in a specific folder, which will be passed in as a string. e.g { android/picture/specificfolder/}

Youcef LAIDANI
  • 55,661
  • 15
  • 90
  • 140
Ibrahim Momoh
  • 51
  • 1
  • 4
  • Possible duplicate of [Uri.parse("file://" + ???); to access specific folder](http://stackoverflow.com/questions/43335103/uri-parsefile-to-access-specific-folder) – fadysi92 Apr 21 '17 at 02:12

3 Answers3

5
val DIRECTORY_NAME = "%your_folder_name%"

val selection = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q)
    MediaStore.MediaColumns.RELATIVE_PATH + " like ? "
else MediaStore.Images.Media.DATA + " like ? "

val selectionArgs = arrayOf(APP_RESOURCE_DIRECTORY_NAME)

val cursor = context.contentResolver.query(
        MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
        null,
        selection,
        selectionArgs,
        null)
Vinod Kamble
  • 201
  • 3
  • 4
3

Check this post

It advice you to use cursor and pass the folder needed in the projection.

the relevant answer from teh post:

mediaCursor = getContentResolver().query( MediaStore.Files.getContentUri("external"),
                null,
                MediaStore.Images.Media.DATA + " like ? ",
                new String[] {"%YOUR_FOLDER_NAME%"},
                null);
Community
  • 1
  • 1
yotam hadas
  • 702
  • 3
  • 14
1

You can use following method -

public List<String> getFromSdcard()
{
    ArrayList<String> imagePaths = new ArrayList<String>();// list of file paths
    File[] listFile;
    File file= new 

    File(android.os.Environment.getExternalStorageDirectory(),"android/picture/specificfolder/");

    if (file.isDirectory())
    {
        listFile = file.listFiles();


        for (int i = 0; i < listFile.length; i++)
        {

            imagePaths.add(listFile[i].getAbsolutePath());

        }
    }
    return imagePaths;
}

to retreive the images use

List<String> sample = getFromSdcard();

    for(int i=0; i<sample.size() ; i++){
      final Uri image = Uri.parse("file://"+sample(i).toString());
    }
rafsanahmad007
  • 23,683
  • 6
  • 47
  • 62
tahsinRupam
  • 6,325
  • 1
  • 18
  • 34