Jackie
Jackie

Reputation: 23477

Setting Background from Image on Android

So I have a program, this I want this program to change the background on the linear layout to a new image in the person's gallery every n minutes. Currently I am getting the path using the following...

public String getCurrentImageLocation(){
    ContentResolver contentResolver = getContentResolver();
    Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
    Cursor c = contentResolver.query(uri, null, null, null, null);
    Integer test2 = c.getCount();
    Random randomGenerator = new Random();
    Integer randomNumber = randomGenerator.nextInt(test2.intValue());
    c.moveToPosition(randomNumber);
    int rowId = c.getColumnIndex(MediaStore.Images.Media.DATA);
    return c.getString(rowId);
}

At the risk of sounding super noob, how do I get this path and turn it into a drawable that I can use for setBackgrounDrawable(Drawable d). Am I thinking about this the wrong way?

Thanks

Upvotes: 0

Views: 561

Answers (2)

Michell Bak
Michell Bak

Reputation: 13242

Another way to do it is by using

.setBackgroundBitmap(BitmapFactory.decodeFile(path));

Upvotes: 0

Bobbake4
Bobbake4

Reputation: 24847

You can just pass the path to createFromPath(String path). This should give you back a drawable resource you can use.

Upvotes: 2

Related Questions