Implementing Trash Bin in Phimpme Android Application
In the Phimpme Android application, users can perform various operations/actions on the photos available, some of the important operations involve renaming an image, sharing an image, deleting image etc. However, when the user performs delete operation for image/images the images are permanently deleted from the storage and there is no option to restore/recover the deleted image. Now imagine a situation where the user accidentally or in a rush deletes photo/photos, and now there is no possible way for the user to recover those accidentally deleted photos, so in such circumstances, the Trash Bin feature could prove to be a breather for the user. So, in this blog post, I will be discussing the implementation of the Trash Bin feature. Step 1 Firstly, we need to implement the functionality to move the images to bin folder whenever the delete operation is performed. For this, we’d programmatically create a folder naming it .nomedia, so that this folder is not picked up by any other similar application while scanning folders. Now when the user deletes images, a check would be performed first to determine whether the bin folder already exists or not and actions would be performed accordingly(a folder would be created if it doesn’t already exist and the selected photos for deletion would be moved to the bin folder or the deleted photos would be moved directly to the bin folder if its there). Code snippets used to implement the bin folder is provided below. private boolean addToTrash(){ int no = 0; boolean succ = false; File file = new File(Environment.getExternalStorageDirectory() + "/" + ".nomedia"); if(file.exists() && file.isDirectory()){ if(!all_photos && !fav_photos && editMode){ no = getAlbum().moveSelectedMedia(getApplicationContext(), file.getAbsolutePath()); }else if(all_photos && !fav_photos && editMode){ no = getAlbum().moveAllMedia(getApplicationContext(), file.getAbsolutePath(), selectedMedias); }else if(!editMode && !all_photos && !fav_photos){ no = getAlbum().moveAllMedia(getApplicationContext(), file.getAbsolutePath(), getAlbum().getMedia()); } else{ if(file.mkdir()){ if(!all_photos && !fav_photos && editMode){ no = getAlbum().moveSelectedMedia(getApplicationContext(), file.getAbsolutePath()); }else if(all_photos && !fav_photos && editMode){ no = getAlbum().moveAllMedia(getApplicationContext(), file.getAbsolutePath(), selectedMedias); }else if(!editMode && !all_photos && !fav_photos){ no = getAlbum().moveAllMedia(getApplicationContext(), file.getAbsolutePath(), getAlbum().getMedia()); } // no = getAlbum().moveSelectedMedia(getApplicationContext(), file.getAbsolutePath()); } // clearSelectedPhotos(); return succ; } Step 2 Now if all the photos(selected by the user for deletion) are successfully moved to the bin folder, realm objects corresponding to those image files would be created and added to the realm database. The corresponding Realm object would consist of attributes namely oldpath, trashbinpath, time of delete and duration. The realm objects would be used at the time of implementing restore functionality for the trash bin images. The realm model class representing the Trash bin object is provided below. public class TrashBinRealmModel extends RealmObject { @PrimaryKey private String trashbinpath; private String oldpath; private String datetime; private String timeperiod; public TrashBinRealmModel(){ } public TrashBinRealmModel(String oldpath, String newpath, String datetime, String timeperiod){ this.oldpath = oldpath; this.trashbinpath = newpath; this.datetime = datetime; this.timeperiod = timeperiod; } public void setTrashbinpath(String trashbinpath){ this.trashbinpath = trashbinpath; } public String getTrashbinpath(){ return trashbinpath; } public void setDatetime(String datetime){ this.datetime = datetime; } public String getDatetime(){ return datetime; } public void setOldpath(String oldpath){…
