onActivityResult()Uri到Byte数组 [英] onActivityResult() Uri to Byte array

查看:123
本文介绍了onActivityResult()Uri到Byte数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从Android Uri(由 onActivityResult)转换为简单的字节数组,以便将文件上传为 多部分邮件的一部分.任何人都可以提供有关如何 从Uri到byte []数组?

I want to get from an Android Uri (of the kind returned by onActivityResult) to a simple byte array, in order to upload a file as part of a multipart message. Can anyone provide an example of how to get from a Uri to a byte[] array?

我的意思是android.net.Uri Uri

推荐答案

将一些SO示例和google组代码放在一起后,我可以通过以下代码来实现它:

After putting together some SO examples and google group codes, i'm able to acheive it by this code:

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();

这篇关于onActivityResult()Uri到Byte数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆