Ivory Yang
Ivory Yang

Reputation: 1

Android: To get a image from server

I used a ListView which contains a ImageView and a TextView. I want to get a image from server. I debug the code below, which told me that I can get the image successful, but when return, the image disappeared. I don't know why. I am the new begginner of android. Hope anyone can help me.

The ListView Adapter class:

public View getView(int position, View convertView, ViewGroup parent) {
    News news = getItem(position); 
    View view = LayoutInflater.from(getContext()).inflate(resourceId, null);
    newsImage = (ImageView) view.findViewById(R.id.news_image);
    newsTitle = (TextView) view.findViewById(R.id.news_title);

    newsTitle.setText(news.getTitle());
    bitmap = HttpUtil.sendImageRequest(news.getImageUrl());
    newsImage.setImageBitmap(ImageUtility.ImageSolution(bitmap, newsImage));
    return view;
}

the sendImageRequest method:

public static Bitmap sendImageRequest(final String address){
    final Bitmap[] bitmap = new Bitmap[1];
    new Thread(new Runnable() {
        @Override
        public void run() {
            HttpURLConnection connection = null;
            try {
                connection = (HttpURLConnection) new URL(address).openConnection();
                connection.setRequestMethod("GET");
                connection.setDoInput(true);

                InputStream in = connection.getInputStream();
                bitmap[0] = BitmapFactory.decodeStream(in);
            } catch (java.io.IOException e) {
                e.printStackTrace();
            } finally {
                if (connection != null){
                    connection.disconnect();
                }
            }
        }
    }).start();
    return bitmap[0];
}

the ImageSolution method:

public static Bitmap ImageSolution (Bitmap bitmap, ImageView imageView){
    int width = bitmap.getWidth();
    int height = bitmap.getHeight();
    int newWidth = imageView.getWidth();
    int newHeight = imageView.getHeight();

    float scaleWidth = ((float) newWidth) / width;
    float scaleHeight = ((float) newHeight) / height;

    Matrix matrix = new Matrix();
    matrix.postScale(scaleWidth, scaleHeight);

    Bitmap resizedBitmap = Bitmap.createBitmap(bitmap, 0, 0, width,
            height, matrix, true);
    return resizedBitmap;
}

the address is http://p2.zhimg.com/10/7b/107bb4894b46d75a892da6fa80ef504a.jpg.

Thank you so much.

Upvotes: 0

Views: 52

Answers (4)

greenapps
greenapps

Reputation: 11214

bitmap = HttpUtil.sendImageRequest(news.getImageUrl());

bitmap will always be empty as it is an image request. The function starts a thread but returns immediately. So returns an empty bitmap.

newsImage.setImageBitmap(ImageUtility.ImageSolution(bitmap, newsImage));

If you then try to set that empty bitmap to an imageview things go wrong.

Redesign your app. Only when the thread finishes there is a valid bitmap. And then you can use it.

Upvotes: 0

Mithun Sarker
Mithun Sarker

Reputation: 4023

You can use a third party library named Glide , which is recommended by google . To add this library , add the the following dependency in your build.gradle file , in module app .

dependencies {
    compile 'com.github.bumptech.glide:glide:3.5.2'
    compile 'com.android.support:support-v4:22.0.0'
}

And then you can simply load image in following way

Glide.with(context)
    .load(news.getImageUrl())
    .into(newsImage);

Upvotes: 1

Exigente05
Exigente05

Reputation: 2211

Make a viewHolder class inside adapter class,

private static class ViewHolder {
    public TextView newsTitle = null;
    public ImageView newsImage = null;
}

then code your getView like,

public View getView(final int position, View convertView, ViewGroup parent) {

    final ViewHolder itemHolder;
    View view = convertView;
    if (view == null) {
        LayoutInflater inflater = ((Activity) context).getLayoutInflater();
        itemHolder = new ViewHolder();
        view = inflater.inflate(layoutResID, parent, false);

        itemHolder.newsImage = (ImageView) view.findViewById(R.id.news_image);
        itemHolder.newsTitle = (TextView) view.findViewById(R.id.news_title);


        view.setTag(itemHolder);
    } else {
        itemHolder = (viewHolder) view.getTag();
    }

    News news = getItem(position);

    itemHolder.newsTitle.setText(news.getTitle());
    bitmap = HttpUtil.sendImageRequest(news.getImageUrl());
    itemHolder.newsImage.setImageBitmap(ImageUtility.ImageSolution(bitmap, newsImage));


    return view;
}

or, you can use picasso, it is easy to use...

add this line inside dependencies,

compile 'com.squareup.picasso:picasso:2.5.0'

and for loading image code for loading image will be,

Picasso.with(context).load(news.getImageUrl()).into(itemHolder.newsImage);

Upvotes: 0

Seishin
Seishin

Reputation: 1477

Why don't you use the Picasso library?

It's really easy to use

Picasso.with(context).load(news.getImageUrl()).into(newsImage);

And it provides caching and image transformations.

Upvotes: 0

Related Questions