Option to Restore Deleted Images in Phimpme Android Application
In the Phimpme Android application, users can perform various operations on the images including renaming an image, sharing images, deleting images from the storage etc. However with the implementation of Trash Bin feature in the app, the user is now provided with the option to restore back the deleted images. Whenever the delete operation is performed, the selected images are moved to the Trash Bin and the user has the option to either delete the photos permanently or restoring back the deleted photos by navigating to the Trash bin section. So, in this post I’d be discussing the implementation of restore image/images functionality.
Step 1
Firstly, we need to add restore option in the popup menu provided in the itemview in the TrashView section. Every item in the Trashbin section displays a popup menu with two options-restore and delete permanently. The popup menu along with the two options have been implemented by adding the following lines of code.
<?xml version=“1.0” encoding=“utf-8”?> <menu xmlns:android=“http://schemas.android.com/apk/res/android” xmlns:app=“http://schemas.android.com/apk/res-auto”> <item android:id=“@+id/restore_option” android:title=“@string/restore_photo” app:showAsAction=“never” /> <item android:id=“@+id/delete_permanently” android:title=“@string/delete_permanently” app:showAsAction=“never” /> </menu> |
Step 2
Now after the user opts to restore a particular image, the corresponding realm object for the image would be retrieved from the Realm database. This realm object would contain attributes namely trashbinpath, oldpath, datetime, timeperiod. Here the oldpath attribute contains the path where it was stored in the device storage before the delete operation was performed on the image. Thereafter to restore the image to its old path, a move operation would be performed which would move the image from the TrashBin section to the album it was stored in before deletion. With this move operation, the image is restored to its old path and is visible in the respective album resulting in the removal/deletion of the image from the TrashBin section. The method implemented to perform the above mentioned operations is provided below.
private boolean restoreImage(TrashBinRealmModel trashBinRealmModel){ String oldpath = trashBinRealmModel.getOldpath(); boolean success = false; try { String from = trashBinRealmModel.getTrashbinpath(); if (success = moveMedia(context, from, targetDir)) { scanFile(context, new String[]{ from, StringUtils.getPhotoPathMoved(pathofmed, targetDir) }); } } catch (Exception e) { e.printStackTrace(); } return success; } |
Step 3
In the previous step, the move operation for the restore feature was performed by the restoreImage method which inturn uses method moveMedia() to get the job done. So in this step, I’d be discussing about the implementation of the moveMedia() method. Inside the moveMedia() method, moveFile method of ContentHelper class is invoked passing-in context, sourcefile, and the destination folder as the parameters. Thereafter a normal rename operation is performed by the use of the renameTo method to move the file to destination folder. Now if the rename operation is performed successfully, the file is moved to its old path whereas if the rename operation fails to get the job done, a copy operation is initiated to copy the file to the destination folder and the image the is deleted from the trashbin section if the copy operation is successful. The code snippets used to implement the moveMedia and moveFile are provided below.
private boolean moveMedia(Context context, String source, String targetDir) { File from = new File(source); File to = new File(targetDir); return ContentHelper.moveFile(context, from, to); } |
public static boolean moveFile(Context context, @NonNull final File source, @NonNull final File targetDir) { // First try the normal rename. File target = new File(targetDir, source.getName()); boolean success = source.renameTo(target); if (!success) { success = copyFile(context, source, targetDir); if (success) { success = deleteFile(context, source); } } //if (success) scanFile(context, new String[]{ source.getPath(), target.getPath() }); return success; } |
This is how we have implemented the functionality to restore a deleted image in the Phimpme Android application. To get the full source code, please refer to the Phimpme Android Github repository listed in the resource section below.
Resources
- Android Developer documentation –https://developer.android.com/reference/java/io/File
- Github-Phimpme Android Repository – https://github.com/fossasia/phimpme-android/
- Renaming a file in java – http://stacktips.com/tutorials/java/how-to-delete-and-rename-a-file-in-java