I have an image view , i had written swiping , at that time of swiping,the images are downloading from Internet, so i thought i have to download the images in the background before swiping , for that which i need to use asynctask or Service or IntentService, all these will help in downloading and storing in data/data/mypackages , but still swiping gets slow in my case any idea, also convey me which one is best one, is it i'm calling in a right way
1. asynctask
2. services
3. Intent Service as shown below,
i m confused which one is right method because still my problem not solved
Here's asynctask code sample snippet
public class Demo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
new FirstTask().execute(); // calling Asynctask here
}
}
Async Task code
private class FirstTask extends AsyncTask<Void, Void, Void> {
private final ProgressDialog dialog = new ProgressDialog(Catalogue.this);
int temp = 0;
// can use UI thread here
protected void onPreExecute() {
this.dialog.setMessage("Loading...");
this.dialog.setCancelable(false);
//this.dialog.show();
System.gc();
Toast.makeText(Catalogue.this, "My Async Created",
Toast.LENGTH_LONG).show();
}
@Override
protected Void doInBackground(Void... params) {
Looper.prepare();
try {
myddownloadmethod();// calling my download method
} catch (Exception e) {
Util.trace("Error in Async"+e.getMessage());
}
Looper.loop();
return null;
}
protected void onPostExecute(Void result) {
if (this.dialog.isShowing()) {
Toast.makeText(Catalogue.this, "My Async destroyed",
Toast.LENGTH_LONG).show();
Toast.makeText(Catalogue.this, "count" + temp,
Toast.LENGTH_LONG).show();
this.dialog.dismiss();
}
}
}
Here's My Service sinppet
public class MyService extends Service implements Runnable
{ @Override
public void onCreate() {
super.onCreate();
Thread mythread = new Thread(this);
mythread.start();
}
public void run() {
Looper.prepare();
try {
myddownloadmethod();// calling my download method
} catch (Exception e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
Looper.loop();
}
@Override
public IBinder onBind(Intent intent) {
// TODO Auto-generated method stub
return null;
}
}
Invoking Service
public class ServicesDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
startService(new Intent(this, MyService.class));
}
}
Here's IntentService Code
public class Downloader extends IntentService {
public Downloader() {
super("Downloader");
}
@Override
public void onCreate() {
super.onCreate();
}
@Override
public void onDestroy() {
super.onDestroy();
}
@Override
public void onHandleIntent(Intent i) {
try {
myddownloadmethod();// calling my download method
} catch (Exception e1) {
// TODO Auto-generated catch block
Log.d("Error",e1.getMessage());
}
}
}
Calling IntentService from MyActivity
public class ServicesDemo extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent i1=new Intent(this, Downloader.class);
startService(i1);
}
}
Use volley. Using volley network image view you can do this
You are probably over engineering this. I have implemented swiping with dynamically loading images and I just a use a simple utility class that does it all for me via static method call. Try this class:
This is all you need. Then when you need to load an image, you call:
That's it.
If you have an URL from a JSON object, parse the URL into your string so:
String url = jsonObj.getString("url");
Then callfetchDrawableOnThread(url, yourImageViewReference);
For anyone running into this question later, take a look at the async download mechanism used in the android sample code for the project
com.example.android.bitmapfun.ui.ImageGridActivity
. It downloads images asynchronously and also caches them for offline display in an ImageView. Folks have wrapped their code around this one and made image loading libraries of their own. These libraries use an AsyncTask instead of a service. Async tasks are expected to wrap up their work within a couple of seconds.If you are looking to download something larger, I'd recommend the DownloadManager that is available since API 9 instead of using services. There is a lot of code in there that adds resilience to the download.
The best way to download it using the service like i have done to download the file from server and put in SD card also use the notification for it. It is quite long code but i think the perfect one,if did not understand any thing then please go to android developer blog for services.
we use latest architecure components here.We make some observers with live data for some flag that represents download status.In service we download the image and completing we update the live data so that the observers method automatically called