Reputation: 185
I am getting null value when the camera is start from intent for startActivityForResult()
and getting null value which means no picture is shown.
the camera takes picture but it is not shown on the activity. Plz help me out. Here is the code for take Image Activity.
public class TakeImage extends ActionBarActivity {
private LinearLayout camera;
private LinearLayout gallery;
ImageView targetImage;
Uri fileUri = null;
Button process;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_take_image);
camera=(LinearLayout)findViewById(R.id.button);
gallery=(LinearLayout)findViewById(R.id.button1);
targetImage=(ImageView)findViewById(R.id.setImage);
process=(Button)findViewById(R.id.process);
camera.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, RESULT_OK);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
InputStream stream = null;
super.onActivityResult(requestCode, resultCode, data);
if (requestCode==1&&resultCode == RESULT_OK && data != null){
Uri targetUri = data.getData();
Bitmap bitmap;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
targetImage.setImageBitmap(bitmap);
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
Upvotes: 0
Views: 50
Reputation: 701
Read This Documentation
First Define The Following in the Manifest.
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera" android:required="false" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Then Do The Following:
// create Intent to take a picture and return control to the calling application
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE); // create a file to save the image
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
// start the image capture Intent
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);
Hope It Helps
Upvotes: 1