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 :
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.
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);
}