File.renameTo()不会重命名并返回false

时间:2017-10-25 14:01:40

标签: android file

我正在尝试创建一个应用程序,您可以从手机中选择图像并重命名。到目前为止,renameTo()返回false并且没有重命名图像,但是为什么会这样,我有点迷失。 我相信我给出了错误的文件路径名,但我需要一些指导。

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data)
{
    if (requestCode == PICK_IMAGE && data != null) {

        Uri photo = data.getData();
        String entirePath = getRealPathFromURI(this, photo);
        String extension = entirePath.substring(entirePath.indexOf("."));
        String imagePath = entirePath.substring(0, entirePath.lastIndexOf("/") + 1);
        String newImageName = "Blocks";


        File oldName = new File(entirePath);
        File newName = new File(imagePath, newImageName.trim() + extension);

        Log.i("oldName ", entirePath);
        Log.i("newName", imagePath + newImageName.trim() + extension);

        boolean renamed = oldName.renameTo(newName);

        String rename = "no";
        if (renamed)
            rename = "yes";

        Toast.makeText(MainMenuActivity.this, rename, Toast.LENGTH_LONG).show();
    }
}

这是我的文件oldname和newname

 oldName: /storage/emulated/0/DCIM/Camera/IMG_20171024_184210.jpg
 newName: /storage/emulated/0/DCIM/Camera/Blocks.jpg

我在清单中有这些权限

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

感谢您的任何意见!

0 个答案:

没有答案