Android :How to load image dynamically from server

2020-06-17 06:25发布

问题:

I am new to android and I'm facing an issue while displaying image from server based its name from 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

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:

  • first is Car name
  • Second is Car Detail

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

Thanks

Here is my Code:

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

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

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

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

回答1:

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;

}

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.



回答2:

You have the path "url" request to the server? like this:

http://server_ip/api/images?imageName="The name you image here"

Then do you need use some lib to load image from server, see this:

Android Picasso

So, something like this:

Picasso.with(context).load("http://server_ip/api/images?imageName="The name you image here.png/jpeg").into(imageView);


回答3:

You're saying that in your server, you've created a directory called cars/ and all images are in jpeg format and also with same file name as their real names. If I'm correct, so your can use codes below.

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

// This a image declared in XML for displaying car image
ImageView carImageView = (ImageView) findViewById(R.id.carImage);    

// URL of this car's image
String imageUrl = "http://your_server_address/cars/" + carName + ".jpeg";

// Use Picasso library to load that image
Picasso.with(this).load(imageUrl).into(carImageView);

Note: Picasso is a fearure rich library for fetching images from network in Android.