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
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
}
}
These codes can only capture and display image I took, and when I pressed save button, it only saves "Note title" onto my list View. And when I clicked "Note Title" it doesn't display the picture anymore. What should I add to save it and update it? Thanks.
protected void onActivityResult(int requestCode, int resultCode, Intent
data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap mphoto = (Bitmap) data.getExtras().get("data");
imageviewid.setImageBitmap(mphoto);
}
}
public void takeImageFromCamera(View view) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
private void saveNote(){
fillNameIfEmpty();
id = DbAccess.addNote(getBaseContext(), etName.getText().toString(), mFileName, DbContract.NoteEntry.TYPE_PHOTO, currentCat);
Toast.makeText(getApplicationContext(), R.string.toast_saved, Toast.LENGTH_SHORT).show();
}
private void updateNote(){
fillNameIfEmpty();
DbAccess.updateNote(getBaseContext(), id, etName.getText().toString(), mFileName, currentCat);
Toast.makeText(getApplicationContext(), R.string.toast_updated, Toast.LENGTH_SHORT).show();
}
Your DbAccess API doesn't know anything about an image in the note!
So I would extend your code to:
After picture is taken it saved in fail in application sandbox
Save filename of image assigned to note to database together with note
And don't forget to handle editing or deletion picture use cases!
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.
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.
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.