Search code examples
androidbitmapimageimagedownload

How to disable image from saving into internal memory while downloading from an url


I am using the following code to download an image from an url, then saving to sqlite and then view in imageview in an activity.

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

The above code is used to call the function to do the above work.

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


}

Here, when the code below is executed, image from the url is saved into internal storage. I wish to disable the auto saving while maintaining my intention. Thanks in advance...

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

Solution

  • Try to use third library like Picasso or Glid that offer

    1. loading without caching
    2. loading with memory or storage caching

    you can do it with single line of code

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