Vahe Muradyan
Vahe Muradyan

Reputation: 1115

Android . Where to save downloaded images?

In my application I have several images in drawable folder. That makes apk big. Now I have uploaded them in my Google drive and when the user will connect to the internet it will download that images from drive. Where to save that downloaded images? in external storage , in database or in other place? I want that the user couldn't delete that images.

Upvotes: 0

Views: 107

Answers (2)

Marcin Orlowski
Marcin Orlowski

Reputation: 75619

I want that the user couldn't delete that images

You can't really do that. Images weren't here unless user selected them so why you do not want to let user delete them? in such case you will download it again or ask user to select. normal "cache" scenario. Not to mention you are in fact unable to protect these images as user can always clear app data (including databases and internal storage).

Upvotes: 2

Aniruddha
Aniruddha

Reputation: 4487

You can store them in Internal phone memory.

To save the images

private String saveToInternalSorage(Bitmap bitmapImage){
        ContextWrapper cw = new ContextWrapper(getApplicationContext());
         // path to /data/data/yourapp/app_data/imageDir
        File directory = cw.getDir("youDirName", Context.MODE_PRIVATE);
        // Create imageDir
        File mypath=new File(directory,"img.jpg");

        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();
        }
        return directory.getAbsolutePath();
    }

To access the stored file

private void loadImageFromStorage(String path)
{

    try {
        File f = new File(path, "img.jpg");
        Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
        // here is the retrieved image in b
    } 
    catch (FileNotFoundException e) 
    {
        e.printStackTrace();
    }

}

EDIT

You can save files directly on the device's internal storage. By default, files saved to the internal storage are private to your application and other applications cannot access them (nor can the user). When the user uninstalls your application, these files are removed.

For more information Internal Storage

Upvotes: 2

Related Questions