在Android中将存储在MySql数据库中的图像名称转换为Drawable

时间:2018-10-02 04:29:32

标签: android mysql drawable

我有这段代码。我的图像名称存储在MySql数据库中,现在我想将其转换为可绘制的横幅横幅目的。

“照片”字段是数据库中的图像字段,“约”是存储在drawable文件夹中的图像名称,但我希望从R.drawable中的数据库获取图片。“ images_stored_in_db”。

for (int i=0;i<array.length();i++)
{
    JSONObject object = array.getJSONObject(i);
    String name = object.getString("name");
    String email = object.getString("email");
    String pass = object.getString("password");
    String photo = Connection.API + object.get("photo");


    banners.add(new DrawableBanner(R.drawable.about));


    ShowItem item = new ShowItem(name,email,pass,photo);
    RegisterContent.ITEMS.add(item);
}

1 个答案:

答案 0 :(得分:0)

如果您有图像名称,则意味着该图像存在于设备上的某个位置-对吗? 如果是这样的话。

JSONObject object = array.getJSONObject(i);
String name = object.getString("name");
String email = object.getString("email");
String pass = object.getString("password");
String photo = Connection.API + object.get("photo");

File image = new  File(photo);

//check if the image really exsist
if(image.exists()){

    Bitmap bm = BitmapFactory.decodeFile(image.getAbsolutePath());

    //.... now do whatever you want to do with it
    //you can use setImageBitmap(bm); to display the image on an ImageView 
}