Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is a part of the Java code on android that is going to take a picture from

ID: 3868655 • Letter: T

Question

This is a part of the Java code on android that is going to take a picture from gallery and upload it to the Firebase file storage.

I am trying to change the image name to a random name while keeping the extention.

@Override protected void onActivityResult (int requestCode, int resultCode, Intent data) t super.onActivityResult (requestCode, resultCode, data) if (requestcode-RESULTIMAGE && resultCode-RESULTOK && data!-null){ - - selectedImagedata.getData) imageDisplay.setImageURI (selectedImage); private void uploadFile ) [ if != null) (selected!mage FirebaseUser userfirebaseAuth.getCurrentUser uploadPath mStorage . child (user. getEmail ( ) ) . child (selectedImage. get Last PathSegment ( ) ) ; uploadPath.putFile (selectedInage) .addonsuccesslistener (new OnsuccessListenerUploadlask.TaskSnapshot>1 { @Override public void onSuccess (UploadTask.TaskSnapshot taskSnapshot)

Explanation / Answer

Hi,

Please find the answer below:-

==================================================================================

Take the imagename in the variable imgname and then proceed according to the code snippet below:-

CODE SNIPPET:-

===================================================================================

String fname = imgname;
File fdn = new File(fname);
String[] filetype = fname.split("\."); \It will split the filename and extension
System.out.println("The existing imagename is:");
System.out.println(fname+filetype[1]); \will show existing filename with the existing extension
System.out.println("The new imagename with existing extension is below:");
String newfname = "newnameyouwant."+filetype[1]; \specify the name you want at newnameyouwant
File newfdn = new File(newfname);
System.out.println("New imagename "+newfname);
boolean renameimg = fdn.renameTo(newfdn); enaming the file
if(renameimg)
System.out.println("Image Renamed with existing extension successful");

========================================================================================

The code snippet will rename the imagename only.It will not change the extension of the image.

Please let me know in case any queries.