Option to Rename an Image in Phimpme Android Application
In the Phimpme Android application, users can perform various operations on images such as editing an image, sharing an image, moving the image to another folder, printing a pdf version of the image and many more. However, another important functionality that has been implemented is the option to rename an image. So in this blog post, I will be discussing how we achieved the functionality to rename an image.
Step 1
First, we need to add an option in the overflow menu to rename the image being viewed. The option to rename an image has been added by implementing the following lines of code in the menu_view_pager.xml file.
<item android:id=“@+id/rename_photo” app:showAsAction=“never” android:title=“@string/Rename”/> |
Step 2
Now after the user chooses the option to rename any image from the overflow menu, an alert dialog with an edittext would be displayed to the user with the old name and provide the user to add a new name for the photo. The dialog box with edittext has been implemented with the following lines of XML code.
<android.support.v7.widget.CardView android:layout_width=“match_parent” android:layout_height=“wrap_content” app:cardCornerRadius=“2dp” android:id=“@+id/description_dialog_card”> <ScrollView android:layout_width=“match_parent” android:layout_height=“match_parent”> <LinearLayout android:layout_width=“match_parent” android:layout_height=“wrap_content” android:orientation=“vertical”> <TextView android:id=“@+id/description_dialog_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/type_description” android:textSize=“18sp” android:textStyle=“bold” /> <LinearLayout android:id=“@+id/rl_description_dialog” android:layout_width=“match_parent” android:layout_height=“wrap_content” android:orientation=“horizontal” android:padding=“15dp”> <EditText android:id=“@+id/description_edittxt” android:layout_width=“fill_parent” android:layout_height=“wrap_content” android:padding=“15dp” android:hint=“@string/description_hint” android:textColorHint=“@color/grey” android:layout_margin=“20dp” android:gravity=“top|left” android:inputType=“textCapSentences|textMultiLine” android:maxLength=“2000” android:maxLines=“4” android:selectAllOnFocus=“true”/> <ImageButton android:layout_width=“@dimen/mic_image” android:layout_height=“@dimen/mic_image” android:layout_alignRight=“@+id/description_edittxt” app2:srcCompat=“@drawable/ic_mic_black” android:layout_gravity=“center” android:background=“@color/transparent” android:paddingEnd=“10dp” android:paddingTop=“12dp” android:id=“@+id/voice_input”/> </LinearLayout> </LinearLayout> </ScrollView> </android.support.v7.widget.CardView> |
The screenshot displaying the dialog with edittext is provided below.
Step 3
Now after retrieving the new name entered by the user for the photo we would extract the extension of the old name which can be .jpg, .png etc. Thereafter we’d need to create a new File object passing in the new path of the image(the path folder remains the same only the image name gets changed to a new one) as the constructor parameter. Now using the renameTo method of the File class the old image file can be renamed. However, with this rename operation the image reference would not be automatically updated in the MediaStore database. So we’d need to delete the old file path from the Android system MediaStore database which keeps a URI reference to all the media files present on the device. At last, we’d need to invoke the MediaScanner class to scan all the media files so that the new file path of the renamed image is scanned and is picked up by the MediaStore database. This can be done with the help of an action intent to initiate the media scan action. The code changes implemented to perform the above-mentioned operation is given below.
public void onClick(View dialog) { if (editTextNewName.length() != 0) { int index = file.getPath().lastIndexOf(“/”); String path = file.getPath().substring(0, index); File newname = new File(path + “/” + editTextNewName.getText().toString() + “.” + imageextension); if(file.renameTo(newname)){ ContentResolver resolver = getApplicationContext().getContentResolver(); resolver.delete( MediaStore.Images.Media.EXTERNAL_CONTENT_URI, MediaStore.Images.Media.DATA + “=?”, new String[] { file.getAbsolutePath() }); Intent intent = new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE); intent.setData(Uri.fromFile(newname)); getApplicationContext().sendBroadcast(intent); } if(!allPhotoMode){ int a = getAlbum().getCurrentMediaIndex(); getAlbum().getMedia(a).setPath(newname.getPath()); }else { listAll.get(current_image_pos).setPath(newname.getPath()); } renameDialog.dismiss(); SnackBarHandler.showWithBottomMargin(parentView, getString(R.string.rename_succes), navigationView .getHeight()); } else { SnackBarHandler.showWithBottomMargin(parentView, getString(R.string.insert_something), navigationView.getHeight()); editTextNewName.requestFocus(); } } }); |
This is how we have implemented the functionality to rename an 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
1.Android Developer documentation-https://developer.android.com/reference/java/io/File
2.Github-Phimpme Android Repository – https://github.com/fossasia/phimpme-android/
3.Renaming a file in java – http://stacktips.com/tutorials/java/how-to-delete-and-rename-a-file-in-java
You must be logged in to post a comment.