【发布时间】:2016-01-14 16:19:14
【问题描述】:
我正在尝试在 android 中复制文件。我有文件的文件路径。我想将它复制到另一个具有不同文件名的文件夹。我正在使用下面的代码,但它不起作用。我的文件是视频文件. 我得到错误-
/storage/emulated/0/testcopy.mp4: open failed: EISDIR (Is a directory)
下面是我的代码
File source=new File(filepath);
File destination=new File(Environment.getExternalStorageDirectory()+ "/testcopy.mp4");
copyFile(source.getAbsolutePath(),destination.getAbsolutePath());
private void copyFile(String inputPath, String outputPath) {
InputStream in = null;
OutputStream out = null;
try {
//create output directory if it doesn't exist
File dir = new File (outputPath);
if (!dir.exists())
{
dir.mkdirs();
}
in = new FileInputStream(inputPath );
out = new FileOutputStream(outputPath);
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1) {
out.write(buffer, 0, read);
}
in.close();
in = null;
// write the output file (You have now copied the file)
out.flush();
out.close();
out = null;
} catch (FileNotFoundException fnfe1) {
Log.e("tag", fnfe1.getMessage());
}
catch (Exception e) {
Log.e("tag", e.getMessage());
}
}
【问题讨论】:
标签: android file directory file-copying