开发者社区> 问答> 正文

关于Android 关于操作文件IO的相关问题

为什么总是移动文件失败!
下面是输出打印的两句路径:

 /storage/sdcard0/dcim/Camera/IMG_20151127_094856.jpg
 /storage/sdcard0/baidu/searchbox/preset/preset4.2/pic/
    /**
     * 移动至指定文件夹
     * @param path
     * @param newPath
     * @return
     */
    public static boolean updateToFolder(String path, String newPath) {
        Log.e("==path==>>>", path);
        Log.e("==path==>>>", newPath);
        File oldfile = new File(path);
        if (!oldfile.exists()) {
            return false;
        }
        File newfile = new File(newPath);
        if (!newfile.exists()) {
            return false;
        }
        int byteread = 0;
        try {
            InputStream ipStream = new FileInputStream(oldfile); // 读入原文件
            OutputStream opStream = new FileOutputStream(newfile);
            byte[] buffer = new byte[1024];
            while ((byteread = ipStream.read(buffer)) != -1) {
                opStream.write(buffer, 0, byteread);
            }
            ipStream.close();
            opStream.close();
            oldfile.delete();
            return true;
        } catch (Exception e) {
            e.printStackTrace();
            return false;
        }
    }

展开
收起
爵霸 2016-03-25 09:13:02 1930 0
1 条回答
写回答
取消 提交回答
  • 原路径:/storage/sdcard0/dcim/Camera/a2051958524f8b085262c960866397ce.jpg
    目标文件夹:/storage/sdcard0/baidu/searchbox/preset/preset4.2/pic/
    最终路径:/storage/sdcard0/baidu/searchbox/preset/preset4.2/pic/a2051958524f8b085262c960866397ce.jpg

    我这样加了,还是不行呀!代码没抛异常,但是图片没移动过去,而且源图片还变没了。

    2019-07-17 19:13:25
    赞同 展开评论 打赏
问答排行榜
最热
最新

相关电子书

更多
58同城Android客户端Walle框架演进与实践之路 立即下载
Android组件化实现 立即下载
蚂蚁聚宝Android秒级编译——Freeline 立即下载