java.io.FileNotFoundException:文件:/storage/emulated/0/WhatsApp/Media/.Statuses/xyz.jpg:打开失败:ENOENT(无此类文件或目录)

时间:2020-11-01 14:17:56

标签: java android file compiler-errors file-copying

我想将所有文件从/WhatsApp/.Statuses/复制到新文件夹,但是我遇到了这个问题,

java.io.FileNotFoundException:文件:/storage/emulated/0/WhatsApp/Media/.Statuses/7cf30a37ee9341508b467ff2d9a361bc.jpg:打开失败:ENOENT(没有这样的文件或目录)

我的代码在这里,

private View.OnClickListener downloadMediaItem(Story files) {
    return new View.OnClickListener() {

        @Override
        public void onClick(View v) {
            new Runnable() {

                @Override
                public void run() {
                    try {
                        checkFolder();
                        String sourcePath = String.valueOf(files.getUri());
                        copyFile(sourcePath, new File(Environment.getExternalStorageDirectory().toString() + Constant.SAVE_FOLDER_NAME));
                        Toast.makeText(context, "Saved!", Toast.LENGTH_SHORT).show();
                    } catch (Exception e) {
                        e.printStackTrace();
                        Log.e("RecyclerV", "onClick: Error:" + e.getMessage());
                    }
                }
            }.run();
        }
    };
}

private void copyFile(String sourcePath, File file) throws IOException {
    if (!file.getParentFile().exists())
        file.getParentFile().mkdirs();

    if (!file.exists()) {
        file.createNewFile();
    }

    FileChannel source = null;
    FileChannel destination = null;

    try {
        source = new FileInputStream(String.valueOf(sourcePath)).getChannel();
        destination = new FileOutputStream(file).getChannel();
        destination.transferFrom(source, 0, source.size());
    } finally {
        if (source != null) {
            source.close();
        }
        if (destination != null) {
            destination.close();
        }
    }
}

我该如何解决这个问题?

0 个答案:

没有答案