There is an image file
inside a directory
. How to copy
this image file
into another directory
that was just created ? The two directories
are on the same internal storage
of the device :)
Asked
Active
Viewed 3.6k times
21
-
With code or generally? – Bojan Kseneman Apr 25 '15 at 15:39
-
programmatically , with code :) – pheromix Apr 25 '15 at 15:39
-
I gave you ny recursion functions. It allows copy of single file or a whole directory and all of it's childer. Cheers. – Bojan Kseneman Apr 25 '15 at 15:42
3 Answers
28
You can use these functions. The first one will copy whole directory with all children or a single file if you pass in a file. The second one is only usefull for files and is called for each file in the first one.
Also note you need to have permissions to do that
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Functions:
public static void copyFileOrDirectory(String srcDir, String dstDir) {
try {
File src = new File(srcDir);
File dst = new File(dstDir, src.getName());
if (src.isDirectory()) {
String files[] = src.list();
int filesLength = files.length;
for (int i = 0; i < filesLength; i++) {
String src1 = (new File(src, files[i]).getPath());
String dst1 = dst.getPath();
copyFileOrDirectory(src1, dst1);
}
} else {
copyFile(src, dst);
}
} catch (Exception e) {
e.printStackTrace();
}
}
public static void copyFile(File sourceFile, File destFile) throws IOException {
if (!destFile.getParentFile().exists())
destFile.getParentFile().mkdirs();
if (!destFile.exists()) {
destFile.createNewFile();
}
FileChannel source = null;
FileChannel destination = null;
try {
source = new FileInputStream(sourceFile).getChannel();
destination = new FileOutputStream(destFile).getChannel();
destination.transferFrom(source, 0, source.size());
} finally {
if (source != null) {
source.close();
}
if (destination != null) {
destination.close();
}
}
}

Bojan Kseneman
- 15,488
- 2
- 54
- 59
-
-
1you can't with this implementation because you don't know how many files are up in front nor their sizes... you would need to know this if you wanted the progress, which would ultimatly slow this down. If you want performance, screw progress. – Bojan Kseneman May 23 '18 at 12:58
-
1You can use `try (FileChannel source = new FileInputStream(sourceFile).getChannel(); FileChannel destination = new FileOutputStream(destFile).getChannel())` to avoid using `finally` – Sergey Stasishin Jun 07 '19 at 08:24
-
7
If you want to copy image programtically then use following code.
File sourceLocation= new File (sourcepath);
File targetLocation= new File (targetpath);
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();

Prashant Bhoir
- 900
- 6
- 8
-1
** Use FileUtils This Is Simple Fast And Best method and Download Jar file from here**
public void MoveFiles(String sourcepath) {
File source_f = new File(sourcepath);
String destinationPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/WhatsappStatus/yourfilename.mp4";
File destination = new File(destinationPath);
try
{
FileUtils.copyFile(source_f , destination);
}
catch (IOException e)
{
e.printStackTrace();
}
}

Community
- 1
- 1

Sahil Choudhary
- 181
- 1
- 7