我尝试了很多来自stackoverflow和Internet的代码。但是我能够找到如何从相机中拾取图像。我使用了以下代码,但是data.getData()始终返回null。不知道如何解决。帮助这个。
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 30);
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == 30 && resultCode == Activity.RESULT_OK&&data!null)
{
if(data.getData()!null){
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}
答案 0 :(得分:1)
尝试此代码@Thrishool,
private static final int CAMERA = 1;
//choosing the image from camera
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA);
//now get the data from the onActivity result
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA) {
Bitmap thumbnail = (Bitmap) data.getExtras().get("data");
imageview.setImageBitmap(thumbnail);
ByteArrayOutputStream baos=new ByteArrayOutputStream();
thumbnail.compress(Bitmap.CompressFormat.PNG,100, baos);
byte [] b=baos.toByteArray();
String temp = Base64.encodeToString(b, Base64.DEFAULT);
Log.e("savedImage",temp);
Toast.makeText(ProfileActivity.this, "Image Saved!", Toast.LENGTH_SHORT).show();
}
}
尝试一下,让我知道@Thrishool