我正在尝试用相机拍照,然后将其转换为bytearray并传递给下一个活动。我无法弄清楚如何做到这一点。这是拍摄照片的活动的代码:
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
}
}
我已经设置了下一个活动,因此它可以接收bytearray并对其进行解码,所以我需要帮助的是这个。
答案 0 :(得分: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();
}
答案 1 :(得分:1)
正如Squonk指出的那样,我应该将相机意图和OnActivityResult移动到下一个活动而不是它自己的活动中。它现在有效。