Android:如何通过 SQlite 的名称从服务器动态加载图像

2023-12-27

我是 android 新手,在根据 Sqlite 的名称显示来自服务器的图像时遇到问题

IE: 我只在 SQLite 数据库中存储图像名称(文本)(列名称图像),并且我想根据图像想要在 imageview 中显示的 sqlite 图像名称从服务器加载图像

在服务器中,我创建一个像 Cars 这样的文件夹,在该文件夹中我存储带有汽车名称的图像。但在 sqlite 中,我只是添加带有 .jpeg 的文本格式的汽车名称

我的数据库中有两个列名:

  • 第一个是汽车名称
  • 其次是汽车细节

当用户选择汽车名称时,在下一个活动中,用户可以看到带有图像的汽车详细信息。 这里我显示详细信息,但我不知道如何显示服务器中的汽车图像

Thanks

这是我的代码:

@Override
protected void onCreate(Bundle savedInstanceState) 
{


    super.onCreate(savedInstanceState);
    setContentView(R.layout.detail_activity);

    detailtext= (TextView) findViewById(R.id.detail);
    imageView= (ImageView) findViewById(R.id.images);

    Intent intent= getIntent();
    final String selectedData = intent.getStringExtra("selectedItem");
    actionBar.setTitle(selectedData);

    dbHelper = new SqlLiteDbHelper(this);
    try {
        dbHelper.openDataBase();
    } catch (SQLException e) {
        e.printStackTrace();
    }

    sqLiteDatabase = dbHelper.getReadableDatabase();
    cursor=dbHelper.getdetails(sqLiteDatabase, selectedData);

    if (cursor.moveToFirst()) {

    detailtext.setText(cursor.getString(0));
        String imagename = cursor.getString(1);
        String imageUrl = "http://your_server/car/" + imagename ;

        Picasso.with(this).load(imageUrl).into(imageView );
    }

注意:图像字段是第四列https://i.stack.imgur.com/lqvOQ.png https://i.stack.imgur.com/lqvOQ.png在服务器中,我将图像放入 www.server.com/cars/carnames.jpg

在 sqlite 中,我只需将图像名称粘贴到 .jpg ex:carnames.jpg

SQLite数据库助手

public Cursor getdetails(SQLiteDatabase db,String img)
    {
        db=this.getReadableDatabase();
        Cursor cursor;
        cursor=db.query("record",new String[]{DETAIL,IMAGES,ITEMNO + " as _id"},SUBCATEGORY + "='" +img+"'",null,null,null,null);
        return cursor;
    }

如果您想从 SQlite 加载图像,那么我建议您保存文件/文件位置(如果数据存储在本地),为了在所有图像视图上显示单个图像,这是因为您只加载单个图像,您可能想要要将所有 id 放入数组中并将其传递给数据库,数据库查询还应返回图像位置的数组/数组列表,您应使用以下命令将其加载到图像视图中:for loop例如,我有一个查询从我的 SQLite 数据库加载一堆图像,这显示了某个名为“鞋子”的类别的子类别图像,因此我们有以下图像smart shoes, Casual shoes还有更多我传递一个 Id 作为参数

  public ArrayList<CategoryItem> getAllSubCategories(int mtargetID) throws SQLException{



    ArrayList<CategoryItem> myshoes = new ArrayList<>();
    // Select All Query



    String sQuery = " SELECT "+Constant.CATEGORY_TB_ID+",  "+Constant.SUB_DESCRIPTION+
            ", "+Constant.SUB_IMAGEPATH+" FROM  "+Constant.CATEGORY_TABLE+
    " INNER JOIN "+Constant.SUB_CATEGORY_TABLE+" ON "+Constant.CATEGORY_TB_ID +" = " +Constant.SUB_CATEGORY_FK
   + " WHERE "+Constant.CATEGORY_TB_ID +" = ?";

    String[] args = new String[1];
    args[0] = String.valueOf(mtargetID);


    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(sQuery, args);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            CategoryItem myShoesItem = new CategoryItem();

            //my shoe image path on external storage
            myShoeItem.setmCategoryImgPath(cursor.getString(2));
            //i add my image paths to my array list
            myshoes.add(myShoeItem);
        } while (cursor.moveToNext());
    }

    // return my arraylist for display into my imageview
    return mshoes;

}

在穿过我的数组列表的横向接收端

   for(int i = 0; i <getAllSubCategories.size(); i++ )
  {

       imageView.setImageUri(getAllSubCategories.get(i).getmCategoryImgPath())
   }

使用此方法,您可以将图像设置为所有图像视图。

本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

Android:如何通过 SQlite 的名称从服务器动态加载图像 的相关文章

随机推荐