I am getting a list of image url from server whose count is approximately in between 400-500. How can i download this image in background into local folder of the device ?
SO far i have run a foreground service in which i am using ExecutorService to run a thread. My service code is below
public class SaveImageService extends Service {
private Context context;
public static final String NOTIFICATION_CHANNEL_ID = "10001";
public SaveImageService() {
}
@Override
public void onCreate() {
super.onCreate();
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O)
startMyOwnForeground();
else
startForeground(1, new Notification());
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
context = this;
super.onStartCommand(intent, flags, startId);
List<Callable<String>> saveDataThreads = new ArrayList<>();
SaveTaskImage saveTaskImage = new SaveTaskImage(this);
saveDataThreads.add(saveTaskImage);
ExecutorService executor = Executors.newFixedThreadPool(saveDataThreads.size());
try {
List<Future<String>> aaa = executor.invokeAll(saveDataThreads);
} catch (InterruptedException e) {
e.printStackTrace();
}
executor.shutdown();
if (executor.isShutdown()) {
stopForeground(true);
stopSelf();
}
return START_STICKY;
}
@Override
public IBinder onBind(Intent intent) {
// TODO: Return the communication channel to the service.
throw new UnsupportedOperationException("Not yet implemented");
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void startMyOwnForeground() {
String NOTIFICATION_CHANNEL_ID = "com.example.simpleapp";
String channelName = "My Background Service";
NotificationChannel chan = new NotificationChannel(NOTIFICATION_CHANNEL_ID, channelName, NotificationManager.IMPORTANCE_NONE);
chan.setLightColor(Color.BLUE);
chan.setLockscreenVisibility(Notification.VISIBILITY_PRIVATE);
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
manager.createNotificationChannel(chan);
NotificationCompat.Builder notificationBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_ID);
Notification notification = notificationBuilder.setOngoing(true)
.setSmallIcon(R.mipmap.talentify_logo_red)
.setContentTitle("App is running in background")
.setPriority(NotificationManager.IMPORTANCE_MIN)
.setCategory(Notification.CATEGORY_SERVICE)
.build();
startForeground(2, notification);
}
}
In SaveTask thread i triggering a sync task as below
new SaveImageAsync ("file_path").executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR,url);
Below is my async task code :
public class SaveImageAsync extends AsyncTask<String, Void, String> {
private MediaSaver mediaSaver;
public SaveImageAsync(MediaSaver mediaSaver){
this.mediaSaver = mediaSaver;
}
@Override
protected String doInBackground(String... params) {
Bitmap bmp = null;
try{
System.out.println("Save image url --> "+params[0].replaceAll(" ", "%20"));
URL url = new URL(params[0].replaceAll(" ", "%20"));
InputStream fileInputStream = url.openStream();
if(fileInputStream != null) {
mediaSaver.save(fileInputStream);
System.out.println("Saving image --> ");
}else{
System.out.println("Not Saving image --> ");
}
}catch (Exception e){
e.printStackTrace();
}
return "";
}
@Override
protected void onPostExecute(String string) {
}
}
But in this approach i am able to save only 50 -60 images in background. Its not running till end. What should i do please suggest
Use below library -
Implementation of that library -
You can also use android DownloadManager API. Android: How to use download manager class?