开发者

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.ne开发者_JS百科xtInt(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


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


Another way to do it is by using

.setBackgroundBitmap(BitmapFactory.decodeFile(path));
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜