如何从网址下载时禁用图像保存到内存

By simon at 2018-02-28 • 0人收藏 • 34人看过

我正在使用以下代码从网址下载图像,然后保存到 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();
    }


}
这里,执行下面的代码时d,来自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 个回复 | 最后更新于 2018-02-28
2018-02-28   #1

尝试使用[Picasso](http://square.github.io/picasso/)或第三个库 [滑翔](https://github.com/bumptech/glide)那个[R   1.没有缓存加载   2.加载内存或存储缓存 你可以用单行代码来完成

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

登录后方可回帖

Loading...