0

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();
Cyril David
  • 103
  • 2
  • 10

1 Answers1

0

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);
Fady Emad
  • 173
  • 2
  • 11
  • Here am storing byte[] data of the image into sqlite so that when there is no network connectivity I can use the byte[] data. Since I am using byte[], I have to use the below code: ByteArrayOutputStream out = new ByteArrayOutputStream(); newBitmap.compress(Bitmap.CompressFormat.PNG, 100, out); buffer = out.toByteArray(); Whenever I use this code, It automatically saves an image in the gallery. Is there any way to avoid that? – Cyril David Feb 28 '18 at 06:55
  • ok, i got it. try this `MemoryStream stream = new MemoryStream(); bitmap.Compress(Bitmap.CompressFormat.Png, 0, stream); byte[] bitmapData = stream.ToArray(); return bitmapData; ` – Fady Emad Feb 28 '18 at 08:31
  • Isn't MemoryStream based on Xamarin? Any android code to work out the above code? If am wrong, can you point it out on how to use your code? – Cyril David Feb 28 '18 at 20:05