4

I have a listactivity app forming multiple rows. Each row opens an activity containing views, one of them is a button, when clicked open infinite gallery class ( images stored in RES => drawable folder inside the app), each image has button under it, when pressed it saves the image to SD card directory in a folder named ( saved_images ) .

I'm using SharedPreferences in gallery class to store all the images in sequential order, that works fine -

but I'm trying to :

  1. Prevent a repeat of images saved in the SD Card folder (saved_images):

    Say you saved image-1 successfully then you press the same button under image-1 it will be saved again in SD card folder so finally you will have same image (image-1) twice ,

    so what i want to get : when i press a button under image already saved a Toast 'image already saved must rise, so all app images will be saved once in Sd card folder.

  2. Keep saving images in sequential order after a reinstall:

    after installing the app in device and saving some images in folder ( saved_images ) which already created in SD card , suppose you uninstall the app from device and keep the ( saved_images ) folder in SD card , then reinstall the app again and want to save some new images, what happens is the new images replace the previously saved images,

    but I want it to : continue saving new images with previous saved images in sequential order.

Code used to save images to SDcard :

public void onClick(View arg0) {
    String root = Environment.getExternalStorageDirectory().toString();
    File myDir = new File(root + "/saved_images");
    if (!myDir.exists()) {
        myDir.mkdirs();
        SharedPreferences saveNumber = mContext.getApplicationContext()
                .getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editorset = saveNumber.edit();
        editorset.putInt("lastsavednumber", 0);
        editorset.commit();
    }
    bm = BitmapFactory.decodeResource(mContext.getResources(),
            images[itemPos]);
    holder.image.setImageBitmap(bm);

    SharedPreferences savedNumber = mContext.getSharedPreferences(
            PREFS_NAME, 0);
    int lastSavedNumber = savedNumber.getInt("lastsavednumber", 0);
    lastSavedNumber++;
    String fname = "Image-" + lastSavedNumber + ".png";
    File file = new File(myDir, fname);
    if (file.exists()) {
        file.delete();
    }
    try {
        FileOutputStream out = new FileOutputStream(file);
        bm.compress(Bitmap.CompressFormat.PNG, 100, out);
        out.flush();
        out.close();
    } catch (Exception e) {
        e.printStackTrace();
    }
    SharedPreferences saveNumber = mContext.getApplicationContext()
            .getSharedPreferences(PREFS_NAME, 0);
    SharedPreferences.Editor editorset = saveNumber.edit();
    editorset.putInt("lastsavednumber", lastSavedNumber);
    editorset.commit();
    Toast.makeText(mContext, "Saved", Toast.LENGTH_SHORT).show();
    vi.setTag(holder);
}
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
androidqq6
  • 1,526
  • 2
  • 22
  • 47
  • In the function which treats the click - set the button to disabled , then Thread.sleep for 1000 and at the end of the function re-enable the button again. For the double saved images - try searching for the latest saved image in the folder, then continue the sequence from that number on. – g00dy Apr 22 '13 at 13:32
  • You want to have the user not save an image twice - what if the user closes your app then reopens it - should he then be able to save the image twice or not ? – Mr_and_Mrs_D May 06 '13 at 15:05
  • @Mr_and_Mrs_D if the user closes my app then reopens it he must also not able to save images twice ,as folder still there in sd card , but if he delete manually the folder (saved_images) , then that folder created again by open app again or reinstall it , so now he is able to save any image . – androidqq6 May 06 '13 at 15:59
  • The only way I can think of it is to keep the CRCs of the photos and the modification date of the directory and whenever you save a file check for its CRC - but wouldn't this be too much overhead ? – Mr_and_Mrs_D May 06 '13 at 16:24
  • @Mr_and_Mrs_D please what do you mean by CRCs also i think is using (onSaveInstanceState) will help or not , i think i need to create database for saved/unsaved state for all images is this right, thanks – androidqq6 May 06 '13 at 16:43

2 Answers2

1

You can check on during save the image that image is already exist or not. If you are save image with different name then compare the image matrix to saved images matrix if it match with any image matrix then show the Toast message.

Second if folder is already exist then you did not delete it. the n when you reinstall your app then the content of folder remain there.

Try this code to check the images is same or not I get this code a SO answer

bool imagesAreEqual(Image i1, Image i2)
{

    if (i1.getHeight() != i2.getHeight) return false;
    if (i1.getWidth() != i2.getWidth) return false;

    for (int y = 0; y < i1.getHeight(); ++y)
       for (int x = 0; x < i1.getWidth(); ++x)
            if (i1.getPixel(x, y) != i2.getPixel(x, y)) return false;

    return true;
}

for folder issue in your code you did not delete folder if folder exist.

tkanzakic
  • 5,499
  • 16
  • 34
  • 41
Singhak
  • 8,508
  • 2
  • 31
  • 34
1

To get the last filename from the folder you could :

String directory = "/parent/of/saved_images/";
File savedImagesDir = new File(directory, "saved_images");
if (savedImagesDir.mkdirs() && savedImagesDir.isDirectory()) {
    // you just created the dir
} else {
    File[] files = savedImagesDir.listFiles();
    if (files == null) {
        // oops savedImagesDir is not a directory
    } else {
        int max = -1;
        Pattern p = Pattern.compile("-[\\d]+");
        for (File file : files) {
            Log.w("file", file.getName());
            Matcher matcher = p.matcher(file.getName());
            if (matcher.find()) {
                final String group = matcher.group();
                final String[] split = group.split("-");
                Log.w("group", file.getName());
                Log.w("split", split[1]);
                int curent = Integer.parseInt(split[1]);
                Log.w("curent", curent + "");
                if (curent > max) max = curent;
            }
        }
        Log.w("max", max + "");
        SharedPreferences saveNumber = mContext.getApplicationContext()
                    .getSharedPreferences(PREFS_NAME, 0);
        SharedPreferences.Editor editorset = saveNumber.edit();
        editorset.putInt("lastsavednumber", 0);
        editorset.commit();
    }
}

You need to do this whenever you first run your activity after an uninstall. Of course the regular expression I used could fail if the user puts stuff there or if you change your naming scheme etc. You must do this carefully

As for the "not saving again" you should store (in shared preferences) a checksum (hash) of the photos in the saved_images. Whenever you try to put a file in there calculate its hash and if the hash is already stored then display your toast ("image already saved") otherwise put it in.

To get you started with hashes in java look here and here

Community
  • 1
  • 1
Mr_and_Mrs_D
  • 32,208
  • 39
  • 178
  • 361
  • thanks for your answer but im new to android development and cant understand second point , if you can translated to code i will be appreciated , which is (As for the "not saving again" you should store (in shared preferences) a checksum (hash) of the photos in the saved_images. Whenever you try to put a file in there calculate its hash and if the hash is already stored then display your toast ("image already saved") otherwise put it in.),thanks – androidqq6 May 06 '13 at 21:01
  • @androidqq6 google for "java md5" - this is not android developemnet it is general programming - hashes (like md5) is a small say string which identifies uniquelly a file - check the links I provided - I will try to write something when I find some time - did the code I posted work for the other issue ? – Mr_and_Mrs_D May 06 '13 at 21:53
  • after uninstall and reinstall app and save first image only replace old first image in folder but rest of image continue to be saved in folder ( first image saving still the problem) – androidqq6 May 06 '13 at 23:00
  • after uninstall and reinstall app and save first image only replace old first image in folder but rest of image continue to be saved in folder ( first image saving still the problem) ,this in gingerbread running device but in ice cream suppose you saved first time 3 image then uninstall app then install app again then want to save first image till third image toast rised saved but go to folder you will see images thumb of previous one but when open images it open the new images saved then fourth image saved normally – androidqq6 May 06 '13 at 23:33
  • also whats happen with device running ice cream happen with device running jelly bean – androidqq6 May 07 '13 at 08:29