0

I'm trying to download image in a Bitmap array with AsyncTask from an https URL

Here my code of AsyncTask:

class GetImages extends AsyncTask<String,Void,Bitmap>{
        int position;
        public GetImages(int i){
            position=i;
        }
        @Override
        protected void onPreExecute() {    }

        @Override
        protected Bitmap doInBackground(String... strings) {
            Log.i(TAG,"Enter in background!!!!!!!!!!!!!!!!!!");
            String src=strings[0];
            Bitmap bitmap=null;
            InputStream in;
            int responseCode=-1;
            try {
                URL url = new URL(src);

                HttpsURLConnection httpURLConnection = (HttpsURLConnection) url.openConnection();
                httpURLConnection.setDoOutput(true);
                httpURLConnection.connect();
                responseCode = httpURLConnection.getResponseCode();
                if (responseCode == HttpURLConnection.HTTP_OK) {
                    in = httpURLConnection.getInputStream();
                    bitmap = BitmapFactory.decodeStream(in);
                    in.close();
                }
                Log.i(TAG,"OK!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!");
            } catch (MalformedURLException e) {
                Log.i(TAG,"!!!!!!!!!Errore1");
                e.printStackTrace();
            } catch (IOException e) {
                Log.i(TAG,"!!!!!!!!!Errore2");
                e.printStackTrace();
            }
            return bitmap;
        }

        @Override
        protected void onPostExecute(Bitmap bitmap) {
            Log.i(TAG,"Saving!!!!!!!!!!!!!!!!!!!!!!!");
            image[position]=bitmap;
        }

    }

Where image[] is a Bitmap array

And I execute my AsyncTask:

for(i=0;i<Array.length;i++){
...
new GetImages(i).execute(MyHttpsURL[i]);
}

What I want to do is to save images from my https urls and save it in a Bitmap array to reuse it in RecyclerView. The problem is that seems my code doesn't download and put images in array because when I use image[] in recycler view my ImageViews that should display downloaded images are empty. What's my error(s)? How can I save images from https in a Bitmap array?

Marco
  • 98
  • 1
  • 1
  • 10
  • What is in Array? `lenght` should be `length`. – ImAtWar Oct 01 '17 at 14:20
  • 1) Do you get any errors? 2) Do your files download in the first place? – Juan Oct 01 '17 at 14:21
  • 1
    I suggest you to use an external library to do this. The best i've used so far are: Volley and Picasso –  Oct 01 '17 at 14:23
  • No I get no errors and the app doesn't crash, the images are not downloaded at all – Marco Oct 01 '17 at 14:24
  • 2
    I recommend Glide https://github.com/bumptech/glide It'đ from google and it's best – Parad0X Oct 01 '17 at 14:27
  • @Francesco it could be a great idea but the problem is that I got limited number of requests so I need to download images to avoid too many requests instead of displaying it from url – Marco Oct 01 '17 at 14:29
  • @trip08 same problem as I write above, I can't use an image loader that loads files from url but don't save them because I have limited number of requests to get images – Marco Oct 01 '17 at 14:31
  • Your "limited number of requests" has no impact on your ability to use a tested and debugged library for making your HTTPS requests and decoding the resulting `Bitmap`. Also, please bear in mind that you may not have memory to hold a bunch of `Bitmap` objects, and bear in mind that the user may not appreciate waiting while you download the entire array's worth of bitmaps. – CommonsWare Oct 01 '17 at 14:37
  • as other guys mention, using Image Loaders like Glide, Picasso or Fresco is a better solution to download, cache and show images in Android. If you want to read the bytes, read them manually from the input stream you get by creating the connection, do not pass the input stream to BitmapFactory – Bardya Momeni Oct 01 '17 at 14:39
  • @CommonsWare My problem is that ,if I understand how Picasso and Glide work, they both load image from url so if I use it in a recycler View every time the recycler View's adapter create an item I need to use a request – Marco Oct 01 '17 at 14:40
  • @Marco: They both offer configurable caches. The [Glide](https://github.com/bumptech/glide) and [Picasso](https://github.com/square/picasso) project pages each mention caching in their very first sentence. "Glide is a fast and efficient open source media management and image loading framework for Android that wraps media decoding, memory and disk caching, and resource pooling into a simple and easy to use interface." Picasso has "A powerful image downloading and caching library for Android". – CommonsWare Oct 01 '17 at 14:44
  • @CommonsWare Ok so I think they could be perfect for my purpose. I will try them – Marco Oct 01 '17 at 14:47

1 Answers1

0

you can use an image loader library like Picasso and save its bitmap value

Get Bitmap from ImageView loaded with Picasso