如何从网址

时间:2018-02-27 07:34:34

标签: android bitmapimage imagedownload

我使用以下代码从网址下载图片,然后保存到sqlite,然后在活动中的imageview中查看。

new LoadProfileImage().execute(jsonObject.getString("image"), id, title, promoexpdate, String.valueOf(i),flag,promostartDate);

上面的代码用于调用函数来完成上述工作。

private class LoadProfileImage extends AsyncTask<String, Void, Bitmap> {
    ImageView bmImage;
    String x,y,z,a,w,s;

    protected Bitmap doInBackground(String... uri) {
        String url = uri[0];
        Log.d("ImageURL",url);
        x = uri[1];
        y = uri[2];
        z = uri[3];
        a = uri[4];
        w = uri[5];
        s = uri[6];
        Log.d("LogValue",url+x+y+z+a+w+s);
        Bitmap mIcon11 = null;
        try {
            InputStream in = new java.net.URL(url).openStream();
            mIcon11 = BitmapFactory.decodeStream(in);
        } catch (IOException e) {
            Log.e("ErroronImageParsing", e.getLocalizedMessage());
            e.printStackTrace();
        }
        return mIcon11;
    }

    protected void onPostExecute(Bitmap result) {
        if (result != null) {
            int width = result.getWidth();
            int height = result.getHeight();
            Bitmap newBitmap = Bitmap.createScaledBitmap(result, width / 2, height / 2, true);
            ByteArrayOutputStream out = new ByteArrayOutputStream();
            newBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
            buffer = out.toByteArray();
            if (result!= newBitmap){
                result.recycle();
            }
            Log.d("ImageUploaded", "Success");
        }
            try {
                dbManager.open();
                Cursor cursor  = dbManager.fetch_PromsID(x);

                if (cursor.getCount() > 0){

                    String fla = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PRO_FLAG));
                    String pri_ID = cursor.getString(cursor.getColumnIndex(DatabaseHelper.PRO_ID));

                    if (!w.equals(fla)) {
                        dbManager.update_Promotions(pri_ID,y,z, buffer,w,s);
                    }
                }else {
                    dbManager.insertPromotions(x,y,z,buffer,w,s);
                }


            } catch (SQLException e) {
                e.printStackTrace();
            }
                SqliteData();
                panel.setVisibility(View.GONE);

            dbManager.close();
    }


}

这里,当执行以下代码时,来自URL的图像被保存到内部存储器中。我希望在保持我的意图的同时禁用自动保存。提前谢谢......

Bitmap newBitmap = Bitmap.createScaledBitmap(result, width / 2, height / 2, true);
ByteArrayOutputStream out = new ByteArrayOutputStream();
newBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
buffer = out.toByteArray();

1 个答案:

答案 0 :(得分:0)

尝试使用第三个库,例如PicassoGlid 提供

  1. 无缓存加载
  2. 加载内存或存储缓存
  3. 你可以使用单行代码

    来完成
    Picasso.with(context).load(imageUrl)
                .error(R.drawable.error)
                .placeholder(R.drawable.placeholder)
                .memoryPolicy(MemoryPolicy.NO_CACHE, MemoryPolicy.NO_STORE)
                .into(imageView);