Copy files from a folder of SD card into another folder of SD card
37,452
Solution 1
An improved version of that example:
// If targetLocation does not exist, it will be created.
public void copyDirectory(File sourceLocation , File targetLocation)
throws IOException {
if (sourceLocation.isDirectory()) {
if (!targetLocation.exists() && !targetLocation.mkdirs()) {
throw new IOException("Cannot create dir " + targetLocation.getAbsolutePath());
}
String[] children = sourceLocation.list();
for (int i=0; i<children.length; i++) {
copyDirectory(new File(sourceLocation, children[i]),
new File(targetLocation, children[i]));
}
} else {
// make sure the directory we plan to store the recording in exists
File directory = targetLocation.getParentFile();
if (directory != null && !directory.exists() && !directory.mkdirs()) {
throw new IOException("Cannot create dir " + directory.getAbsolutePath());
}
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();
}
}
Got some better error handling and better handles if the passed target file lies in a directory that does not exist.
Solution 2
See the example here. The sdcard is external storage, so you can access it via getExternalStorageDirectory
.
Solution 3
yes it is possible and im using below method in my code . Hope use full to you:-
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();
}
}
Solution 4
To move files or directories, you can use File.renameTo(String path)
function
File oldFile = new File (oldFilePath);
oldFile.renameTo(newFilePath);
Solution 5
Kotlin code
fun File.copyFileTo(file: File) {
inputStream().use { input ->
file.outputStream().use { output ->
input.copyTo(output)
}
}
}
fun File.copyDirTo(dir: File) {
if (!dir.exists()) {
dir.mkdirs()
}
listFiles()?.forEach {
if (it.isDirectory) {
it.copyDirTo(File(dir, it.name))
} else {
it.copyFileTo(File(dir, it.name))
}
}
}
Author by
Siva Kumar
Updated on July 09, 2022Comments
-
Siva Kumar almost 2 years
Is it possible to copy a folder present in sdcard to another folder present the same sdcard programmatically ??
If so, how to do that?
-
Siva Kumar about 13 yearsyea .. I know that sdcard can be accessed using the getExternalStorageDirectory .. but how can I copy from one folder to another folder of the same sdcard ?? thanks
-
Vladimir Ivanov about 13 yearsFile source = new File(Environment.getExternalStorageDirectory(), "sourcedir"); File dest = new File(Environment.getExternalStorageDirectory(), "destDir"); Then use the code from the link.
-
Siva Kumar about 13 years. sorry ... I am unable to understand that as I am new to this ... in the link he has given for a file to copy into sdcard by converting into byte stream .. but how to copy the whole directory ??
-
Vladimir Ivanov about 13 yearsThe directory is also a File.
-
Ankit over 10 yearsThis will remove file from source directory.
-
Pro Mode almost 7 years@VladimirIvanov above link is broken.