开发者

Download an image with AsyncTask, I need help if there's no network connection

开发者 https://www.devze.com 2023-03-09 20:16 出处:网络
I\'m new to programming so bear with me, this is a big learning experience for me, and it\'s been a lot of fun so far.

I'm new to programming so bear with me, this is a big learning experience for me, and it's been a lot of fun so far.

What I'm trying to do is to load an image into my app that I change and upload to my server on a regular basis. Right now, it loads a placement image until the proper image is finished downloading in the background, then shows the new image that it's downloaded.

My problem is that if there is no network connection, it ends up showing nothing at all. I'm assuming that there's something I can do in PostExecute that would show a drawable from within the app if it fails to load the image from the net, I'm just not sure how to do it!

Any help would be greatly appreciated!

EDIT: Am I on the right track to displaying an alternate image?

import java.io.IOException;
import java.lang.ref.WeakReference;
import java.net.MalformedURLException;
import java.net.URL;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.widget.ImageView;
import android.widget.Toast;

public class ImageDownloader extends AsyncTask<String, Void, Bitmap> {

private String url;
private final WeakReference<ImageView> imageViewReference;
private Resources res;

public ImageDownloader(ImageView imageView) {
    imageViewReference = new WeakReference<ImageView>(imageView);
}


@Override
protected Bitmap doInBackground(String... params) {
    url = params[0];        
    try {
        return BitmapFactory.decodeStream(new URL(url).openConnection()
                .getInputStream());
    } catch (MalformedURLException e) {
        e.printStackTrace();
        return null;
    } catch (IOException e) {


      return BitmapFactory.decodeResource(res, R.drawable.displayThis);

        }
}


@Override
protected void onPostExecute(Bitmap result) {
    if (isCancelled()) {
        result = null;
    }
    if (imageViewReference != null) {
        ImageView imageView = imageViewReference.get();
        if (imageView != null)开发者_如何学运维 {
            imageView.setImageBitmap(result);
        } 
    } 
}

@Override
protected void onPreExecute() {
    if (imageViewReference != null) {
        ImageView imageView = imageViewReference.get();
        if (imageView != null) {
            imageView.setImageResource(R.drawable.uboxback);
        }
    }
}
}


If you get null as a return value from you doInBackground function can you not just display another image? Also you could always check if there is a network connection available before trying to do any of it and do something at that point?

You can check if a network connection is available by doing something similar to the following:

ConnectivityManager cm = (ConnectivityManager) ctx.getSystemService(Context.CONNECTIVITY_SERVICE);
        NetworkInfo netInfo = cm.getActiveNetworkInfo();
        if (netInfo != null && netInfo.isConnected()) {
//this is connected
    }

Does this help at all?

Bex


In case of internet failed during data getting from internet in doInBackground() , Exception would come in the catch block. Here u can do the trick.

Take one global variable. If internet is failed then assign the "Failed" value to that variable in the catch block. Then in the onPostExecute() u can check do with the help of if and else depends on this variable.

Hope it helps....

0

精彩评论

暂无评论...
验证码 换一张
取 消