Waseem
Waseem

Reputation: 1408

Where and How to store png images in android Application

I am working on an application which will contain tons of graphics....Its approx size will be 80 MB.Application will download the graphics(Greeting Cards in png format actually) on first start from web service.The problem I am facing is that where to save these graphics (Greeting cards) and how ???Whether to save them in internal storage or SD card or SQLite....My questions are

  1. How to store them in different folders based on their categories.
  2. How to update them.
  3. How to retrieve/access them.

    All info of these images will be in SQLite.Also please show me some code.

Upvotes: 1

Views: 3662

Answers (3)

success_anil
success_anil

Reputation: 3658

Use the below code.

public class ImageDownload {

    public static void downloader(String imageURL, String fileName) { 
            try {
                    URL url = new URL("http://www.exampleurl.com/" + imageURL); 
                    File file = new File(fileName);


                    URLConnection con = url.openConnection();

                    InputStream is = con.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);

                    ByteArrayBuffer baf = new ByteArrayBuffer(50);
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                            baf.append((byte) current);
                    }

                    FileOutputStream fos = new FileOutputStream(file);
                    fos.write(baf.toByteArray());
                    fos.close();

            } catch (IOException e) {
                    Log.d("Downloader", "Error: " + e);
            }

    }
    }

Upvotes: 1

Jack
Jack

Reputation: 9242

  1. This is all dependent on how you want to structure your storage. If the user can create categories, store the users files that relate to category1, in a folder called category1.
  2. Updating them would be a matter of comparing the newly downloaded file to either your information in the database, or the actual file itself, and replacing if necessary. Or firstly compare your file on disk to the file from the web, and updating as necessary.
  3. You could use an HttpClient to retrieve / access them from the web, and maybe the Android ContentManager to store / retrieve them from the phone. You would store the path to the image in Sqlite

As far as code, you will hardly find anyone on here who is just going to give you teh codez. StackOverflow is for when you get stumped and need help.

Google:

android httpclient
android sqlite
android content provider

Upvotes: 0

PravinCG
PravinCG

Reputation: 7708

Answers below. For code you should search this place and try out few things and get back if it does not work.

  1. Since the size is like 80MB, I would recommend you to store in External storage. You can create directory structure and store each category there.
  2. You need to give WRITE_EXTERNAL_STORAGE permission to your application and then you can read/write/delete/update your png.
  3. Simple file operation can be used to retrieve/access the image. In SQLite you can store the path of the images.

Upvotes: 1

Related Questions