0

I have an android app that generates 3 random images from my external storage. The random image I get is then set to an Imageview which is working. The problem is that I do not want to view every picture but just get random images from one specific folder in the external folders . How do I change the code to get this?

public class SuggestedOutfit extends AppCompatActivity{

public ImageView shirts;
public ImageView pants;
public ImageView shoes;
Button reload;
private Bitmap currentBitmap = null;
String[] projection = new String[]{
        MediaStore.Images.Media.DATA,
};


@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.suggested_outfit);
    shirts = (ImageView)findViewById(R.id.shirtImage);
    pants = (ImageView)findViewById(R.id.pantsImage);
    shoes = (ImageView)findViewById(R.id.shoesImage);

    shirts.setImageBitmap(getImage());
    pants.setImageBitmap(getImage());
    shoes.setImageBitmap(getImage());

    reload = (Button)findViewById(R.id.getNewOutfit);
    reload.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            finish();
            startActivity(getIntent());
        }
    });
}


public Bitmap getImage () {
    Uri images = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Cursor cur = getContentResolver().query(images,
            projection,"", null, ""
    );
    final ArrayList<String> imagesPath = new ArrayList<String>();
    if (cur.moveToFirst()) {

        int dataColumn = cur.getColumnIndex(
                MediaStore.Images.Media.DATA);
        do {
            imagesPath.add(cur.getString(dataColumn));
        } while (cur.moveToNext());
    }
    cur.close();
    final Random random = new Random();
    final int count = imagesPath.size();
    int number = random.nextInt(count);
    String path = imagesPath.get(number);
    currentBitmap = BitmapFactory.decodeFile(path);

    return currentBitmap;
}

}
Ashley
  • 225
  • 1
  • 8
  • http://stackoverflow.com/questions/5039779/displaying-images-from-a-specific-folder-on-the-sdcard-using-a-gridview – Breavyn Dec 07 '15 at 22:48
  • I don't understand the question. From what I can tell from your code you're doing the right thing. Example, 1. Get all image URIs. 2. Pick one at random. 3. Repeat step 2 the number of times required. – Squonk Dec 07 '15 at 22:49
  • @Squonk Yes but I am getting all the pictures in the external folder. I just want to get random images from "/Pictures/Wardrobe" in the external folder only but I can't figure out how to change the code to get this. – Ashley Dec 07 '15 at 22:54
  • the first three answers of my link solve your problem – Breavyn Dec 07 '15 at 22:55
  • @ColinGillespie yes that question may help me to display the whole folder , which i need to do too, but for this question I just need to select 3 random images and display. Not sure how to change that code – Ashley Dec 07 '15 at 23:02

1 Answers1

1

There're 2 ways I can think of and you got it almost everything there:

  1. just in this one line:

    imagesPath.add(cur.getString(dataColumn));

you have to add a filter to only add to the array if the value is what u want. For example:

do {
   String path = cur.getString(dataColumn);
   if(path.contains("/wardrobe/")){
      imagesPath.add(path);
   }
} while (cur.moveToNext());

you can maybe make the if a bit more sophisticate to make sure not to hit any weird edge case, but that's about it. Only images from the place u want get added to the array and then you pick a random one.

  1. Alternatively you could also pass an appropriate query parameter to getContentResolver().query instead of simply passing "",

something like:

 getContenteResolver().query(
   images,
   projection,
   MediaStore.Images.Media.DATA + " contains /wardrobe/",
   null,
   "");

ps: (this SQL statement above it's just an example, it will not work because it's not properly escaped, it's only a pointer to the right direction in case u want to implement it in the SQL query)

but personally I would use the 1st method.

Budius
  • 39,391
  • 16
  • 102
  • 144