Java 在Android中将文件从内部存储复制到外部存储

Java 在Android中将文件从内部存储复制到外部存储,java,android,file-io,copy,android-external-storage,Java,Android,File Io,Copy,Android External Storage,我的应用程序(Android API 15)制作一张图片并将其存储在内存文件夹中。现在,我想将此文件复制到外部存储器中的另一个文件夹,例如/sdcard/myapp。我尝试了以下方法: 方法#1: private void copyFile(文件src、文件dst)引发IOException{ filefrom=新文件(src.getPath()); File to=新文件(dst.getPath()); 由.更名为(至);; } 方法#2: private void copyFile(文件

我的应用程序(Android API 15)制作一张图片并将其存储在内存文件夹中。现在,我想将此文件复制到外部存储器中的另一个文件夹,例如
/sdcard/myapp
。我尝试了以下方法:

方法#1:

private void copyFile(文件src、文件dst)引发IOException{
filefrom=新文件(src.getPath());
File to=新文件(dst.getPath());
由.更名为(至);;
}

方法#2:

private void copyFile(文件src、文件dst)引发IOException{
FileChannel inChannel=null;
filechanneloutchannel=null;
试一试{
inChannel=newfileinputstream(src).getChannel();
outChannel=newfileoutputstream(dst).getChannel();
}catch(filenotfounde异常){
e、 printStackTrace();
}
试一试{
inChannel.transferTo(0,inChannel.size(),outChannel);
}最后{
如果(inChannel!=null)
inChannel.close();
如果(输出通道!=null)
outChannel.close();
}
}

方法#3:

private void copyFile(文件src、文件dst)引发IOException{
FileInputStream inStream=新的FileInputStream(src);
如果(!dst.exists()){
dst.mkdir();
}
如果(!dst.canWrite()){
系统输出打印(“无法写入”);
返回;
}
FileOutputStream outStream=新的FileOutputStream(dst);
FileChannel inChannel=inStream.getChannel();
FileChannel outChannel=outStream.getChannel();
inChannel.transferTo(0,inChannel.size(),outChannel);
流内关闭();
exptream.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=新文件(dstPath);
变量
dstPath
具有完整的目标路径,包括文件名,这是错误的。以下是正确的代码片段:

String dstPath=Environment.getExternalStorageDirectory()+File.separator+“myApp”+File.separator;
文件dst=新文件(dstPath);
导出文件(图片文件,dst);

private File exportFile(File src,File dst)引发IOException{
//如果文件夹不存在
如果(!dst.exists()){
如果(!dst.mkdir()){
返回null;
}
}
字符串时间戳=新的SimpleDateFormat(“yyyyMMdd_HHmmss”)。格式(新日期();
File expFile=new File(dst.getPath()+File.separator+“IMG_”+timeStamp+“.jpg”);
FileChannel inChannel=null;
filechanneloutchannel=null;
试一试{
inChannel=newfileinputstream(src).getChannel();
outChannel=新文件outputstream(expFile).getChannel();
}catch(filenotfounde异常){
e、 printStackTrace();
}
试一试{
inChannel.transferTo(0,inChannel.size(),outChannel);
}最后{
如果(inChannel!=null)
inChannel.close();
如果(输出通道!=null)
outChannel.close();
}
返回EXP文件;
}
谢谢你的提示。

Kotlin


可能是路径问题?@Proxytype,关于路径,我是这样做的:
String dstPath=Environment.getExternalStorageDirectory()+File.separator+“myapp”+File.separator+“IMG_”+timeStamp+“.jpg”;文件dst=新文件(dstPath)。我的目标路径应该包括文件名还是文件夹名?为什么
新建FileOutputStream(dst).getChannel()dst
已填充且存储器上有可用空间,code>仍返回null?尝试在写入目标文件之前创建目标文件,file dest=new file(path);检查它是否是在设备上创建的。。。也给它起个名字。。File to=新文件(dst.getPath()+“/myname”);我已经这样做了,甚至更多:
if(!dst.exists()){isCreated=dst.mkdir();}
,在这个代码之后,变量
isCreated
等于
false
。奇怪的是,为什么我可以创建初始文件,但不能将其复制到另一个文件夹,甚至不能创建文件夹。内部路径是否包括数据目录?按照这个答案回答,谢谢。您的方式比编写缓冲区快得多。
    // video is some file in internal storage
    val to = File(Environment.getExternalStorageDirectory().absolutePath + "/destination.file")
    video.copyTo(to, true)