在SD卡中移动文件

Joh*_*ohn 3 file-io android

我一直试图在SD卡中移动文件,但无济于事:这是代码:

try {
            File sd=Environment.getExternalStorageDirectory();
            // File (or directory) to be moved
            String sourcePath="mnt/sdcard/.Images/"+imageTitle;
            File file = new File(sd,sourcePath);
            // Destination directory
            String destinationPath="mnt/sdcard/"+imageTitle;
            File dir = new File(sd,destinationPath);

            // Move file to new directory
            boolean success = file.renameTo(new File(dir, file.getName()));
            if (!success) {
                handler.post(new Runnable(){
                    @Override
                    public void run() {
                        Toast.makeText(getApplicationContext(), "File moved", Toast.LENGTH_LONG).show();
                    }
                });
            }

        } 
        catch (Exception e) {
        }
Run Code Online (Sandbox Code Playgroud)

我不知道wasup.Will欣赏帮助.

Otr*_*tra 12

第一:如果你已经得到了外部目录,也没有必要将其添加到您的开头sourcepathdestinationpath

其次,destinationPath似乎没必要,因为它看起来只是想将它移动到sdcard的根文件夹.

它应该是

File sd=Environment.getExternalStorageDirectory();
// File (or directory) to be moved
String sourcePath="/.Images/"+imageTitle;
File file = new File(sd,sourcePath);
// Destination directory
boolean success = file.renameTo(new File(sd, imageTitle));
Run Code Online (Sandbox Code Playgroud)