0

I Have a activity that opens the Camera by starting ACTION_IMAGE_CAPTURE Intent:

Intent intent = new Intent(
            android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

    SimpleDateFormat dateformat = new SimpleDateFormat("ddMMyy");


    File photo1 = new File(Environment
            .getExternalStorageDirectory(), imageName);
    intent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(photo1));
    startActivityForResult(intent, 5);

After starting this intent it opens the image capture screen and sometimes after clicking on the capture button it doesn't return to my app (onActivityResult) it enforce me to take an image another time again, and it doen't close this screen only if i hit the back button.

I put a break point in OnActivityResult when debugging and it doen't stop in this method.

3 Answers3

0

Here is the sample application which gives option to select a image from either using camera or gallery and its also allows the option of cropping image. I hope this is what you exactly want.

Please see this https://github.com/lorensiuswlt/AndroidImageCrop

Its opensource download it and explore.

Juned
  • 6,290
  • 7
  • 45
  • 93
0

Here is my code for camera intent .It works perfectly ,you can try using it .

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.graphics.Bitmap;
import android.media.MediaPlayer;
import android.os.Bundle;
import android.util.Log;
import android.view.KeyEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.Toast;

public class PlayMenuActivity extends Activity implements OnClickListener {

    Intent intent;
    ImageButton cameraBtn, galleryBtn, maleBtn, femaleBtn;
    static Bitmap photo;
    Dialog dialog;
    static String gender;
    MediaPlayer mp; 

    protected static final int PHOTO_PICKED = 0;
    private static final int CAMERA_REQUEST = 1337;
    private static final int SELECT_PHOTO = 100;
    private static final String TEMP_PHOTO_FILE = "tempPhoto.jpg";
    protected boolean circleCrop = true;
    private final static String TAG = "GetImageFromGalleryActivity";


    // values for scaling image
    protected int outputX = 320;
    protected int outputY = 480;
    protected int aspectX = 2;
    protected boolean scale = true;
    protected int aspectY = 3;
    protected boolean return_data = false;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Log.i("hello", "oncreate PlayMenu");
        setContentView(R.layout.play_menu_screen);//setting layout
        //Image Buttons on activity screen
        cameraBtn = (ImageButton) findViewById(R.id.cameraBtn_id);
        galleryBtn = (ImageButton) findViewById(R.id.galleryBtn_id);
        cameraBtn.setOnClickListener(this);
        galleryBtn.setOnClickListener(this);
        // sound played when button clicked
        mp = MediaPlayer.create(this, R.raw.click);



    }

    public void onClick(View v) {
        switch (v.getId()) {
        case R.id.cameraBtn_id:
            // camera intent for starting camera
            Intent cameraIntent = new Intent(
                    android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            cameraIntent.putExtra("crop", "true");
            cameraIntent.putExtra("aspectX", aspectX);
            cameraIntent.putExtra("aspectY", aspectY);
            cameraIntent.putExtra("outputX", outputX);
            cameraIntent.putExtra("outputY", outputY);
            cameraIntent.putExtra("scale", scale);
            cameraIntent.putExtra("category", "camera");
            startActivityForResult(cameraIntent, CAMERA_REQUEST);

            break;

        case R.id.galleryBtn_id:
            // calling GalleryActivity for picking image from gallery
            intent = new Intent(PlayMenuActivity.this, GalleryActivity.class);

            startActivity(intent);
            break;

        default:
            break;
        }

    }

    /* creates bitmap of the captured photo */
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {

        // Log.i("hello", "REQUEST cALL");
        //if a camera request is made and resultcode matches then bitmap is created
        if (requestCode == CAMERA_REQUEST || resultCode == Activity.RESULT_OK) {
            Log.i("hello", "REQUEST cALL");
            try {
                Log.i("hello", "Try Call");
                Bitmap bMap = (Bitmap) data.getExtras().get("data");//creating bitmap 


                photo = bMap;
                Intent intent = new Intent(PlayMenuActivity.this,
                        ShowActivity.class);
                intent.putExtra("category", "camera");//adding category selected ie camera
                startActivity(intent);

            } catch (Exception e) {
                Log.i("hello", "Exception" + e.getMessage());
            }

        } else {
            // Log.i("hello", "Else call");

            Toast.makeText(PlayMenuActivity.this, "Picture NOt taken",
                    Toast.LENGTH_LONG).show();
        }

    } // fn



}// class

Hope this helps .

Rachita Nanda
  • 4,509
  • 9
  • 42
  • 66
0

this is very late, but the issue here could be missing read/write permissions. I had this very issue myself and it had to do with me wanting to put the image file into my own app folder, to which the camera app had no access to.

To be safe, when sharing files betweens apps, consider using Environment.getExternalStoragePublicDirectory(), instead of Environment.getExternalStorageDirectory().

ghost23
  • 2,150
  • 1
  • 20
  • 35