3

I have such task: need to download images from the internet and set them as view's background every 15 minutes (user sets the period). I have done something like that:

I have JobService, it downloads images and saves them as files to created directory. After it send broadcast, receiver takes it and sets file names to it's listener. Listener - my class LauncherApplication extends Application, it is created before all activities, I save it's instance and thus I can load images from files in every part of program.I do it with such AsyncTask, execute it if necessary in OnResume() :

public class BackgroundImageAsyncChanger extends AsyncTask<String, Void ,Drawable> {

    private int pictureNumber;
    private View backgroundView;
    private Context context;

    public BackgroundImageAsyncChanger(View backgroundView, Context context, int pictureNumber) {
        this.backgroundView = backgroundView;
        this.context = context;
        this.pictureNumber = pictureNumber;
    }

    @Override
    protected Drawable doInBackground(String ... imageFilesNames) {
        final int index = pictureNumber;
        final Bitmap bitmap = ImageFileOperator.getInstance().loadImage(context, imageFilesNames[index]);
        final Drawable drawable = new BitmapDrawable(context.getResources(), bitmap);
        return drawable;
    }

    @Override
    protected void onPostExecute(Drawable backgroundImage) {
        backgroundView.setBackground(backgroundImage);
    }
}

And so there is the problem. It's takes time to download images from the Internet, and when they are downloaded after the Activity/Fragment OnResume() , it doesn't change background. How I can implement it better?

Thanks every one for answers!

Komal12
  • 3,340
  • 4
  • 16
  • 25
Ening
  • 455
  • 3
  • 19

4 Answers4

1

Downloading every image can lead to memory leak and its not a good idea.

You can use Glide

Here is the working example:

        Glide.with(context)
                .load(imageUrl) // or URI/path
                .placeholder(placeholder)
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .priority(Priority.IMMEDIATE)
                .error(placeholder)
                .skipMemoryCache(false)
                .dontAnimate()
                .listener(listener) // you can skip this if do not want to handle callback
                .into(imageView); //imageview to set thumbnail to.

Hope it will help you

Vishal Chhodwani
  • 2,567
  • 5
  • 27
  • 40
1

Its better from downloading the image. Use Picasso or Glide

picasso is simple to use

Picasso.with(context)
    .load(url)
    .placeholder(R.drawable.user_placeholder)
    .error(R.drawable.user_placeholder_error)
    .into(imageView);

Glide

Glide.with(mContext).load(imgUrl)
                .thumbnail(0.5f)
                .crossFade()
                .diskCacheStrategy(DiskCacheStrategy.ALL)
                .into(imageView);

in both the case you not need to worry about downloading the image.As soon as it get load it automatically project on image view. And automatically cache management.

/*********************************************************/

since every 15 min your job scheduler take url from server or whatever so suppose you have a function call

    private void jobScheduler(){
    new url generates here
    call the function to load image and send the url as paramater
         loadImages(url);
      }

here you load the url in same pitcure again and again after every 15 min

 private void loadImages(String url){
    Picasso.with(context)
        .load(url)
        .placeholder(R.drawable.user_placeholder)
        .error(R.drawable.user_placeholder_error)
        .into(imageView);
    }
007
  • 516
  • 1
  • 5
  • 17
0

Instead of downloading image,
you can directly load image using third party like picasso/glide.

Picasso

Picasso.with(mContext)
    .load(imageUrl)
    .placeholder(R.drawable.img_placeholder)
    .error(R.drawable.img_error)
    .into(mImageView);

Glide

Glide.with(mContext)
    .load(imageUrl)
    .thumbnail(0.5f)
    .crossFade()
    .skipMemoryCache(false)
    .diskCacheStrategy(DiskCacheStrategy.ALL)
    .listener(mImagelistener)//optional
    into(mImageView);

To upgrade images every specific interval,
You can set repeating alarm to send specific broadcast at some interval.
refer: Set Repeating Alarm Every Day at Specific time In Android
or
you can timer/handler class to get event on specific interval.
(Use based on your app structure)

bhumik
  • 231
  • 2
  • 11
0

I have developed a similar service to configure wallpapers, the schedule task is managing by a "job service"

        MyService myService;
Handler myHandler = new Handler(){
    @Override
    public void handleMessage(Message msg){
        myService = (MyService) msg.obj;
        myService.setUICallback(YourActivity.this);
    }
};

myServiceComponent = new ComponentName(this, MyService.class);

settings:

                JobInfo.Builder builder = new JobInfo.Builder(0, myServiceComponent);
            //builder.setRequiresCharging(true);
            //builder.setRequiresDeviceIdle(true);
            builder.setExtras(bundle);//if you need
            builder.setRequiredNetworkType(JobInfo.NETWORK_TYPE_UNMETERED);//only wifi
            builder.setPeriodic(900000);//15 min, dont try set less
            builder.setPersisted(true);

            myService.scheduleJob(builder.build());

and tasks in a jobservice class

public class MyService extends JobService{}

check this tutorial for jobservice

spam:you can see de live project here

Dario
  • 123
  • 10