onActivityResult()Uri到Byte数组

Man*_*mar 3 android android-intent

我想从Android Uri(由onActivityResult返回的那种)到一个简单的字节数组,以便将文件作为多部分消息的一部分上传.任何人都可以提供一个如何从Uri到byte []数组的例子吗?

编辑:我的意思是android.net.UriUri

Man*_*mar 12

在汇总了一些SO示例和google组代码后,我可以通过以下代码实现:

Uri data = result.getData();   
ByteArrayOutputStream baos = new ByteArrayOutputStream();  
FileInputStream fis;  
try {  
    fis = new FileInputStream(new File(data.getPath()));  
    byte[] buf = new byte[1024];  
    int n;  
    while (-1 != (n = fis.read(buf)))  
        baos.write(buf, 0, n);  
} catch (Exception e) {  
    e.printStackTrace();  
}  
byte[] bbytes = baos.toByteArray();
Run Code Online (Sandbox Code Playgroud)

  • 我收到此错误:java.io.FileNotFoundException:/document/primary:Simon/Sample file/Financial Sample.xlsx:打开失败:ENOENT(没有此类文件或目录)。任何想法? (2认同)