我正在尝试从相机捕获图片(三星 S3 特定问题),下面是我的代码:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if( requestCode == 1337 && resultCode== RESULT_OK){
Bundle extras = data.getExtras();
if (extras != null){
BitmapFactory.Options options = new BitmapFactory.Options();
// options.inSampleSize = 1;
// options.inPurgeable = true;
// options.inInputShareable = true;
thumbnail = (Bitmap) extras.get("data");
image(thumbnail);
}else{
Toast.makeText(CreateProfile.this, "Picture NOt taken", Toast.LENGTH_LONG).show();
}
图像函数:
public void image(Bitmap thumbnail){
Bitmap photo = thumbnail;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
b = bos.toByteArray();
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
}
启动相机 Intent 的代码:
if(i==0){
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
}
原木猫:
http://img534.imageshack.us/img534/8388/logcamera.png
http://img585.imageshack.us/img585/7559/camera1u.png
我的代码在 HTC Wildfire S、Dell XCD35、Samsung Galaxy Grand 和 Samsung Galaxy Tab 上运行良好,但不知道为什么在 S3 中会显示此错误。有线索吗?
最佳答案
从您的 LOGCAT
输出来看,相机 Activity 似乎正在返回所拍摄照片的 URI。并且没有缩略图,只是为了确保使用:
if (extras.keySet().contains("data") ){
thumbnail = (Bitmap) extras.get("data");
image(thumbnail);
}
关于解析 Uri 在 Intent 中您可以使用以下内容:
Uri imageURI = getIntent().getData();
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
imageview.setImageURI(imageURI);
所以完整的代码看起来像这样:
if (extras.keySet().contains("data") ){
thumbnail = (Bitmap) extras.get("data");
image(thumbnail);
} else {
Uri imageURI = getIntent().getData();
ImageView imageview = (ImageView)findViewById(R.id.imageView1);
imageview.setImageURI(imageURI);
}
关于android - 从相机拍照时出现空指针异常,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15130971/