1

这是我正在做的事情:

在 onActivityResult 中,我在 Intent 中获取数据。

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

此代码获取图像 ID:

data.getData().getLastPathSegment();

现在我想使用这个图像 ID 来获取 byte[] imageData,这样我就可以将图像上传到服务器上。

我该怎么做?

4

2 回答 2

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();
}
于 2010-12-22T11:19:13.043 回答
0

data.getData() 将为您返回图像的 URI。(首先检查数据!= null!)

那么你只需要向你的服务器执行一个http post。

请参阅以下关于 SO 的 http post 代码参考: Sending images using Http Post

于 2010-12-22T09:52:21.353 回答