2010-12-22 134 views
1

这是我在做什么:Android:从图像ID获取字节[

在onActivityResult,我得到的数据在一个意图。

Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); 
startActivityForResult(intent, TAKE_PICTURE); 

这段代码获得的图像ID:

data.getData().getLastPathSegment(); 

现在我想用这个形象的ID来获得的byte []的imageData,所以我可以上传图片的服务器上。

我该怎么办?

回答

1
Uri selectedImageUri = data.getData(); 

String[] projection = { MediaStore.Images.Media.DATA }; 
Cursor cursor = managedQuery(selectedImageUri, projection, null, null, null); 
cursor.moveToFirst(); 
selectedImagePath = cursor.getString(cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA)); 
cursor.close(); 

try 
{ 
    File myFile = new File(selectedImagePath); 
    int filelenghth=(int)myFile.length(); 
    byte [] mybytearray = new byte [filelenghth]; 
    BufferedInputStream bis1 = new BufferedInputStream(new FileInputStream(myFile)); 

    bis1.read(mybytearray,0,mybytearray.length); 

    dos.write(mybytearray,0,mybytearray.length); // write the array to the data output stream 
                // or whatever else you want to do with it 
    finish(); 
} 
catch(Exception e) 
{ 
    e.printStackTrace(); 
} 
+0

由于阿布舍克巴克。这对我很有帮助。 – 2010-12-22 13:09:24

0

data.getData()会返回给你一个图像的URI。 (检查数据!=空第一!)

然后你只需要执行一个http post到你的服务器。

查看的HTTP POST码对SO以下参考:Sending images using Http Post