在SD卡中移动文件

时间:2011-07-05 20:17:10

标签: android file-io

我一直试图在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) {
        }

我不知道wasup。会很感激帮助。

1 个答案:

答案 0 :(得分: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));