[英]How to pass image captured by camera to next screen in android?
我在android中使用相机应用程序。 我想将字节数据从PictureCallback方法传递给另一个活动,并希望在该活动中显示它。
Camera.PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
}
};
如果有人知道,请帮助我。
您可以使用其他功能:
Camera.PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
Intent i = new Intent(context, B.class);
Bundle bundle = new Bundle();
bundle.putByteArray("photo", data);
i.putExtra(bundle );
startActivity(i);
}
};
和B活动:
Bundle extras = getIntent().getExtras();
byte[] photo = extras.getByteArray("photo");
要在第二个活动中显示图像,必须将byte []转换为位图并将其分配给imageView:
Bitmap bitmap = decodeByteArray (photo, 0, photo.length);
ImageView imgView = (ImageView)findViewById(R.id.preview);
imgView.setImageBitmap(bitmap);
我从没有尝试过从byte []解码为位图..但是您可以在这里找到更多信息。
编辑: @ ss1271的评论是正确的。 根据此答案,似乎有500Kb的限制。 这意味着,如果您的图片很大,则应保存它,并将引用传递给新活动,如下所示:
// A ACTIVITY
Camera.PictureCallback jpegCallback = new PictureCallback() {
public void onPictureTaken(byte[] data, Camera camera) {
String fileName = "tempIMG.png";
try {
FileOutputStream fileOutStream = openFileOutput(fileName, MODE_PRIVATE);
fileOutStream.write(data);
fileOutStream.close();
} catch (IOException ioe) {
ioe.printStackTrace();
}
Intent i = new Intent(context, B.class);
Bundle bundle = new Bundle();
bundle.putExtra("photoPath", fileName);
i.putExtra(bundle);
startActivity(i);
}
};
// B ACTIVITY
Bundle extras = getIntent().getExtras();
String photoPath = extras.getString("photoPath");
File filePath = getFileStreamPath(photoPath);
//And do whatever you want to do with the File
您可以通过putExtra(String name,byte value)将字节(数据)添加到Intent,然后使用该Intent启动新的Activity。
蒂姆,祝你好运
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.