Reputation: 3049
I want to take a single picture and put it in an imageView.I have managed to open the camera and every time i take a picture it lets me take another one. This is my onCreate method:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera);
File photo = dispatchTakePictureIntent();
ImageView imgView = (ImageView) findViewById(R.id.DisplayImageView);
if(photo.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(photo.getAbsolutePath());
imgView.setImageBitmap(myBitmap);
}
}
This is my taking picture method:
private File dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
File photoFile = null;
// Ensure that there's a camera activity to handle the intent
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
// Create the File where the photo should go
try {
photoFile = createImageFile();
} catch (IOException ex) {
// Error occurred while creating the File
Toast.makeText(this, "Failed to create the image file!", Toast.LENGTH_SHORT).show();
}
// Continue only if the File was successfully created
if (photoFile != null) {
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT,
Uri.fromFile(photoFile));
startActivityForResult(takePictureIntent, REQUEST_TAKE_PHOTO);
}
}
return photoFile;
}
I need to get back to the activity from the camera after one photo and view it. How can i do that?
Upvotes: 0
Views: 372
Reputation: 4091
You need to Override onActivityResult() of your Activity to get the image and display it in ImageView. You should sample the image according to your needs when you get it from Camera and also to prevent your image from rotating while displaying in ImageView, you need to look for Exif params.
Here is the onActivityResult() code.
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
try {
if (resultCode == RESULT_OK && requestCode == Constants.REQUEST_TAKE_PHOTO) {
// in case of taking pic from camera, you have to define filepath already and image get saved in that filepath you specified at the time when you started camera intent.So in your case it will be following
String filePath=photoFile.getAbsolutePath(); //photofile is the same file you passed while starting camera intent.
if (filePath != null) {
int orientation = 0;
private Bitmap imageBitmap;
try {
ExifInterface exif = new ExifInterface(filePath);
orientation = exif.getAttributeInt(
ExifInterface.TAG_ORIENTATION, 1);
} catch (IOException e) {
e.printStackTrace();
}
try {
final BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
BitmapFactory.decodeFile(filePath, options);
options.inSampleSize = calculateInSampleSize(
options, reqWidth, rewHeight);
options.inJustDecodeBounds = false;
imageBitmap = BitmapFactory.decodeFile(filePath,
options);
if (orientation == 6) {
Matrix matrix = new Matrix();
matrix.postRotate(90);
imageBitmap = Bitmap.createBitmap(imageBitmap,
0, 0, imageBitmap.getWidth(),
imageBitmap.getHeight(), matrix, true);
} else if (orientation == 8) {
Matrix matrix = new Matrix();
matrix.postRotate(270);
imageBitmap = Bitmap.createBitmap(imageBitmap,
0, 0, imageBitmap.getWidth(),
imageBitmap.getHeight(), matrix, true);
} else if (orientation == 3) {
Matrix matrix = new Matrix();
matrix.postRotate(180);
imageBitmap = Bitmap.createBitmap(imageBitmap,
0, 0, imageBitmap.getWidth(),
imageBitmap.getHeight(), matrix, true);
}
} catch (OutOfMemoryError e) {
imageBitmap = null;
e.printStackTrace();
} catch (Exception e) {
imageBitmap = null;
e.printStackTrace();
}
}
if (imageBitmap != null) {
// set this imageBitmap to your ImageView
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
and this is the sampling function
public static int calculateInSampleSize(BitmapFactory.Options options,
int reqWidth, int reqHeight) {
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
final int halfHeight = height / 2;
final int halfWidth = width / 2;
while ((halfHeight / inSampleSize) > reqHeight
&& (halfWidth / inSampleSize) > reqWidth) {
inSampleSize *= 2;
}
}
return inSampleSize;
}
Upvotes: 1
Reputation: 1967
Well in your Activity that does startActivityForResult override the following Method
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_TAKE_PHOTO && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
mImageView.setImageBitmap(imageBitmap);
}
}
Replace mImageView with the ImageView you want to show your image
Upvotes: 0