0

I am loading bitmap from a twitter feed. I am using below code

private class getBitmapFromLink extends AsyncTask<String, Void, Bitmap> {
    private ImageView imgView;

    public getBitmapFromLink(ImageView imgView) {
        this.imgView = imgView;
    }

    @Override
    protected Bitmap doInBackground(String... params) {
        Bitmap myBitmap;
        try {
            URL url = new URL(params[0]);
            HttpURLConnection connection = (HttpURLConnection) url
                    .openConnection();
            connection.setDoInput(true);
            connection.connect();
            InputStream input = connection.getInputStream();
            myBitmap = BitmapFactory.decodeStream(input);
            return myBitmap;
        } catch (IOException e) {
            Log.v("BITMAP", e.getMessage());
            return null;
        } catch (Exception e) {
            Log.v("BITMAP", e.getMessage());
            return null;
        }

    }

    @Override
    protected void onPostExecute(Bitmap result) {
        imgView.setImageBitmap(result);
        imgView.invalidate();
    }

}

When I debug myBitmap = BitmapFactory.decodeStream(input); line it directly come to catch block and on return null line. While debugging I tried printing error message on logs but it does not execute that line and directly come to return statement.

Thanks in Advance

unflagged.destination
  • 1,576
  • 3
  • 19
  • 38

1 Answers1

0

You can use the Universal Image Loader and than

  imageLoader = ImageLoader.getInstance(); // Get singleton instance
    ImageLoader.getInstance().init(ImageLoaderConfiguration.createDefault(ProfileScreen.this));
    imageLoader = com.nostra13.universalimageloader.core.ImageLoader.getInstance();
    imageOptions = new DisplayImageOptions.Builder()
            .showImageOnFail(R.drawable.user_icon)
            .showImageOnLoading(R.drawable.user_icon)
            .showImageForEmptyUri(R.drawable.user_icon)
            .cacheInMemory(true)
            .resetViewBeforeLoading(true)
            .build();

And than where you get the url use these lines of code

     iimageLoader.displayImage("your url", imgView, imageOptions);

                            imageLoader.loadImage("your url", new SimpleImageLoadingListener() {
                                @Override
                                public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
                                    // Do whatever you want with Bitmap
                                    imgView.setImageBitmap(loadedImage);

                                }
                            });

You can also use the Picasso library to perform yours operation... for that you can use these lines of code for it

Picasso.with(getContext()).load("your url").into(new Target() {
                @Override
                public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
                    //do what ever you want with your bitmap 
                  imgView.setImageBitmap(loadedImage);
                }

                @Override
                public void onBitmapFailed(Drawable errorDrawable) {

                }

                @Override
                public void onPrepareLoad(Drawable placeHolderDrawable) {

                }
            });
Ravindra Kushwaha
  • 7,846
  • 14
  • 53
  • 103