问题
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);
}
回答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.
回答2:
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
来源:https://stackoverflow.com/questions/16148242/prevent-duplicated-image-saved-to-sd-card