0

I'm working on a school android project. I need to have a download button which downloads a picture(when we have class) And after display it in another activity(even in offline mode, and after quiting)

I've tried picasso, but I can't get it to save and use it in offline mode.

bytecode77
  • 14,163
  • 30
  • 110
  • 141
MrBurnzie
  • 21
  • 1
  • 6

3 Answers3

2

For you to support offline mode, You need to Save the image on your disk because when your cache is cleared, The image is cleared as well.

You can easily use Glide to Solve this, also storing on device and retrieving

You can Learn more about Glide here http://inthecheesefactory.com/blog/get-to-know-glide-recommended-by-google/en

/** Download the image using Glide **/

Bitmap theBitmap = null;
theBitmap = Glide.
    with(YourActivity.this).
    load("Url of your image").
    asBitmap().
    into(-1, -1).
    get();

   saveToInternalStorage(theBitmap, getApplicationContext(), "your preferred image name");

/** Save it on your device **/

public String saveToInternalStorage(Bitmap bitmapImage, Context context, String name){


        ContextWrapper cw = new ContextWrapper(context);
        // path to /data/data/yourapp/app_data/imageDir

        String name_="foldername"; //Folder name in device android/data/
        File directory = cw.getDir(name_, Context.MODE_PRIVATE);

        // Create imageDir
        File mypath=new File(directory,name);

        FileOutputStream fos = null;
        try {

            fos = new FileOutputStream(mypath);

            // Use the compress method on the BitMap object to write image to the OutputStream
            bitmapImage.compress(Bitmap.CompressFormat.PNG, 100, fos);
            fos.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
        Log.e("absolutepath ", directory.getAbsolutePath());
        return directory.getAbsolutePath();
    }

/** Method to retrieve image from your device **/

public Bitmap loadImageFromStorage(String path, String name)
    {
        Bitmap b;
        String name_="foldername";
        try {
            File f=new File(path, name_);
            b = BitmapFactory.decodeStream(new FileInputStream(f));
            return b;
        }
        catch (FileNotFoundException e)
        {
            e.printStackTrace();
        }
        return null;
    }

/** Retrieve your image from device and set to imageview **/
//Provide your image path and name of the image your previously used.

Bitmap b= loadImageFromStorage(String path, String name)
ImageView img=(ImageView)findViewById(R.id.your_image_id);
img.setImageBitmap(b);
Tosin Onikute
  • 3,883
  • 6
  • 38
  • 61
0

You could use Android Library called Universal Image Loader:

https://github.com/nostra13/Android-Universal-Image-Loader

mhdjazmati
  • 4,152
  • 1
  • 26
  • 37
0

Thanks to @Droidman : How to download and save an image in Android

Of course you can perform downloading and managing images by yourself, but if your project is quite complex already, there are a lot of libraries around and you do not need to reinvent the wheel. I won't post code this time since there are a lot of examples, but I'm going to tell you about 2 most useful libraries (IMO) related to image downloading.

1) Android Volley. A powerful networking library created by Google and covered by official documentation. POST'ing or GET'ing data, images, JSON - volley will manage it for you. Using volley just for image downloading is a bit of an overkill in my opinion.

2) Picasso

Image downloading and caching, perfect for ListView/GridView/RecyclerView. Apache 2.0 license.

3) Fresco

Quite a new image loading library created by Facebook. Progressive JPEG streaming, gifs and more. Apache 2.0

Community
  • 1
  • 1
mhdjazmati
  • 4,152
  • 1
  • 26
  • 37