1

I'm trying to create an app in Android, and part of it's functionality is renaming .jpg file extensions to .jpeg file extensions. However, it's not working.

filepath is the path of the .jpg file, and doThings() is what it does after the file has been renamed.

Here is my code:

// Create new string to store edited file path
String newfilepath = filepath;

// Create new file to be used for renaming
File file1 = new File(filepath);

// Remove JPG extension
newfilepath = newfilepath.substring(0, newfilepath.length() - 3);

// Replace with JPEG extension
newfilepath += "jpeg";

// Add new file for renaming purposes
File file2 = new File(newfilepath);

// Rename file from JPG to JPEG
boolean rename = file1.renameTo(file2);

// Check if file renaming was successful
if(rename) {
    // Does things
    doThings(newfilepath);
}

Note: I also tried changing file1.renameTo(file2); to this:

boolean test = file1.renameTo(file2);
System.out.println("Renamed? " + test);

And received this in logcat:

I/System.out: Renamed? false

Also, to prove it's not a permissions issue, here is the AndroidManifest.xml file:

<uses-permission
    android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission
    android:name="android.permission.READ_EXTERNAL_STORAGE" />

And I do request permissions in the Android 6+ format here:

public void getPermissions(View view) {
    String[] perms = { Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE };
    if (EasyPermissions.hasPermissions(this, perms)) {
        // We have permissions, send message
        Toast.makeText(this, "Select an image.", Toast.LENGTH_SHORT).show();
        selectFile();
    } else {
        // We don't have permissions
        Toast.makeText(this, "Permissions are required", Toast.LENGTH_SHORT).show();
        // Ask again
        ActivityCompat.requestPermissions(MainActivity.this,
                perms, PERMISSIONS_MULTIPLE_REQUEST);
    }
}

Help is appreciated, thanks!

EDIT: I've been testing and getting weird results. I will update this later today.

user4775991
  • 127
  • 2
  • 14

0 Answers0