Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android Can';不要将文件复制到文件夹中_Android_File_Copy_Storage - Fatal编程技术网

Android Can';不要将文件复制到文件夹中

Android Can';不要将文件复制到文件夹中,android,file,copy,storage,Android,File,Copy,Storage,任务:我想将所选文件从A文件夹复制到B文件夹。两个文件夹都在外部存储器中 问题:它工作得非常好,但在某个时候它只是停止复制文件。例如,如果我想复制500个文件,它只会复制110个文件。我还注意到我不能复制视频文件,它只能处理图像 代码: 我用于复制文件的方法: private static void makeFileCopy(File source, File dest) throws IOException { InputStream is = null; OutputSt

任务:我想将所选文件从
A
文件夹复制到
B
文件夹。两个文件夹都在外部存储器中

问题:它工作得非常好,但在某个时候它只是停止复制文件。例如,如果我想复制500个文件,它只会复制110个文件。我还注意到我不能复制视频文件,它只能处理图像

代码

我用于复制文件的方法:

  private static void makeFileCopy(File source, File dest) throws IOException {
    InputStream is = null;
    OutputStream os = null;
    try {
        is = new FileInputStream(source);
        os = new FileOutputStream(dest);
        byte[] buffer = new byte[1024];
        int length;
        while ((length = is.read(buffer)) > 0) {
            os.write(buffer, 0, length);
        }
    }finally {
        try {
            if (is != null)
                is.close();
            if (os != null)
                os.close();
        }catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}
还有一点:

 public static void copyFileList(Context context, List<MediaFile> contentList, File mediaFolder) {
    if (contentList != null) {
        ContentValues values=new ContentValues();
        for (int index=0;index<contentList.size();index++) {
            MediaFile mediaFile=contentList.get(index);
            File file = new File(mediaFolder, mediaFile.mediaFile().getName());
            boolean isVideo=mediaFile.getType()== MediaFile.Type.VIDEO;
            if (!file.exists()) {
                try {
                    if (!file.createNewFile()) {
                        continue;
                    }
                    FileUtils.makeFileCopy(mediaFile.getRealFile().getAbsoluteFile(), file);

                } catch (IOException ex) {
                    ex.printStackTrace();
                    continue;
                }

                if (isVideo) {
                    values.put(MediaStore.Video.VideoColumns.DATA, file.getAbsolutePath());
                    context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
                } else {
                    values.put(MediaStore.Images.ImageColumns.DATA, file.getAbsolutePath());
                    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                }
                values.clear();
            }
        }
    }
}
publicstaticvoidcopyfilelist(上下文、列表contentList、filemediafolder){
if(contentList!=null){
ContentValues=新的ContentValues();

对于(int index=0;index最后,我解决了这个问题。这是我犯的极其愚蠢的错误

解决方案:我想复制目标文件夹中已有的文件,通过检查
如果(!file.exists())
它就是没有通过。因此,我想出了以下代码:

public static void copyFileList(Context context, List<MediaFile> contentList, File mediaFolder) {
    if (contentList != null) {
        ContentValues values=new ContentValues();
        for (int index=0;index<contentList.size();index++) {
            MediaFile mediaFile=contentList.get(index);
            String fileName=mediaFile.mediaFile().getName();

            boolean isVideo=mediaFile.getType()== MediaFile.Type.VIDEO;
            File file = new File(mediaFolder, fileName);
            //let a user to decide whether to create a copy of already existing files
            if(!file.exists()) {
                file=new File(mediaFolder,uniqueNameFor(fileName));
            }

            if(!file.exists()) {
                try {
                    FileUtils.makeFileCopy(mediaFile.mediaFile().getAbsoluteFile(), file);
                } catch (IOException ex) {
                    ex.printStackTrace();
                    continue;
                }

                if (isVideo) {
                    values.put(MediaStore.Video.VideoColumns.DATA, file.getAbsolutePath());
                    values.put(MediaStore.Video.VideoColumns.MIME_TYPE,mediaFile.getMimeType());
                    context.getContentResolver().insert(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, values);
                } else {
                    values.put(MediaStore.Images.ImageColumns.DATA, file.getAbsolutePath());
                    values.put(MediaStore.Images.ImageColumns.MIME_TYPE,mediaFile.getMimeType());
                    context.getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
                }

            }
            values.clear();
        }
    }
}
使用通道比使用以前的方法快一点。 查看如何复制文件的4种方法


感谢您的帮助!

它总是只复制110个文件吗?关于视频文件-它们有多大?如果您放置了一个小的视频文件,它是否也被忽略?您是否检查了日志中是否有任何异常?
如果(!file.createNewFile()){continue;
。删除该代码。这没有用。请尝试在没有ContentValues内容的情况下进行测试。
   private static void makeFileCopy(File source, File dest) throws IOException {
    FileChannel inputChannel = null;
    FileChannel outputChannel = null;
    try {
        inputChannel = new FileInputStream(source).getChannel();
        outputChannel = new FileOutputStream(dest).getChannel();
        outputChannel.transferFrom(inputChannel, 0, inputChannel.size());
    } finally {
        try {
            if (inputChannel != null)
                inputChannel.close();
            if (outputChannel != null)
                outputChannel.close();
        }catch (IOException ex) {
            ex.printStackTrace();
        }
    }
}