将图像保存到Web服务器(字节数组)

时间:2012-05-08 11:37:33

标签: android android-image

我想以Byte数组的形式将图像上传到服务器。这里我使用表面视图和“拍照”按钮,当用户点击拍照按钮时,

    TakePicture.setOnClickListener(new OnClickListener() {

    public void onClick(View v) {
        // TODO Auto-generated method stub
        camera.takePicture(null, null, callback);
    }
});

和pictureCallback是:

       PictureCallback callback = new PictureCallback(){

 public void onPictureTaken(**byte[] data**, Camera camera) {
  // TODO Auto-generated method stub

     try {
            //async task for storing the photo
         Log.i("Picture Taken.", "Picture Taken.");

            new SavePhotoTask(CameraPreviewActivity.this, data).execute();
        } catch (final Exception e) {
            //some exceptionhandling
            Log.i("Save Photo exception",e.getMessage());
        }

}}; 现在我在这里使用这个字节数组'data'

我希望以byte []的形式将此图像发送到Web服务器..

我该怎么做?

2 个答案:

答案 0 :(得分:0)

将字节数组转换为base64

String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);

另见this link

答案 1 :(得分:0)

1)发送相机意图

public void onCameraClick(View view) {
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    imageFile = FileUtil.newFile();
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(imageFile));
    startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
}

2)当图片准备就绪时获取文件路径,从文件读取字节并将字节发送到服务器。

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE && resultCode == RESULT_OK){
            if (resultCode == RESULT_OK) {
                final String path = imageFile.getAbsolutePath();
// get file from path and send bytes to server
            }
        }
    }