开发者社区> 问答> 正文

关于Android 移动文件相关问题!

上午开始问的问题了,难道就没人会了吗?为什么我这个方法移动不了?代码是正常执行的,但是图片却没移动,而且要移动的那个图片也变成空的了。

    /**
     * 移动至指定文件夹
     * @param path 图片源完整路径
     * @param newPath 目标文件夹路径
     * @param name 图片名称(例如:aaa.jpg)
     * @return
     */
    public static boolean moveFile(String path, String newPath, String name) {
        File oldfile = new File(path);
        if (!oldfile.exists()) {
            return false;
        }
        File desfile = new File(newPath);
        if (!desfile.exists()) {
            desfile.mkdirs();
        }
        int size = 0;
        try {
            InputStream is = new FileInputStream(oldfile); // 读入原文件
            OutputStream os = new FileOutputStream(desfile.getAbsolutePath() + name);
            byte[] bs = new byte[1024];
            while ((size = is.read(bs)) != -1) {
                os.write(bs, 0, size);
                os.flush();
            }
            os.close();
            is.close();
            oldfile.delete();
            return true;
        } catch (IOException e) {
            e.printStackTrace();
            Log.e(TAG, "移动至指定文件夹异常失败");
            return false;
        }
    }

展开
收起
爵霸 2016-03-25 09:07:40 2873 0
1 条回答
写回答
取消 提交回答
  • 要移动的肯定为空了啊,因为你调了oldfile.delete();你应该在return true之前加上
    if(desfile.getAbsolutePath().equals(newPath)

    2019-07-17 19:13:24
    赞同 展开评论 打赏
问答分类:
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

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