0

I am trying to move file from /mnt/sdcard to /mnt/extsd Currently file is stored in /mnt/sdcard/DCIM/camera after shooting a video but now i want to move this file to /mnt/extsd

I am using following code

File fromFile=new File( "/mnt/sdcard/folderpath" ,"/video.mp4");
File toFile=new File("/mnt/extsd" ,fromFile.getName());
fromFile.renameTo(toFile);

I read that renameTo dosen't work for moving in different file systems

Please help me

Hemantwagh07
  • 1,516
  • 1
  • 15
  • 27

3 Answers3

0
 try {

  java.io.FileInputStream fosfrom = new java.io.FileInputStream(fromFile);

  java.io.FileOutputStream fosto = new FileOutputStream(toFile);

  byte bt[] = new byte[1024];

  int c;

  while ((c = fosfrom.read(bt)) > 0) {

  fosto.write(bt, 0, c); //将内容写到新文件当中

  }

  fosfrom.close();

  fosto.close();

  } catch (Exception ex) {

  Log.e("readfile", ex.getMessage());

  }

  }

nick.yu
  • 89
  • 3
0

give source file where exist ur file and target location where u want to store .

public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation)
    throws IOException {

if (sourceLocation.isDirectory()) {
    if (!targetLocation.exists()) {
        targetLocation.mkdir();
    }

    String[] children = sourceLocation.list();
    for (int i = 0; i < sourceLocation.listFiles().length; i++) {

        copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]),
                new File(targetLocation, children[i]));
    }
} else {

    InputStream in = new FileInputStream(sourceLocation);

    OutputStream out = new FileOutputStream(targetLocation);

    // Copy the bits from instream to outstream
    byte[] buf = new byte[1024];
    int len;
    while ((len = in.read(buf)) > 0) {
        out.write(buf, 0, len);
    }
    in.close();
    out.close();
}

}

duggu
  • 37,851
  • 12
  • 116
  • 113
0

according to Android docs "Both paths must be on the same mount point" like it could be used for file renaming only in case of different paths. So if you want to move it you probably should copy it and then rename and then delete the source file.
But in this case you are trying not only to move the file from one FS to another but also you are trying to use /mnt/extsd which might not be able at all. Follow this question about such paths.

Community
  • 1
  • 1
Stan
  • 6,511
  • 8
  • 55
  • 87