image put into ImageView from taken picture not always showing up - android

An image taken from the camera doesn't always show up in my ImageView. It seems like it is random when it wants to show up. Why is it doing this?
Activity that takes picture
private void takePicture() {
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if (intent.resolveActivity(getPackageManager()) != null) {
startActivityForResult(intent, REQUEST_PICTURE);
}
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
String spokenText;
// Handles picture taking after finished
if (requestCode == REQUEST_PICTURE && resultCode == RESULT_OK) {
Bundle extras = data.getExtras();
Bitmap image = (Bitmap) extras.get("data");
ImageView imageResult = (ImageView) findViewById(R.id.result_image);
imageResult.setImageBitmap(image);
ImageHandler imageHandler = new ImageHandler(this);
imageHandler.writeToFile(image, step.getChecklistId(), step.getOrder());
step.setImageFilename(imageHandler.getFilename(step.getChecklistId(), step.getOrder()));
finishStep();
}
}
XML to show picture
<ImageView
android:id="#+id/result_image"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
android:scaleType="fitCenter"
android:adjustViewBounds="true"
android:contentDescription="#string/taken_img_desc" />

Are you using notifyDataSetChanged for your adapter?
Is your view being refreshed? Try .invalidate();

One more thing: You might experience that the camera intent does not return an image or an uri to an image. I created a wrapper to help you with that. See the whole discussion on stackoverflow here or find the code on github.

Related

Capturing and displaying the same image

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
}
}

Use of captured image bitmap outside onActivityResult method

I am taking photo from my activity using camera intent as shown in the below code, but i need to use the captured image bitmap outside the onActivityResult() method but this is not working, i tried to declare the bitmap outside the method and then use outside the method but this did not work, it seems that the bitmap did not catch the image in the onActivityResult, so can anyone help in order to use the bitmap of captured image outside the method or to save it and get its path like using the select image from gallery, so my goal is to use the bitmap of captured image for image processing purpose outside the onActivityResult() method, below is my code, thanks
captureButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
if (checkSelfPermission(Manifest.permission.CAMERA)
!= PackageManager.PERMISSION_GRANTED) {
requestPermissions(new String[]{Manifest.permission.CAMERA},
MY_CAMERA_PERMISSION_CODE);
} else {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
}
});
and this the onActivityResult() method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST && resultCode == Activity.RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageSelected.setImageBitmap(photo);
}
}
I tried this steps and it worked for me
1) define a Bitmap Object above of your onCreate method like bellow
Bitmap photo;
2) instead of Bitmap photo = (Bitmap) data.getExtras().get("data"); in onActivityResult write bellow code
photo = (Bitmap) data.getExtras().get("data");
and after that you can access to your photo object everywhere in your activity you like

Android: Taking picture with low quality [duplicate]

This question already has answers here:
Low picture/image quality when capture from camera
(3 answers)
Closed 3 years ago.
I created a project that allow user to take a picture and view it. I have no face any issue while taking, view, save and retrieve the photo. But my problem is the image that camera took is in bad quality, I don't know how to set the quality of the camera Intent. Here is my code looks like.
....
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 == RESULT_OK) {
Bitmap userPhoto = (Bitmap) data.getExtras().get("data");
iv_user.setImageBitmap(userPhoto);
}
}
Here is a screenshot of while camera is on.
And here is in preview camera mode after the image is captured.
If we compare these two photos, we can see the image in preview mode is blur.
Im actually not sure why it's like that but it's same here after every method tried, so I suggest the solution that you make use of a very simple library like below, good things is your picture quality remains intact.
simple add to your gradle file:
implementation 'com.theartofdev.edmodo:android-image-cropper:2.7.+'
Simple call startCameraAndOptions(); method to start you camera
startCameraAndOptions(); //call to starts you camera with other additional option like gallery.
public void startCameraAndOptions(View view) {
CropImage.activity()
.setGuidelines(CropImageView.Guidelines.ON)
.setActivityTitle("My Crop")
.setCropShape(CropImageView.CropShape.RECTANGLE)
.setCropMenuCropButtonTitle("Done")
// .setRequestedSize(400, 400)
.setAllowRotation(true)
.setOutputCompressQuality(100)
.setCropMenuCropButtonIcon(R.drawable.arrow)
.start(this);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
BitmapDrawable background;
Bitmap bitmap;
ImageView img_preview = (ImageView) findViewById(R.id.img_preview);
// handle result of CropImageActivity
if (requestCode == CropImage.CROP_IMAGE_ACTIVITY_REQUEST_CODE) {
CropImage.ActivityResult result = CropImage.getActivityResult(data);
if (resultCode == RESULT_OK) {
bitmap = null;
try {
bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), result.getUri());
background = new BitmapDrawable(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
Picasso.get()
.load(result.getOriginalUri())
// .resize(200, 200)
// .centerCrop()
.into(img_preview);
} else if (resultCode == CropImage.CROP_IMAGE_ACTIVITY_RESULT_ERROR_CODE) {
Toast.makeText(this, "Cropping failed: " + result.getError(), Toast.LENGTH_LONG).show();
}
}
}
**/* LAYOUT SECTION */ optional (style it your own way)**
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context="com.hollaport.hollaport.Demo">
<Button
android:layout_marginTop="10dp"
android:layout_gravity="center"
android:id="#+id/trigger"
android:layout_width="50dp"
android:layout_height="50dp"
android:background="#drawable/camera"/>
<!--<com.theartofdev.edmodo.cropper.CropImageView-->
<!--android:id="#+id/cropImageView"-->
<!--android:layout_width="match_parent"-->
<!--android:layout_height="0dp"-->
<!--android:layout_weight="1"/>-->
<android.support.v7.widget.CardView
android:id="#+id/view2"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_margin="5dp"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:innerRadius="0dp"
android:shape="rectangle"
android:thicknessRatio="2"
app:cardCornerRadius="0dp">
<ImageView
android:id="#+id/img_preview"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_gravity="center"
android:adjustViewBounds="true"
android:scaleType="fitXY"/>
</android.support.v7.widget.CardView>
</LinearLayout>
The Android Camera application encodes the photo in the return Intent delivered to onActivityResult() as a small Bitmap (we call it thumbnail) in the extras, under the key "data".
If you want to retrive full size image your app first need to store image to external storage and then read it back. You can access full code at
https://developer.android.com/training/camera/photobasics

How to add two different layouts to Listview?

I want to develop an interface like WhatsApp chat.
I implemented the whole interface with ListView. I can able to add text to ListView. But I don't know how to add an image to the same ListView as an attachment.
My main aim is to add text, images, audio files, and video files to single ListView.
Please help me.
Finally I got the answer.
Here is my code for combining the TextView and ImageView in ListView:
adding-more-than-two-different-layouts-to-listview.html
So if understand the question well, you would like to load an image from the SDcard or from the gallery and load into a listview item`s imageview, right ?
Load image from file:
File imgFile = new File("/sdcard/Images/test_image.jpg");
if(imgFile.exists()){
Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());
ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);
myImage.setImageBitmap(myBitmap);
}
or if you would like to select from gallery:
public void pickImage() {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_PHOTO_FOR_AVATAR);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO_FOR_AVATAR && resultCode == Activity.RESULT_OK) {
if (data == null) {
//Display an error
return;
}
InputStream inputStream = context.getContentResolver().openInputStream(data.getData());
//Now you can do whatever you want with your inpustream, save it as file, upload to a server, decode a bitmap...
}
}
You can call these methods from the listview onItemClick listener, and return with the Bitmap, when you are finished the loading of the image.

setting picture captured by built-in camera into an ImageView

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.

Categories

Resources