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

问题描述 投票:0回答:1

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

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 Holiday List 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(本机方法)

android android-file filepicker
1个回答
0
投票

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

使用InputStream实例。

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

然后像使用is一样使用fis

© www.soinside.com 2019 - 2024. All rights reserved.