1

I have an Application and that is live on google, but Now we have plan to change its little working to make it less in size. At the time now its about 60mb and its just due to number of images we have packaged in. Now We have decided to send less embed images in the app built and later let the user download these manually from the server. But I have confusion in couple of things. and these are as following.

  • How to download the images from server and save them in the device to reuse purpose. I mean let say we have uploaded 10 images on server, user download all of them , now next time if our server has no new images the last 10 images which are saved in devices should be able to reuse and should not be download by the app again.

so this point has three more point

  • 1) How to download and where to download the images
  • 2) If the user has deleted some images downloaded then how to check which images are deleted and to re download them.
  • 3) Is there any way to save the images in a place where user can not see them out side app and not be able to delete them (to eliminate my second point)

Also I have an grid view I am using them to show the image in grid , user click on the image and the real images open in new activity with some info now I have confusion in this also and that is

  1. As we are planning to embed at least 10 images in apk , so we need to show them in the grid (its easy as I am showing them from the app resource folder into the grid view ). But now as we have to download the images from server also , so now How do we populate the images into that grid view and how do we keep track that which image is coming from where either from the resource (as in previous version ,I have just to get it from drawable. but now the things are changed and complicated)

so in short the grid view is mix up of the local app images and the images from the server.

  1. would not it be time consuming to search the last downloaded images from the device and them to display them into a grid view ?

Thanks for reading so long question.

Androider
  • 3,833
  • 2
  • 14
  • 24
Coas Mckey
  • 701
  • 1
  • 13
  • 39

2 Answers2

4

I will try to make it as much clear as possible in step by step:

  • Create a table in local db (sqlite) and put its name some thing like "Image Paths" with 3 columns, "image_id" , "image_path", "image_name"
  • When your app launches for the first time after being installed, check if table is empty or not by doing something like this:

    // this will return true if the table has no data
    
    public static boolean checkTable(String q){
      DataBaseHelper dbh = new DataBaseHelper();
      SQLiteDatabase sqldb = dbh.createAndOpenDB();
      Cursor cd =null;
    try{
    
    
    boolean isEmpty=false;
    cd = sqldb.rawQuery(q, null);
    if(cd!=null){
        cd.moveToFirst();
        if(cd.getInt(0)==0){
            isEmpty = true;
        }
    }
    
    cd.close();
    dbh.closeDB();
    
    return isEmpty;}
    catch(Exception e){
        e.printStackTrace();
        if(cd!=null){
            cd.close();
            dbh.closeDB();
        }
        return false; } }
    
  • If the table is empty then it means either the app is running the first time or the user has deleted the private application data, either way after that you will want to download the whole list of images from server.

  • When you download images to the device now you should create a separate folder in internal memory which should be private to your app only, to do that:

    // write images privately 
    FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();
    
  • You should then save the name of each image along with its access path to the table that we just created in step 1.

  • So now accessing those images will be faster and much reliable, if for example you want to update the images from the server. Lets say you have some new images which you want to be synchronized with the device then you should make another table of versions which must be synchronized with the server's version table.. (both must have the same table) So when you check your image_path table in step 2, if the data is found then you should get a list of current versions from the server and match them with your local copy of it. If any version doesn't match then you should update accordingly.

  • If you want the user to not be able to delete your app's data using the clear data button, then you can hide it using the android:manageSpaceActivity attribute in the manifest file for example:

    android:manageSpaceActivity="com.main.slash.LaunchingActivity"
    

EDIT

After your comments, to create a folder in internal memory privately and save a file in it:

  File mydir = context.getDir("mydir", Context.MODE_PRIVATE); //Creating an internal directory/folder;
  File imageFile1 = new File(mydir, "myfile"); //Getting a file within the dir.
  FileOutputStream out = new FileOutputStream(imageFile1);

  // to get the path of the file you can call the getPath() or getAbsolutePath() methods

  String path = imageFile1.getAbsolutePath();
Sharp Edge
  • 4,144
  • 2
  • 27
  • 41
  • there are quite a good info you have mentioned in answer , now let say I have downloaded the image from url and then How to save it above mention point in which you are saying to create a separate folder in internal memory – Coas Mckey Nov 03 '15 at 10:15
  • and after saving it in internal memory how to get a complete path of the image to save it in the database ? – Coas Mckey Nov 03 '15 at 10:16
  • but i am unable to under stand your these wording but it sound really usefull as we have to do some thing like that with images Lets say you have some new images which you want to be synchronized with the device then you should make another table of versions which must be synchronized with the server's version table.. (both must have the same table) So when you check your image_path table in step 2, if the data is found then you should get a list of current versions from the server and match them with your local copy of it. If any version doesn't match then you should update accordingly. – Coas Mckey Nov 03 '15 at 10:36
  • please let me understand what you are saying above – Coas Mckey Nov 03 '15 at 10:37
  • Can you tell me which wordings you're unable to understand ? – Sharp Edge Nov 03 '15 at 10:37
  • table of version ? and same table of both sides ? – Coas Mckey Nov 03 '15 at 10:38
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94082/discussion-between-sharp-edge-and-coas-mckey). – Sharp Edge Nov 03 '15 at 10:38
  • please join the discussion @Sharp Edge – Coas Mckey Nov 03 '15 at 10:42
1

Get images by parsing in bitmap format and store them in your app database, or sdcard.

I think you have downloaded the images, then you should go for saving images to your sdcard memory, with numbering so that you can check them in later launch either present or not.

If not present then download the same image and save to that location with same name.

eg: image_1, image_2, .....image_10. (downloaded images to sd card)

In Activity or wherever you want check if image_1 not present then download and write it with the same filename(file path).

Checking from the sdcard will not consume so much time, wherever you use them like in you GridView.

check link for if need, how to save image from urlto sdcard(external storage): Android - Save image from URL onto SD card

link to show images from the sdcard to GridView: http://android-er.blogspot.in/2013/10/gridview-example-load-images-to.html

That's all folks.

Thanks

Community
  • 1
  • 1
Androider
  • 3,833
  • 2
  • 14
  • 24
  • but there are several other points and you are answering me about the thing about how to download and saving them , I have used these library in my many other apps to load images and to show them without geting OOM but now there are several cases which are not easy to understand and thus creating confusions – Coas Mckey Nov 03 '15 at 08:29
  • cool , but is that saving images on sd card good ? I mean what happen if the user has not inserted the sd card or if he has no sd card ? – Coas Mckey Nov 03 '15 at 09:17
  • No problem dude, then it will occupy your phone android storage(memory) whatever, otherwise, you can also save them in local storage of you app, if you want like database, sharedpreferences. – Androider Nov 03 '15 at 09:20
  • please clear me , if I have downloaded 10 images and then save them on sd card or where ever , how to keep track that which image has beeen save where ? – Coas Mckey Nov 03 '15 at 09:24
  • you have to track them as i said by name which you used to download and write them, if that name(file on sdcard) exist then it presents else not. Wherever means it's can be done with database, sharepreferences, and also with sdcard(easiet option is sd card,). If you solve your problem which is easy, then pls do you job, and best of luck Thanks – Androider Nov 03 '15 at 09:27
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/94074/discussion-between-coas-mckey-and-androider). – Coas Mckey Nov 03 '15 at 09:30