无法使用文件选择器将文件复制到其他位置

Unable to copy file to different location using file picker

我正在尝试实现一个用于浏览文件并将其复制到 'downloads' 文件夹的按钮。我使用 解决方案来做到这一点。请在此处查看我的代码:

public void browseFile(View view) {

    Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT);
    chooseFile.addCategory(Intent.CATEGORY_OPENABLE);
    chooseFile.setType("*/*");
    startActivityForResult(
            Intent.createChooser(chooseFile, "Choose a file"),
            PICKFILE_RESULT_CODE
    );
}


@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    File destination = null;
    File source = null;
    if (requestCode == PICKFILE_RESULT_CODE && resultCode == Activity.RESULT_OK) {
        Uri content_describer = data.getData();
        String src = content_describer.getPath();
        source = new File(src);
        Log.d("src is ", source.toString());
        String filename = content_describer.getLastPathSegment();
        //text.setText(filename);
        Log.d("FileName is ", filename);
        destination = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + File.separator + "/GSW/" + filename);
        Log.d("Destination is ", destination.toString());
        //SetToFolder.setEnabled(true);
    }

    try {
        copy(source, destination);
    } catch (IOException e) {
        e.printStackTrace();
    }
}

我正在使用解决方案中提到的复制功能将文件复制到目标文件夹,如下所示:

private void copy(File src, File dest) throws IOException {

    FileChannel in = new FileInputStream(src).getChannel();
    FileChannel out = new FileOutputStream(dest).getChannel();

    try {
        in.transferTo(0, in.size(), out);
    } catch(Exception e){
        Log.d("Exception", e.toString());
    } finally {
        if (in != null)
            in.close();
        if (out != null)
            out.close();
    }
}

我尝试了许多其他给出的解决方案,但我仍然得到 "FileNotFoundException" 如下:

错误

W/System.err: java.io.FileNotFoundException: /document/primary:WhatsApp/Media/WhatsApp Documents/IGN 假期列表 2020.pdf (没有那个文件或目录) W/System.err:在 java.io.FileInputStream.open0(本机方法)

编辑 1

我按照@blackapps 的建议更新了我的代码仍然收到 FileNotFoundException,但这次是目的地。

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    super.onActivityResult(requestCode, resultCode, data);
    Uri content_describer = data.getData();
    InputStream in = null;
    OutputStream out = null;
    try {
        // open the user-picked file for reading:
        in = getContentResolver().openInputStream(content_describer);
        // open the output-file:
        out = new FileOutputStream(new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getAbsolutePath() + "/GSW/" + File.separator));
        // copy the content:
        byte[] buffer = new byte[1024];
        int len;
        while ((len = in.read(buffer)) != -1) {
            out.write(buffer, 0, len);
        }
        // Contents are copied!
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (in != null) {
            try {
                in.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        if (out != null){
            try {
                out.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}

错误

2020-02-12 17:07:09.214 5412-5412/com.gsw.nfc W/System.err: java.io.FileNotFoundException: /storage/emulated/0/Download/GSW (是目录) 2020-02-12 17:07:09.215 5412-5412/com.gsw.nfc W/System.err: 在 java.io.FileOutputStream.open0(本机方法)

您将数据视为文件并使用 FileInputStream 作为不存在的路径。

使用 InputStream instread。

InputStream is = getContentResolver().openInputStream(data.getData());

然后像使用 fis 一样使用 is