lundi 7 décembre 2015

Get random images from specific folder

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

}




Aucun commentaire:

Enregistrer un commentaire