简体   繁体   English

在Android上设置图像背景

[英]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. 所以我有一个程序,我希望这个程序每n分钟将线性布局的背景更改为人物库中的新图像。 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). 冒着发出超级菜鸟的风险,我该如何获得这条路径并将其变成一个可用于setBackgrounDrawable(Drawable d)的drawable。 Am I thinking about this the wrong way? 我是否以错误的方式思考这个问题?

Thanks 谢谢

You can just pass the path to createFromPath(String path) . 您只需将路径传递给createFromPath(String path)即可 This should give you back a drawable resource you can use. 这应该会为您提供可以使用的可绘制资源。

另一种方法是使用

.setBackgroundBitmap(BitmapFactory.decodeFile(path));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM