Android Camera activity stays open - android

I'm trying to take a picture with my Android app but when I take the image, the camera display doesn't go away. The code I am using is below. I also used the SDK on Google's developer website SDK.
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button)findViewById(R.id.mainBtn);
mainButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Intent userCreationIntent = new Intent(v.getContext(), SecondviewActivity.class);
//startActivity(userCreationIntent);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
return false;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}

The problem is within your onTouchListener for the Button!
I just ran your code and changed the onTouchListener to a onClickListener for the Button instead and the code is working.
See my revised code here:
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button) findViewById(R.id.mainBtn);
mainButton.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0) {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView) findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
}
}
}
I'm not precisely sure, why you're using the onTouchListener for a Button, but it's not working apparently - really weird behavior actually ;-)
EDIT: Just a small update. I tried to debug the code and if you use the onTouchListener instead of the onClickListener, when clicking on the button, you actually trigger 3 MotionEvents: MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP.
Now the first event MotionEvent.ACTION_DOWN will trigger your intent to show the camera and when you click OK after you've taken a picture, the next MotionEvent MotionEvent.ACTION_MOVE is waiting in line to be triggered and this will send you to the camera activity once again. Now after taking one more picture and clicking OK, you return to your activity and now the last MotionEvent MotionEvent.ACTION_UP is waiting in line and triggers a 3rd call to the camera activity. After the last camera call, you will be able to get back to your activity without problems ;-)
Why the onClickListener doesn't do this, is because it handles a "full" click which could be MotionEvent.ACTION_DOWN, MotionEvent.ACTION_MOVE and MotionEvent.ACTION_UP and so all 3 events is happening in ONE click instead.
Hope this helps ;-)

This is happening because you are not attaching a path with the intent as to tell Android where to store the image. I had the same problem too.
Try the following code: ( I just added 4 lines )
import android.os.Bundle;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.Camera;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
// Creating the Uri where Camera saves a picture each time
String imagePath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/" + System.currentTimeMillis() + "_myCameraImage.jpg";
File imageFile = new File(imagePath);
imageUri = Uri.fromFile(imageFile);
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button mainButton = (Button)findViewById(R.id.mainBtn);
mainButton.setOnTouchListener(new View.OnTouchListener() {
#Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
//Intent userCreationIntent = new Intent(v.getContext(), SecondviewActivity.class);
//startActivity(userCreationIntent);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
cameraIntent.putExtra(android.provider.MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
return false;
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
ImageView imageView = (ImageView)findViewById(R.id.imageView1);
imageView.setImageBitmap(photo);
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_main, menu);
return true;
}
}
Before you copy and paste this code, make sure that you have necessary import statements in your code like import statements for File, Uri etc.
I strongly hope it will work now :)

Related

saving and then displaying camera photo on application

I'm trying to make a android app that will take a picture from the camera and display it on the same screen next to the "take picture button". I've been trying to use a tutorial but it is not working. whenever I take the picture and press save, the picture app fails. Also I don't know if it would display the photo on screen if I saved it. However, I am not sure of this since it always breaks after I press save.
Does anybody have any suggestions to help me save and display the photo on screen?
Thanks
Here's my xml:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:paddingBottom="#dimen/activity_vertical_margin"
android:paddingLeft="#dimen/activity_horizontal_margin"
android:paddingRight="#dimen/activity_horizontal_margin"
android:paddingTop="#dimen/activity_vertical_margin"
tools:context="com.example.pictureproject.MainActivity"
android:orientation ="vertical">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="#string/hello_world" />
<Button
android:id="#+id/button_camera"
android:text="#string/button_camera"
android:layout_width="fill_parent"
android:layout_height="wrap_content"/>
<ImageView
android:id = "#+id/image_camera"
android:contentDescription="#string/image_cd_camera"
android:layout_width="fill_parent"
android:layout_height = "wrap_content"
/>
</LinearLayout>
and Heres my java:
package com.example.pictureproject;
import java.io.File;
import android.app.Activity;
import android.content.ContentResolver;
import android.view.View.OnClickListener;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.support.v7.app.ActionBarActivity;
import android.util.Log;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends ActionBarActivity {
private static String logtag ="CameraApp8";
private static int TAKE_PICTURE = 1;
private Uri imageUri;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Button cameraButton = (Button)findViewById(R.id.button_camera);/*possibly button camera 1*/
cameraButton.setOnClickListener(cameraListener);
}
private OnClickListener cameraListener = new OnClickListener() {
public void onClick(View v){
takePhoto(v);
}
};
private void takePhoto(View v){
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photo = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"picture.jpg");
imageUri = Uri.fromFile(photo);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE);
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent intent){
super.onActivityResult(requestCode, resultCode, intent);
if(resultCode == Activity.RESULT_OK){
Uri selectedImage = imageUri;
getContentResolver().notifyChange(selectedImage, null);
ImageView imageView = (ImageView)findViewById(R.id.image_camera);
ContentResolver cr = getContentResolver();
Bitmap bitmap;
try{
bitmap = MediaStore.Images.Media.getBitmap(cr,selectedImage);
imageView.setImageBitmap(bitmap);
Toast.makeText(MainActivity.this, selectedImage.toString(),Toast.LENGTH_LONG).show();
}catch(Exception e){
Log.e(logtag,e.toString());
}
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
}
try to add this code..
public class MyCameraActivity extends Activity {
private static final int CAMERA_REQUEST = 1888;
private ImageView imageView;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
this.imageView = (ImageView)this.findViewById(R.id.imageView1);
Button photoButton = (Button) this.findViewById(R.id.button1);
photoButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_REQUEST);
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAMERA_REQUEST && resultCode == RESULT_OK) {
Bitmap photo = (Bitmap) data.getExtras().get("data");
imageView.setImageBitmap(photo);
}
}
}

Android Capture image with filename

this is my first day on android and i would like to make an app that will capture an image and the image name will output at the variable and textbox under the ImageView.
This is my code now and capture image is functioning. What i need to do next is to get the file name and filepath. Thanks. I tried to find but i dont know where to put the codes.
import android.support.v7.app.ActionBarActivity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class Create extends ActionBarActivity {
Button capImg;
int requestcode = 1;
ImageView imgView;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_create);
capImg = (Button)findViewById(R.id.capImg);
imgView = (ImageView)findViewById(R.id.imgView);
capImg.setOnClickListener(new OnClickListener() {
#Override
public void onClick(View arg0){
Intent i = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
if(i.resolveActivity(getPackageManager())!=null){
startActivityForResult(i, requestcode);
}
}
});
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.create, menu);
return true;
}
#Override
public boolean onOptionsItemSelected(MenuItem item) {
// Handle action bar item clicks here. The action bar will
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
int id = item.getItemId();
if (id == R.id.action_settings) {
return true;
}
return super.onOptionsItemSelected(item);
}
public void onActivityResult(int requestCode, int resultcode, Intent data){
if(requestCode==requestcode){
if(resultcode==RESULT_OK){
Bundle bundle = new Bundle();
bundle = data.getExtras();
Bitmap BMP;
BMP = (Bitmap)bundle.get("data");
imgView.setImageBitmap(BMP);
}
}
}
}
To save a picture Add a fileUri to the intent.
private Uri fileUri;
fileUri = getOutputMediaFileUri(MEDIA_TYPE_VIDEO); // create a file to save the video
intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri); // set the image file name
http://developer.android.com/guide/topics/media/camera.html
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
if (resultCode == RESULT_OK) {
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
data.getData(), Toast.LENGTH_LONG).show();
} else if (resultCode == RESULT_CANCELED) {
// User cancelled the image capture
} else {
// Image capture failed, advise user
}
}

Android ImageView content disapearing after showing

My application does this: takes a photo, then show the photo in an ImageView. The weird thing is that the photo is displayed for about a second (after taking it with the camera), and then the ImageView is empty again.
This is my code:
publish.xml
<ImageView
android:id="#+id/itemImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
/>
PublishActivity.java
package ar.com.guiagratis;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;
public class PublishActivity extends Activity {
final int TAKE_PICTURE_REQUEST_CODE = 115;
#Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.publish);
}
public void btnNextClick(View v) {
// TODO: disable all buttons
//Intent intent=new Intent(getApplicationContext(), TakePhotoActivity.class);
// startActivityForResult(intent, TAKE_PICTURE_RESULT_CODE);
Toast.makeText(getApplicationContext(), "Sacate una foto viteh", Toast.LENGTH_SHORT).show();
Intent intent = new Intent("android.media.action.IMAGE_CAPTURE");
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, TAKE_PICTURE_REQUEST_CODE);
}
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
switch (requestCode) {
case TAKE_PICTURE_REQUEST_CODE:
if (resultCode == Activity.RESULT_OK) {
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
// Image captured and saved to fileUri specified in the Intent
Toast.makeText(this, "Image saved to:\n" +
imageUri, Toast.LENGTH_LONG).show();
Bitmap myBitmap = BitmapFactory.decodeFile(imageUri.getPath());
BitmapDrawable ob = new BitmapDrawable(myBitmap);
ImageView myImage = (ImageView) findViewById(R.id.itemImage);
myImage.setBackgroundDrawable(ob);
Toast.makeText(getApplicationContext(), "Qué linda foto! ", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(getApplicationContext(), "Hubo un problema al subir la imágen... ", Toast.LENGTH_SHORT).show();
}
}
}
}
Any help will be appreciated.
You're using setBackgroundDrawable(Drawable drawable) which sets the View's background.
If you want to change the ImageView's content you need to use
setImageDrawable(Drawable drawable)
or
setImageBitmap(Bitmap bm)
Ok, I finally found the problem. I was not aware that I need to use onSaveInstanceState and onRestoreInstanceState to store the values I don't want to loose from an Activity when I'm doing certain things, like initiating the Camera and taking a photo.
So I added this code:
#Override
public void onSaveInstanceState(Bundle savedInstanceState) {
super.onSaveInstanceState(savedInstanceState);
// Save UI state changes to the savedInstanceState.
// This bundle will be passed to onCreate if the process is
// killed and restarted.
savedInstanceState.putBoolean("photoUploaded", photoUploaded);
}
#Override
public void onRestoreInstanceState(Bundle savedInstanceState) {
super.onRestoreInstanceState(savedInstanceState);
// Restore UI state from the savedInstanceState.
// This bundle has also been passed to onCreate.
photoUploaded = savedInstanceState.getBoolean("photoUploaded");
if (photoUploaded) {
File photoFile = new File(Environment.getExternalStorageDirectory(), "Photo.png");
Uri imageUri = Uri.fromFile(photoFile);
Bitmap myBitmap = BitmapFactory.decodeFile(imageUri.getPath());
BitmapDrawable ob = new BitmapDrawable(myBitmap);
ImageView myImage = (ImageView) findViewById(R.id.uploadedImage);
myImage.setBackgroundDrawable(ob);
}
}
and now is working.
Not sure how to do to make this question as solved.

Audio Recorder Android

I'm doing a aplication to recorder the sound, but when I try to record the sound,the emulator is stopped because it ask me to insert an SD card. What I have to do? I couldn't do without an SD card ?
Thank you!
package proiektua.proiektua;
import java.io.FileDescriptor;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.app.Activity;
import android.content.Intent;
import android.view.Menu;
import android.view.View;
public class MainActivity extends Activity {
int peticion = 1;
Uri url1;
private int STATE_CONFIGURE;
private int mState = STATE_CONFIGURE;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
public void grabar(View v) {
Intent intent = new Intent(MediaStore.Audio.Media.RECORD_SOUND_ACTION);
startActivityForResult(intent, peticion);}
public void setOutputFile(FileDescriptor fd) {
switch (mState) {
case STATE_RECORD:
throw new RuntimeException("setOutputFile cannot be called while recording!");
case STATE_RELEASED:
throw new RuntimeException("setOutputFile called on an already released recorder!");
default:
break;
}
}
public void reproducir(View v) {
MediaPlayer mediaPlayer = MediaPlayer.create(this, url1);
mediaPlayer.start();
}
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (resultCode == RESULT_OK && requestCode == peticion) {
url1 = data.getData();
}
}
}
You have the option to create an SDCard through the mksdcard command and associate it with the amulator.
please look at this function:
public void setOutputFile (FileDescriptor fd)
Added in API level 3
Pass in the file descriptor of the file to be written. Call this after setOutputFormat() but before prepare().
You can decide where to store the file.
also look at this example.
http://androidfreakers.blogspot.co.il/2011/09/using-mediarecorder.html

Android Camera : Picture should not be stored locally but should be saved temporarily in the App

I am new to android and am trying to make an android app where the user can click an image and save it to a database . However i do not want the image to be stored locally in the gallery folder. Or everytime a picture is taken it saves itself in a self made directory on the phone and keeps replacing earlier pics .I donot want all pictures to be stored on the phone .
Below is my current code :
package com.example.camerastart;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class CameraMainActivity extends Activity
{
private static final int CAMERA_PIC_REQUEST = 2500;
private Button cam_button;
#Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_camera_main);
cam_button = (Button) findViewById(R.id.button1);
cam_button.setOnClickListener(new OnClickListener()
{
#Override
public void onClick(View v)
{
Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(cameraIntent, CAMERA_PIC_REQUEST);
// TODO Auto-generated method stub
}
});
}
protected void onActivityResult(int requestCode, int resultCode, Intent data)
{
if (requestCode == CAMERA_PIC_REQUEST && resultCode == RESULT_OK)
{
Bitmap image = (Bitmap) data.getExtras().get("data");
ImageView imageview = (ImageView) findViewById(R.id.imageView1);
imageview.setImageBitmap(image);
}
}
#Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.activity_camera_main, menu);
return true;
}
}
any help will be appreciated
You can make a folder, and save all the images to this folder.
just remember to add .nomedia file to this folder, and then the gallery will not show those files

Categories

Resources