简体   繁体   中英

Android get image from server to SQLite database

Android get image from server to SQLite database

I need to display images in the news section and when there is no internet connection i want to read them offline. I did it with the title and the body text but for the news image it is too slow how can I improve that?

I tried to get the string url and store it as byte array:

         public byte[] downloadImage(String urlImg) throws Exception{

            URL url = new URL(urlImg);
            HttpURLConnection con = (HttpURLConnection) url.openConnection();
            con.setRequestMethod("GET");
            con.setReadTimeout(10000);
            con.setConnectTimeout(10000);

            ByteArrayOutputStream bos = new ByteArrayOutputStream();

            try {
                Bitmap b = BitmapFactory.decodeStream(con.getInputStream());
                b.compress(Bitmap.CompressFormat.WEBP, 0, bos);
            } finally {
                con.disconnect();
            }

            return bos.toByteArray();
        }

then I call it inside the doInBackground like this:

@Override
        protected Boolean doInBackground(String... urls) {

            try {
                HttpGet httppost = new HttpGet("http://www.77-webdev.com/clients/thebeautyworkshop/news.json");
                HttpClient httpclient = new DefaultHttpClient();
                HttpResponse response = httpclient.execute(httppost);

                int status = response.getStatusLine().getStatusCode();
                if (status == 200){
                    DataSource.deleteAll();
                    HttpEntity entity = response.getEntity();
                    String data = EntityUtils.toString(entity);

                    JSONObject jsonObject = new JSONObject(data);
                    JSONArray jsonArray = jsonObject.getJSONArray("News");
                    News news = new News();
                    for (int i=0; i < jsonArray.length();i++){
                        JSONObject JObject = jsonArray.getJSONObject(i);

                        String Title =  JObject.getString("title");
                        String body =  JObject.getString("mobile_body");
                        String Image =  JObject.getString("image");
                        String lang =  JObject.getString("lang");
                        byte[]  IMG = downloadImage(Image);


                        news.setTitle(Title);
                        news.setMobile_body(body);
                        news.setImageB(IMG);
                        news.setLang(lang);
                        DataSource.create(news);
                        newsList.add(news);

                    }

                    return true;
                }


            } catch (ParseException e1) {
                e1.printStackTrace();
            } catch (IOException e) {
                e.printStackTrace();
            } catch (JSONException e) {
                e.printStackTrace();
            } catch (Exception e) {
                e.printStackTrace();
            }
            return false;
        }

but it's too because I'm connecting for each image

If you know into which view you want to put the image, you can use Square's Picasso to simplify the image process:

Picasso
        .with(context)
        .load(Image)
        .into(imageView);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM