How to load images from server properly? - android

I have a code below, this code works perfectly fine when you are trying to load one or two images. However, the problem is, when you have a lot of images in one Activity, all images must load first before you can get in. My question is, what is the best way to handle this images? I want the images to load one by one and not simultaneously. I want it to load just like the ListView made by Fedor "http://stackoverflow.com/questions/541966/android-how-do-i-do-a-lazy-load-of-images-in-listview" (Note: I'm not using a ListView, I just want my images to load like that). Please help me, I would really appreciate it a lot. Thanks in advance!
class ImageDownlaodThread extends Thread {
ImageDownloadMessageHandler imageDownloadMessageHandler;
String imageUrl;
#Override
public void run() {
Drawable drawable = LoadImageFromWebOperations(imageUrl);
Message message = imageDownloadMessageHandler.obtainMessage(1, drawable);
System.out.println("Message sent");
}
}
class ImageDownloadMessageHandler extends Handler {
View imageTextView;
}
#Override
public void handleMessage(Message message) {
progressBar.setVisibility(View.GONE);
imageTextView.setVisibility(View.VISIBLE);
}
}
Drawable LoadImageFromWebOperations(String url) {
Drawable d = null;
InputStream is = null;
try {
} catch (IOException e) {
e.printStackTrace();
}
return d;
}

Would this tutorial help [TUT] ImageView with Loading Spinner:
http://www.anddev.org/novice-tutorials-f8/imageview-with-loading-spinner-t49439.html
It basically show's a spinner until the image has loaded from the remote site :-)
You could strip the code to show a blank space / whatever you want till it loads.
If you don't want anything to happen till it all loads, you could have a count of how many images there are and then increment this each time an image has loaded.

IMO, AsyncTask is the easiest way to do this; in fact, it was basically built for this sort of task.
There's really too much to discuss in a StackOverflow answer. Just see Painless Threading to get started.

Related

set wallpaper refresh on Android

I am trying to change the current user wallpaper.
I have set the set_wallpaper permission and it works.
But when I change the wallpaper I have to wait about 15 seconds to see the wallpaper change.
This is currious because if I check the lock screen it has already changed.
Here is my code:
public static void setWallpaper(final Context context, final Bitmap image){
Thread thread = new Thread() {
#Override
public void run() {
WallpaperManager wallpaperManager = WallpaperManager.getInstance(context.getApplicationContext());
try {
if(image != null){
wallpaperManager.setBitmap(image);
}
} catch (IOException ignored) {}
}
};
thread.start();
}
I'm on Android 6.
Any help would be appreciated.
Why you used a Thread for this purpose?
Remove that and use your code directly on MainThread and see your code works correctly and instantly changed user wallpaper!
It might be worth having a deeper look at the WallpaperManagers setBitmap method and the inner callback (ln 1055, ln 1713) ref
I am not entirely sure how this whole code works nor do I think this really helps you solving the problem but it might give you a hint.
See this question for a similar problem

Dynamic ImageView in android

I'm a newbie in android so please don't judge me hard.
I'm trying to create a screen with two areas, an image area and a text area.
In the image area several images should be changed within a periodical time, 3-4 seconds. (Please see the image: )
Can you please give me an example how can I accomplish this?
Thank you.
You can use CountDownTimer class for this.
Or you can use a Gallery with Thread and Handler to achieve this.
Here is a link to the project called AutoSlideGallery,
https://github.com/nixit28/AutoSlideGallery
The core logic is here,
(new Thread() {
public void run() {
myslideshow();
handler.postDelayed(this, 2000); // execute every two second.
}
}
).start();
And the method which performs the action,
private void myslideshow() {
PicPosition = gallery.getSelectedItemPosition() + 1;
if (PicPosition >= pics.length)
gallery.setSelection(0); // stop
else
gallery.setSelection(PicPosition);// move to the next gallery
// element.
}

Changing image consumes lot of time

Is there any way that I can increase the speed of setting the image? What I am trying to say is suppose I have some hundreds of images and each of those images can be changed automatically or by pressing forward/rewind buttons. (Think of the media player situation where you can forward or rewind as many files, same I am doing with images and some audio behind).
Now, if I continuously keep pressing RW/FW buttons, the layout appears to be black for some time or asks for force close or wait of the application.
Any one know how to increase this speed of reading the bitmap and setting it to the imageview?
Here is a part of my code,
private void playAudio() {
msg = new Message();
msg.obj = (filepath);
imageHandler.sendMessage(msg);
}
private Handler imageHandler = new Handler() {
public void handleMessage(Message msg) {
try {
path = (String) msg.obj;
imagePath = RaconTours.PATH + path;
imagenamearray = imagePath.split("/");
currentImagename = imagenamearray[8];
i++;
if (!imagePath.equalsIgnoreCase(staticpath)) {
isSeekBarChangedManually = false;
staticpath = imagePath;
Bitmap snoop = readBitmap(Uri.fromFile(new File(filepath)));
image.setImageBitmap(snoop);
image.setMaxZoom(4f);
} catch (Exception e) {
e.printStackTrace();
}
}
};
Hope you get some idea with this code, this is just a small piece of the big cake.
I am not getting the method to solve this. I tried disabling the button also until some action is being performed but that is also of no use.
Any help, cheers
I think the only way to make this process "faster" is to load and cache the bitmaps in a buffer before you actually need them. When you need a bitmap, you check this forward-cache to see if it's already loaded. If it is, then it means it's already in memory -> you just need to set it for the proper ImageView (or whatever you use). If not, then you load it from the disk.
Of course, you need to know that which bitmaps you'll need most likely in the near future for this to work.
You can't really make disk I/O work any faster you see.

How to load activity before loading all images?

I have been struggling with this problem for many days. please help me..
In my android application i am trying to download images from remote server dynamically ( no of images dynamically come ). for downloading all images it is taking 30 to 40 seconds mean time user has to wait to see the activity . But it is the worst case that loading activity after loading all images. I want to load activity first then load images one by one.
Is there anything to do it?
Thanks and Regards,
Kiran
I think you need to implemet your code using "AsyncTask", for more info, refer this link: http://developer.android.com/reference/android/os/AsyncTask.html
for example:
public void onClick(View v) {
new DownloadImageTask().execute("http://example.com/image.png");
}
private class DownloadImageTask extends AsyncTask {
protected Bitmap doInBackground(String... urls) {
return loadImageFromNetwork(urls[0]);
}
protected void onPostExecute(Bitmap result) {
mImageView.setImageBitmap(result);
}
}
See in above example, Write your code for downloading images inside "doInBackground()" function and write the code for displaying iamges inside "onPostExecute()" function.
Enjoy!!

ProgressBar between to activities in android?

when click the grid item, i want to show a progressbar between the time of next Activity shown. then the second activity has a custom listview. there also i want to show a progressbar. how to do that?
I'm just learning myself and I haven't had a chance to inspect the source, but I know that the RedditIsFun app (for Reddit, of course) does this in-between loading the next links, and loading comments. Check out the source.
You should try this,
First Create the ProgressDilog in Activity. . .
private ProgressDialog progressDialog;
Now, Set the ProgressDialog in the Any action where u want to Take Some time
progressDialog = ProgressDialog.show(FoodDriveModule.this, "", "Loading...");
Now use Thread to Handle the Progressbar
new Thread()
{
public void run()
{
try
{
sleep(1500);
// Now Do some Task whatever u want to do
}
catch(Exception e)
{
Log.e("tag",e.getMessage());
}
}.start();
Thats it. Hope this will help you.

Categories

Resources