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

Android :How to load image dynamically from server by its name from SQlite(Android:如何通过 SQlite 的名称从服务器动态加载图像)

本文介绍了Android:如何通过 SQlite 的名称从服务器动态加载图像的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是 android 新手,在显示基于 Sqlite 名称的服务器图像时遇到问题

I am new to android and I'm facing an issue while displaying image from server based its name from Sqlite

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

ie: I stored only image name (text) in SQLite database (column name images) and I want to load images from Server based on the sqlite image name the image want to display in imageview

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

In server I create a folder like Cars in that folder I store images with car names..but in sqlite I just add a carname in text format with .jpeg

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

I have two column names in my DB:

  • 首先是车名
  • 其次是汽车细节

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

When user selects the Car name, in next activity the user can see the Car details with images. Here I display Details, But I don't know How to display Car Images From Server

谢谢

这是我的代码:

@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 );
    }

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

Note:Image Field is the 4th Column http://i.stack.imgur.com/lqvOQ.png and in server i put image in www.server.com/cars/carnames.jpg

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

in sqlite i just paste the image name with .jpg ex:carnames.jpg

SqliteDbHelper

SqliteDbHelper

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 循环 例如,我有一个查询,它从我的 SQLite 数据库中加载一堆图像,这会显示名为鞋子的某个类别的子类别图像,因此我们有 的图像智能鞋休闲鞋等我传递了一个ID作为参数

If you want to load images from SQlite then I suggest you save the file/file location if the data is stored locally, for the showing of a single image on all image view it is because you are only loading a single image, you might want to put all your ids in an array and passing it to the db, the db query should also return an array/arraylist of image locations which you should load into your image views using a for loop e.g I have a query that loads a bunch of images from my SQLite database, this displays sub-category images of a certain category named shoes so we have images of smart shoes, Casual shoes and more I pass an an Id as parameter

  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;

}

在接收端,然后我横穿我的 araylist

on the receiving side I then transverse through my araylist

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

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

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

with this method you will set images to all your imageviews.

这篇关于Android:如何通过 SQlite 的名称从服务器动态加载图像的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

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

基础教程推荐