-3

Taking example of viewpager or recyclerview in android we know that they can contain images as well.What i want to know is that how we can load images from database from server and put that images in view pager or recycler view.Upto know my understanding is that i have to make a database in server and put images in it ,now during splash screen download that images and after that put that images into view.Am i going right? Basically what i want to achieve is like shopping app which shows images which changes time to time ? how i can do same ?Do i should go with Rest API to download that images from server and then put in view also do i need to make sure that every time user opens app then it download image?

Vipin Dubey
  • 582
  • 1
  • 9
  • 26
  • Possible duplicate of [Android Download Image From URL and show in Imageview](https://stackoverflow.com/questions/12161137/android-download-image-from-url-and-show-in-imageview) – puya ars Oct 02 '18 at 11:40

1 Answers1

3

You can simply fetch .jpg URL from API and then load that image in your app by using Fresco library.

Check this out: Display images with Fresco

First add Fresco to your dependencies in app/build.gradle.

dependencies {
    implementation 'com.facebook.fresco:fresco:1.10.0'
}

Don't forget to add in your AndroidManifest.xml proper permission:

<uses-permission android:name="android.permission.INTERNET"/>

Initialize Fresco in class that extend Application class. You can do it in Activity, but it's better to do it just once in Application class.

Fresco.initialize(context);

Instead of ImageView use SimpleDraweeView like this:

<com.facebook.drawee.view.SimpleDraweeView
    android:id="@+id/sdvImage"
    android:layout_width="130dp"
    android:layout_height="130dp"
    fresco:placeholderImage="@drawable/myPlaceholderImage" />

Then just init your SimpleDraweeView object in Activity/ViewHolder/Fragment, parse Url string to Uri like this:

Uri imageUri = Uri.parse("https://i.imgur.com/tGbaZCY.jpg");

And you can set Uri to your SimpelDraweeView object this way:

draweeView.setImageURI(imageUri);

You can use Glide or Picasso as well. Find one that suit your needs by read this post:

Picasso v/s Imageloader v/s Fresco vs Glide

Da Artagnan
  • 1,109
  • 3
  • 17
  • 26