samedi 5 décembre 2015

Get random image from external storage - android

I have an app that generates 3 random images. The problem I am having is that the random images are coming from my gallery but I want the images to come from my external storage pictures. How do you I change the code so my images come from there instead?

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

}

Also, I have the images being set to an ImageView with an exact width and height but sometimes the images are different sizes. Why are my images changing in layout and showing up as different sizes even though the imageView has a set width and height?




Aucun commentaire:

Enregistrer un commentaire