简体   繁体   English

FileInputStream(file)返回ENOENT(没有这样的文件或目录)android

[英]FileInputStream(file) returning ENOENT (No such file or directory) android

I'm trying to retrieve a file (any type) from file manager and encode this file in a Base64 String. 我正在尝试从文件管理器中检索文件(任何类型)并将此文件编码为Base64字符串。 I've found a lot of answers involving IMAGES, but I need any type of file. 我发现了很多涉及IMAGES的答案,但我需要任何类型的文件。 He's what I'm doing. 他就是我在做的事。

I'm retrieving a file(any type) from gallery like this 我正在从这样的库中检索文件(任何类型)

Intent intent = new Intent();
intent.setType("*/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(Intent.createChooser(intent, "Choose file"), GALLERY_REQUEST_CODE);

And in my result 在我的结果

Uri returnUri = intent.getData();

What gives me 'content://com.android.providers.downloads.documents/document/1646' 什么给了我'内容://com.android.providers.downloads.documents/document/1646'

Then I try 然后我试试

File file = new File( returnUri.getPath() );

So far so good, but then I try to encode the file into a Base64 string: 到目前为止一直很好,但后来我尝试将文件编码为Base64字符串:

    String str = null;
    try {
        str = convertFile(file);
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

And the convertFile method 和convertFile方法

public String convertFile(File file)
        throws IOException {
    byte[] bytes = loadFile(file);
    byte[] encoded = Base64.encode(bytes, Base64.DEFAULT);
    String encodedString = new String(encoded);

    return encodedString;

}

And the loadFile method 和loadFile方法

private static byte[] loadFile(File file) throws IOException {
    InputStream is = new FileInputStream(file);

    long length = file.length();
    if (length > Integer.MAX_VALUE) {
        Log.i("MobileReport","Anexo -> loadfile(): File is too big!");
        // File is too large
    }
    byte[] bytes = new byte[(int)length];

    int offset = 0;
    int numRead = 0;
    while (offset < bytes.length
           && (numRead=is.read(bytes, offset, bytes.length-offset)) >= 0) {
        offset += numRead;
    }

    if (offset < bytes.length) {
        throw new IOException("It was not possible to read the entire file "+file.getName());
    }

    is.close();
    return bytes;
}

The error is in the first line of 'loadFile' method 错误发生在'loadFile'方法的第一行

InputStream is = new FileInputStream(file);

The app crashes and in the log I got: 应用程序崩溃并在我得到的日志中:

java.io.FileNotFoundException: /document/1646: open failed: ENOENT (No such file or directory)

I've declared the READ and WRITE permissions. 我已经声明了READ和WRITE权限。 Could anyone help me with that error? 任何人都可以帮我解决这个错误吗? Thank you! 谢谢!

returnUri is a Uri . returnUri是一个Uri It is not a filesystem path. 它不是文件系统路径。

In particular, if you examine it, you will find that it is a content:// Uri . 特别是,如果你检查它,你会发现它是一个content:// Uri To get an InputStream for that content, use openInputStream() on a ContentResolver . 要获取该内容的InputStream ,请在ContentResolver上使用openInputStream()

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

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