梅明子 2015-12-07 06:06 采纳率: 100%
浏览 1626
已采纳

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;
        }
    }
  • 写回答

2条回答 默认 最新

  • 梅明子 2015-12-07 08:33
    关注

    原路径:/storage/sdcard0/dcim/Camera/a2051958524f8b085262c960866397ce.jpg
    目标文件夹:/storage/sdcard0/baidu/searchbox/preset/preset4.2/pic/
    最终路径:/storage/sdcard0/baidu/searchbox/preset/preset4.2/pic/a2051958524f8b085262c960866397ce.jpg

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

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 对于这个问题的解释说明
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败
  • ¥20 java在应用程序里获取不到扬声器设备
  • ¥15 echarts动画效果的问题,请帮我添加一个动画。不要机器人回答。