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

在Android中将文件从内部存储复制到外部存储,第1张

在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;}

感谢您的提示。



欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/zaji/5462069.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-12
下一篇 2022-12-12

发表评论

登录后才能评论

评论列表(0条)

保存