0

我正在尝试用相机拍照,然后将其转换为字节数组并传递给下一个活动。我不知道该怎么做。这是拍摄照片的活动的代码:

import java.io.ByteArrayOutputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.provider.MediaStore;
import android.widget.ImageView;

public class TakePictureActivity extends Activity {

    public final static int CAMERA_REQUEST = 1888;

    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        TakePicture();
    }

    public void TakePicture() {
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        startActivityForResult(takePictureIntent, CAMERA_REQUEST);
    }

    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // I don't know what to do here
    }

}

我已经设置了下一个活动,因此它可以接收字节数组并对其进行解码,所以我需要的只是这个。

4

2 回答 2

1

正如 Squonk 指出的那样,我应该只是将相机意图和 OnActivityResult 移动到下一个活动而不是它自己的活动中。现在可以了。

于 2013-03-10T21:09:55.447 回答
1

试试这个

byte[] inputData=null;
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
    Uri uri = intent.getData();
    InputStream iStream =   getContentResolver().openInputStream(uri);
    inputData = getBytes(iStream);
} 

getBytes 方法

public byte[] getBytes(InputStream inputStream) throws IOException {
      ByteArrayOutputStream byteBuffer = new ByteArrayOutputStream();
      int bufferSize = 1024;
      byte[] buffer = new byte[bufferSize];

      int len = 0;
      while ((len = inputStream.read(buffer)) != -1) {
        byteBuffer.write(buffer, 0, len);
      }
      return byteBuffer.toByteArray();
    }
于 2013-03-10T20:48:18.253 回答