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

Android 无法使用文件选择器将文件复制到其他位置,android,android-file,filepicker,Android,Android File,Filepicker,我正在尝试实现一个按钮,用于浏览文件并将其复制到“下载”文件夹。我使用了一种解决方案。请在此处查看我的代码: public void browseFile(View view) { Intent chooseFile = new Intent(Intent.ACTION_GET_CONTENT); chooseFile.addCategory(Intent.CATEGORY_OPENABLE); chooseFile.setType("*/*"); startA

我正在尝试实现一个按钮,用于浏览文件并将其复制到“下载”文件夹。我使用了一种解决方案。请在此处查看我的代码:

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,如下所示:

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();
    }
}
错误

W/System.err:java.io.FileNotFoundException:/document/primary:WhatsApp/Media/WhatsApp Documents/IGN Holiday List 2020.pdf无此类文件或目录 W/System.err:at java.io.FileInputStream.open0Native方法

编辑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.open0Native方法中,您将数据视为文件,并将FileInputStream用于不存在的路径

使用InputStream instread

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

然后使用就像您使用了fis。

我尝试了您的建议,现在目标文件出现错误。您应该只对输入文件应用此选项。GWS不是目录吗?我看不到文件名。如果要打开文件输出流,应该使用类似/storage/simulated/0/Download/GSW/copiedfile.jpg的内容。您有/GSW/+File.separator,因此末尾有两条斜线。如果是文件路径,则结尾不能有斜杠。GWS是否存在?是的,GSW是否存在。文件名是个问题。一旦我添加了文件名,问题就解决了!!谢谢您: