【问题标题】:Android get image from server to SQLite databaseAndroid 从服务器获取图像到 SQLite 数据库
【发布时间】:2015-02-03 16:25:36
【问题描述】:

Android 从服务器获取图像到 SQLite 数据库

我需要在新闻部分显示图像,并且在没有互联网连接时我想离线阅读它们。我是用标题和正文来做的,但是对于新闻图片来说太慢了,我该如何改进呢?

我试图获取字符串 url 并将其存储为字节数组:

         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();
        }

然后我在 doInBackground 中这样称呼它:

@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;
        }

但这也是因为我正在为每张图片连接

【问题讨论】:

    标签: java android sqlite mobile bitmap


    【解决方案1】:

    如果您知道要将图像放入哪个视图中,可以使用Square's Picasso 来简化图像处理:

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

    【讨论】:

    • 但如果我在没有互联网连接的情况下打开应用程序,它将不会加载图像
    • 如果您之前打开过应用程序,图像将被缓存在设备上。如果不是,那么您需要采用与您的代码和我的示例完全不同的路线。
    • 我最终使用了 Universal Image Loader,但您的回答引导我解决了问题
    猜你喜欢
    • 1970-01-01
    • 2011-10-27
    • 1970-01-01
    • 2016-06-25
    • 2016-04-02
    • 1970-01-01
    • 2011-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多