0

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;
}
}
Destiny Dawn
  • 1,457
  • 3
  • 15
  • 29

2 Answers2

1

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 :)

Arslan Ali
  • 17,418
  • 8
  • 58
  • 76
1

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 ;-)

Darwind
  • 7,284
  • 3
  • 49
  • 48
  • Thank you so much! It's working now! One more question (I am still giving you the reputation) but how can I call the photo gallery? – Destiny Dawn Apr 04 '13 at 20:29
  • No problem ;-) To call the gallery: `Intent intent = new Intent(); intent.setType("image/*"); intent.setAction(Intent.ACTION_GET_CONTENT); startActivityForResult(Intent.createChooser(intent, "Select Picture"), 1);` Or see http://stackoverflow.com/questions/2169649/get-pick-an-image-from-androids-built-in-gallery-app-programmatically – Darwind Apr 04 '13 at 20:33
  • Any time - I actually figured out what happened, so I extended my answer a bit :-) Now give me the reputation :P – Darwind Apr 04 '13 at 20:53
  • when using the gallery, how do I get the content that was selected? – Destiny Dawn Apr 04 '13 at 21:06
  • The thread is getting too cluttered - finish this question and ask a new one - ;-) – Darwind Apr 04 '13 at 21:13