繁体   English   中英

如何修复Android-Studio中Uri-Path的“FileNotFoundException”(没有这样的文件或目录)错误?

[英]How to fix my “FileNotFoundException”(No such file or directory) error with Uri-Path in Android-Studio?

我正在编写一个应用程序来保存文件(图片)作为csv文件中的列给出的特定名称。 用户必须首先使用filebrowser选择csv,然后将文件复制到我的Dir-Data目录。

一切都很好,但似乎我得到的路径File src Object不适用于操作。 我希望这里的错误显而易见(第2个Code-Box)并且如果它显而易见或者容易避免,请提前预订,这是我的第一个Android项目。

我已经尝试使用不同的复制函数和不同的参数类型,并尝试其他格式,如uri.toString()给出的字符串。

// CSV打开程序

    public void performFileSearch() {

        // ACTION_OPEN_DOCUMENT is the intent to choose a file via the system's file
        // browser.
        Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT);

        // Filter to only show results that can be "opened"
        intent.addCategory(Intent.CATEGORY_OPENABLE);

        // Filter to show only  .csv  using the image MIME data type.
        // For all it would be "*/*".
        intent.setType("text/comma-separated-values");

        startActivityForResult(intent, READ_REQUEST_CODE);

    }

//创建路径

@Override

public void onActivityResult(int requestCode, int resultCode,
                                 Intent resultData) {

        if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK)
        {

            if (resultData != null) {
                Uri path = resultData.getData();
                stringUri = path.getPath();
                File src = new File(stringUri);
                File destination = new File(getFilesDir().getPath());

                try {
                    copyDirectoryOneLocationToAnotherLocation(src,destination);
                }
                catch(IOException e) {
                    e.printStackTrace();
                    System.out.print("error in upload");
                }

                Toast.makeText(MainActivity.this, "Path: "+stringUri , Toast.LENGTH_SHORT).show();

            }


        }
    }

//来自StackOverflow的复制操作

    public static void copyDirectoryOneLocationToAnotherLocation(File sourceLocation, File targetLocation)
            throws IOException {

        if (sourceLocation.isDirectory()) {
            if (!targetLocation.exists()) {
                targetLocation.mkdir();
            }

            String[] children = sourceLocation.list();
            for (int i = 0; i < sourceLocation.listFiles().length; i++) {

                copyDirectoryOneLocationToAnotherLocation(new File(sourceLocation, children[i]),
                        new File(targetLocation, children[i]));
            }
        } else {

            InputStream in = new FileInputStream(sourceLocation);

            OutputStream out = new FileOutputStream(targetLocation);

            // Copy the bits from instream to outstream
            byte[] buf = new byte[1024];
            int len;
            while ((len = in.read(buf)) > 0) {
                out.write(buf, 0, len);
            }
            in.close();
            out.close();
        }

    }

我希望将选择的文件复制到我的data / data / ...目录中,以便稍后在App中使用。

但是:我从objets获得的路径对我不起作用

Thx @Mike M.,使用getContentResolver()的提示在尝试之后让我感受到了anwser。

最后使用其他复制功能并重新编写onActivityResult();

public void onActivityResult(int requestCode, int resultCode,
                             Intent resultData) {

    if (requestCode == READ_REQUEST_CODE && resultCode == Activity.RESULT_OK)
    {

        if (resultData != null) {
            try {
                String destination = getFilesDir().getPath();
                InputStream src = getContentResolver().openInputStream(resultData.getData()); // use the uri to create an inputStream
                try {

                    convertInputStreamToFile(src, destination);
                } catch (IOException e) {
                    e.printStackTrace();
                    System.out.print("error in upload");
                }
            } catch (FileNotFoundException ex) {
                }
            String destination = getFilesDir().getPath();
            Toast.makeText(MainActivity.this, "Success!: CSV-File copyed to : " +destination  , Toast.LENGTH_SHORT).show();
        }
    }


}
public static void convertInputStreamToFile(InputStream is, String destination) throws IOException
{
    OutputStream outputStream = null;
    try
    {
        File file = new File(destination + "/Student.csv");
        outputStream = new FileOutputStream(file);

        int read = 0;
        byte[] bytes = new byte[1024];
        while ((read = is.read(bytes)) != -1) {
            outputStream.write(bytes, 0, read);
        }
    }
    finally
    {
        if(outputStream != null)
        {
            outputStream.close();
        }
    }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM