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){…

Continue ReadingImplementing Trash Bin in Phimpme Android Application

Option to rename albums in the Phimpme Android Application

In the Phimpme Android application, users can perform various operations on the albums available such as creating a zip file of the album, hiding an album and many more. However, one another useful functionality that has been added to the Phimpme Android application is the option to rename albums. So in this post, I will be discussing the implementation of the renaming functionality. Step 1 First we need to add an option in the overflow menu to rename an album while we long-select an album or we are viewing photos inside a particular album. The option in the overflow menu can be added by integrating the following lines of code in the menu_albums.xml file(this file contains the xml code for overflow menu options provided for albums). <item android:id="@+id/renameAlbum" android:title="@string/rename" app:showAsAction="never" /> Step 2 Now after the user selects the option in the overflow menu to rename the current viewing album, an alertdialog with an edittext would be displayed to the user to enter the new name for the album. The alertdialog containing the edittext has been implemented with the following lines of code. <android.support.v7.widget.CardView xmlns:android="http://schemas.android.com/apk/res/android" xmlns:app="http://schemas.android.com/apk/vn.mbm.phimp.me" android:layout_width="match_parent" android:layout_height="wrap_content" app:cardCornerRadius="2dp" android:id="@+id/dialog_chose_provider_title"> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:orientation="vertical"> <TextView android:id="@+id/rename_title" android:layout_width="match_parent" android:textColor="@color/md_dark_primary_text" android:layout_height="wrap_content" android:background="@color/md_dark_appbar" android:padding="24dp" android:text="@string/rename_photo_action" android:textSize="18sp" android:textStyle="bold" /> <RelativeLayout android:id="@+id/container_edit_text" android:layout_width="match_parent" android:layout_height="match_parent" android:orientation="vertical" android:padding="24dp"/> </LinearLayout> </android.support.v7.widget.CardView> The screenshot of the dialog to enter the new name is provided below. Step 3 Finally, after retrieving the new name for the album entered by the user, the renameAlbum method is invoked by passing in the application context and new name as the parameters. Now inside the renameAlbum method, a new file path is created by the use of the getAlbumPathRenamed method of the StringUtils class and the current path of the album is changed to the new path generated earlier. Similarly path of each media inside the album is changed accordingly by the use of the method getPhotoPathRenamedAlbumChange of the StrinUtils class by looping through the media objects one by one. The lines of code implemented to perform the above-mentioned operations are provided below. public boolean renameAlbum(final Context context, String newName) {  found_id_album = false;  boolean success;  File newDir = new File(StringUtils.getAlbumPathRenamed(getPath(), newName));  File oldDir = new File(getPath());  success = oldDir.renameTo(newDir);  if(success) {     for (final Media m : media) {        File from = new File(m.getPath());        File to = new File(StringUtils.getPhotoPathRenamedAlbumChange(m.getPath(), newName));        scanFile(context, new String[]{ from.getAbsolutePath() });        scanFile(context, new String[]{ to.getAbsolutePath() }, new MediaScannerConnection.OnScanCompletedListener() {           @Override           public void onScanCompleted(String s, Uri uri) {              if (!found_id_album) {                 id = MediaStoreProvider.getAlbumId(context, s);                 found_id_album = true;              }              Log.d(s, "onScanCompleted: "+s);              m.setPath(s); m.setUri(uri.toString());           }        });     }     path = newDir.getAbsolutePath();     name = newName;  }  return success; } This is how we have implemented the functionality to rename particular albums 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 1.Android Developer Guide - https://developer.android.com/reference/android/media/MediaScannerConnection 2.Github-Phimpme Android Repository - https://github.com/fossasia/phimpme-android/ 3.Media Scanner tutorial -https://stackoverflow.com/questions/13270789/how-to-run-media-scanner-in-android

Continue ReadingOption to rename albums in the Phimpme Android Application