【问题标题】:Convert Image Name stored in MySql Database to Drawable in Android将存储在 MySql 数据库中的图像名称转换为 Android 中的 Drawable
【发布时间】:2018-10-02 04:29:32
【问题描述】:

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

“photo”字段是数据库中的图像字段,“about”是存储在可绘制文件夹中的图像名称,但我想要 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);
}

【问题讨论】:

  • 你要android get drawable by namegoogle搜索查询
  • 为什么你只需要drwable?您可以将图像设置为来自路径

标签: android mysql drawable


【解决方案1】:

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

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 
}

【讨论】:

    猜你喜欢
    • 2012-07-05
    • 2012-09-23
    • 1970-01-01
    • 2011-05-07
    • 2015-05-19
    • 2012-03-18
    • 1970-01-01
    • 2020-03-22
    • 1970-01-01
    相关资源
    最近更新 更多