Android: how to dynamically download an image from a server by its name from SQlite

I am new to Android and am facing a problem when displaying an image from a server based on its name from Sqlite

namely: I only saved the image name (text) in the SQLite database (column name images), and I want to download images from the server based on the sqlite image name that the image should display in imageview

On the server, I create a folder such as "Cars" in this folder, I store images with the names of the cars .. but in sqlite I just add carname in text format with .jpeg

I have two column names in my database:

  • car name first
  • Second - Car Detail

When the user selects the name of the car, in the next step the user can see the details of the car with images. Here I display the details, but I do not know how to display car images from the 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. The image field is the 4th column of http://i.stack.imgur.com/lqvOQ.png and on the server I put the 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; } 
+6
android sqlite android-sqlite imageview
source share
3 answers

If you want to load images from SQlite, I suggest you keep the file / file location if the data is stored locally, to display one image in all images, this is because you upload only one image, you may want to put all your identifiers in an array and pass it in db, the db request should also return an array / arraylist of image locations that you should load into your image views using for loop , for example, I have a request that loads a bunch of images from my database SQLite data, it displays images of subcategories of a certain category named shoes, so we have images of smart shoes , Casual shoes and more. I pass the identifier as a 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 across across my aralist

  for(int i = 0; i <getAllSubCategories.size(); i++ ) { imageView.setImageUri(getAllSubCategories.get(i).getmCategoryImgPath()) } 

Using this method, you set images for all of your images.

+4
source share

Do you have a url request to the server? eg:

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

Then you need to use some library to download the image from the 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); 
+1
source share

You say that on your server you created a directory called cars/ , and all the images are in jpeg format, as well as with the same file name as their real names. If I am right, so you can use the 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 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 rich library for receiving images from the network in Android.

+1
source share

All Articles