Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

onActivityResult() Uri to Byte array

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?

Edit: I mean the android.net.Uri Uri

like image 963
Manoj Kumar Avatar asked Dec 11 '22 22:12

Manoj Kumar


1 Answers

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();
like image 96
Manoj Kumar Avatar answered Jan 01 '23 14:01

Manoj Kumar