Prevent Duplicated Image Saved To Sd Card
Solution 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
boolimagesAreEqual(Image i1, Image i2){
if (i1.getHeight() != i2.getHeight) returnfalse;
if (i1.getWidth() != i2.getWidth) returnfalse;
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)) returnfalse;
returntrue;
}
for folder issue in your code you did not delete folder if folder exist.
Solution 2:
To get the last filename from the folder you could :
Stringdirectory="/parent/of/saved_images/";
FilesavedImagesDir=newFile(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 {
intmax= -1;
Patternp= Pattern.compile("-[\\d]+");
for (File file : files) {
Log.w("file", file.getName());
Matchermatcher= p.matcher(file.getName());
if (matcher.find()) {
finalStringgroup= matcher.group();
final String[] split = group.split("-");
Log.w("group", file.getName());
Log.w("split", split[1]);
intcurent= Integer.parseInt(split[1]);
Log.w("curent", curent + "");
if (curent > max) max = curent;
}
}
Log.w("max", max + "");
SharedPreferencessaveNumber= mContext.getApplicationContext()
.getSharedPreferences(PREFS_NAME, 0);
SharedPreferences.Editoreditorset= 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.
Post a Comment for "Prevent Duplicated Image Saved To Sd Card"