I'm trying to do an app (with the master/detail flow template) that in the detail fragment, has an ImageView that shows an image taken by the camera.
For this option, I added a button to the ActionBar that opens a Dialog box and asks the user if he wishes to add an image with the camera. So for this option I made this two methods that are called when the user clicks ok. However, (here I now I'm making huge mistakes) I don't have the least idea about how to call the onActivityResult() with the end of having my ImageIcon showing the thumbnail retrieved from the camera. (as developer.android tutorial shows)
If anyone of you guys knows what I am doing wrong, I would be really happy to hear all the solutions that you can give me, since I wasn't able to find a solution to this problem anywhere.
private void dispatchTakePictureIntent() {
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(takePictureIntent, REQUEST_IMAGE_CAPTURE);
onActivityResult(REQUEST_IMAGE_CAPTURE, RESULT_OK, takePictureIntent);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap imageBitmap = (Bitmap) extras.get("data");
ImageView mImageView = (ImageView) findViewById(R.id.imageView1);
mImageView.setImageBitmap(imageBitmap);
}
}
THANKS A LOT!!!!
You do not have to call onActivityResult explicitly. onActivityResult method called in the parent activity when the immediately child/successor activity finished its task. So when your Image taking activity will finished the task it call automatically a "requestCode" and Image data(If your Image taking activity take a picture otherwise it will be null).
For more details about the onActivityResult see the here.
To set the thumbnail to the imageview you can do this in the following way.
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
You can see this tutorial about the how to take Image using intent.
Happy Coding
You have to create a scaled bitmap from the bitmap received in onActivityResult. You will get information about how to create a scaled bitmap Here
You can set the width and height as desired
imageBitmap = Bitmap.createScaledBitmap(realImageBitmap, final_width, final_height, false);
imageView.setImageBitmap(imageBitmap);
Related
I am working on a semester project and it requires that I send images from an android device to server without storing the image. I want to make sure my cameta application is working properly. I want to check this using an ImageView that displays the captured image on the screen. How can I do it? I thought I could load from drawable but I cannot add files to drawable during runtime.
Thanks for any support
You don't need to save it to the drawable. These are the two steps:
1.Convert the captured image to Bitmap
2.Set the bitmap image to an imageview
Ref: StackOverflow Answer
private void takePicture(){ //you can call this every 5 seconds using a timer or whenever you want
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap picture = (Bitmap) data.getExtras().get("data");//this is your bitmap image and now you can do whatever you want with this
imageView.setImageBitmap(picture); //for example I put bmp in an ImageView
}
}
This question already has answers here:
Low picture/image quality when capture from camera
(3 answers)
Closed 3 years ago.
I want the good quality image to show in ImageView and upload to the server. I have searched the Internet, StackOverflow and GitHub, but I could not find the answer, maybe somebody knows how can fix it?
protected void onActivityResult(int requestCode, int resultCode, Intent imageReturnedIntent) { //work android
super.onActivityResult(requestCode, resultCode, imageReturnedIntent);
imageReturnedIntent.putExtra(MediaStore.EXTRA_OUTPUT, uri);//try
if (resultCode == RESULT_OK ) {// work every android than need click OK
//working
Uri selectedImage = imageReturnedIntent.getData();//convert to for bitmap that can send
Bundle extras = imageReturnedIntent.getExtras();//finish converting and copy the image
bitmap = extras.getParcelable("data");//receive image to bitmap
imageView.setImageBitmap(bitmap);
onCreate:
btnCamera.setOnClickListener(new View.OnClickListener() {
public void onClick(View view) {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
//intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, 0);
}
});
What is wrong here?
The Intent you send returns only low-quality picture as a small bitmap.
If you want the full-resolution image, you need to prepare the file where this image will be saved and provide all necessary information to the Camera application. The details are available at Save the full-size photo section, with all explanations and the code samples.
I am using the SurfaceHolder.Callback and Camera.PictureCallback to open Camera in my activity After taking image i want to crop that image and then save it to sd card. I found many solution for cropping via intent camera but didn't find any solution as per my Requirement.
I'll be thankful if someone help me.
Thanks in Advance.
You should implement Camera.PreviewCallback instead of Camera.PictureCallback,
by using the interface implemented method:
public void onPreviewFrame(byte[] data, Camera camera) {
if (shutterEnabled){
shutterEnabled = false;
// Process data
// Put it on a Bitmap
// Send it to cropImage
// -----
}
}
Take in account that if you didn't set on your camera parameters, setPreviewFormat, the picture on viewPreviewFrame would arrive as YUV (NV21) format.
Actually I'm looking how to make this part, this is why I bumped into your question.
The point here, how are you going to pass the picture to the crop method, you should look into that.
The shutterEnabled flag, it's enabled from your listener that 'watches' for that event.
private void cropImage( picture){
Intent cropIntent = new Intent("com.android.camera.action.CROP");
cropIntent.setDataAndType(picture, "image/*");
cropIntent.putExtra("aspectX",0);
cropIntent.putExtra("aspectY",0);
cropIntent.putExtra("return-data", true);
startActivityForResult(cropIntent, CAMERA_CROP_CODE);
}
protected void onActivityResult(int requestCode, int resultCode, Intent data){
if (resultCode == RESULT_OK && requestCode == CAMERA_CROP_CODE){
Bitmap bmp = (Bitmap)data.getExtras().get("data");
}
}
Okay, so I have an onclick on a button that opens the camera, when I take a picture and save it how do I get the file name+location of where it was saved to open it in my app?
Here is my button click
Button btncamera = (Button)findViewById(R.id.btncamera);
btncamera.setOnClickListener(new OnClickListener(){
public void onClick(View v){
Intent cameraIntent = nwe Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, 2500);
}
}
Here is my Result it doesn't contain anything, but it will start another Intent to display the image:
protected void onActivityResult(int requestCode, int resultCode, Intent data){
}
Maybe I am doing it wrong, but any guidance would be much appreciated thanks!
Try adding this to your onActivityResult.
Bitmap picture = (Bitmap) data.getExtras().get("data");
Take the image out of the camera intent, create a bitmap out of it, and then use it from there. Write it to a file, or import it directly into a display resource.
Also be sure to check your ResultCode to determine if a picture actually has been taken.
in my app i m calling built in camera for capturing picture but i want to set that picture into an image view following is the code . so what code should i add to it to set the picture into imageview.
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
startActivityForResult(intent, 0);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == 0) {
String result = data.toURI();
// ...
}
}
Thanks in advance
Add a button and an ImageView to your main layout. When you click the button, first intent.putExtra to specify where the image will be stored, then start the camera intent. In your onActivityResult, if the resultCode comes back 0 that means the user accepted the picture. In this case, go grab the image from the path you specified as an extra of the intent. Create a bitmap from the file specified by the path, then set the image bitmap in your ImageView.