在Android中将文件从内部复制到外部存储
我的应用(Android API 15)生成一张照片并将其存储在内部存储器的文件夹中。现在,我想将此文件复制到外部存储器内的另一个文件夹,例如/sdcard/myapp
。我尝试以下方法:在Android中将文件从内部复制到外部存储
方法#1:
private void copyFile(File src, File dst) throws IOException {
File from = new File(src.getPath());
File to = new File(dst.getPath());
from.renameTo(to);
}
方法2:
private void copyFile(File src, File dst) throws IOException {
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
inChannel = new FileInputStream(src).getChannel();
outChannel = new FileOutputStream(dst).getChannel();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
方法3:
private void copyFile(File src, File dst) throws IOException {
FileInputStream inStream = new FileInputStream(src);
if (!dst.exists()) {
dst.mkdir();
}
if (!dst.canWrite()) {
System.out.print("CAN'T WRITE");
return;
}
FileOutputStream outStream = new FileOutputStream(dst);
FileChannel inChannel = inStream.getChannel();
FileChannel outChannel = outStream.getChannel();
inChannel.transferTo(0, inChannel.size(), outChannel);
inStream.close();
outStream.close();
}
这些方法都没有解决我的任务。在检查了一些相关的话题,而且我发现唯一的建议是验证
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
执意AndroidManifest.xml
和它确实存在。
该方法#1完成执行,但没有文件夹和文件被复制。
在方法#2,应用程序失败,并在outChannel = new FileOutputStream(dst).getChannel();
异常java.lang.NullPointerException
,但对象dst为不是空。
在办法#3,我决定以验证目标对象存在,它会创建如果需要的文件夹,但是当我检查,如果我能写,检查返回false
。
我尝试了一些其他方法,它们成功地创建了一个空文件夹,但没有真正复制文件。
由于这是我迈向Android的第一步,我觉得我错过了一些小事情。请指点一下,如何将文件从一个文件夹复制到Android中的另一个文件夹,包括文件从内部移动到外部内存。
谢谢。
我解决了我的问题。问题是在目标路径中,在原有代码:
File dst = new File(dstPath);
变量dstPath
有充分的目标路径,包括文件名,这是错误的。下面是正确的代码片段:
String dstPath = Environment.getExternalStorageDirectory() + File.separator + "myApp" + File.separator;
File dst = new File(dstPath);
exportFile(pictureFile, dst);
private File exportFile(File src, File dst) throws IOException {
//if folder does not exist
if (!dst.exists()) {
if (!dst.mkdir()) {
return null;
}
}
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
File expFile = new File(dst.getPath() + File.separator + "IMG_" + timeStamp + ".jpg");
FileChannel inChannel = null;
FileChannel outChannel = null;
try {
inChannel = new FileInputStream(src).getChannel();
outChannel = new FileOutputStream(expFile).getChannel();
} catch (FileNotFoundException e) {
e.printStackTrace();
}
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
return expFile;
}
感谢您的提示。
谢谢。你的方式比写缓冲区快得多。 – TruongHieu
可能是路径问题? – Proxytype
@Proxytype,关于路径,我这样做:'String dstPath = Environment.getExternalStorageDirectory()+ File.separator +“myapp”+ File.separator +“IMG_”+ timeStamp +“.jpg”; 文件dst =新文件(dstPath);'。我的目的地路径应该包含文件的名称还是文件夹?为什么'new FileOutputStream(dst).getChannel();'即使'dst'被填充并且存储上有可用空间也会返回null? –
尝试在写入之前创建目标文件,File dest = new File(path);检查它是否在设备上创建...也给它的名称..文件到=新文件(dst.getPath()+“/ myname”); – Proxytype