5

I am capturing an image from my application using phone's camera and using android.provider.MediaStore.ACTION_IMAGE_CAPTURE.

After clicking capture button it shows save or discard. Clicking save button it returns back to camera not to application. and the image is saved in gallary not to the output file which i have declared.

Below is my code

    @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
    Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(in, 0);

}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
if (resultCode==RESULT_OK) {
    Bitmap bm=(Bitmap) data.getExtras().get("data");
    ByteArrayOutputStream bytes = new ByteArrayOutputStream();
    bm.compress(Bitmap.CompressFormat.JPEG, 40, bytes);

    //you can create a new file name "test.jpg" in sdcard folder.
    File f = new File(Environment.getExternalStorageDirectory()
                            + File.separator + "player1.jpg");
    try {
        f.createNewFile();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    //write the bytes in file
    FileOutputStream fo = null;
    try {
        fo = new FileOutputStream(f);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    try {
        fo.write(bytes.toByteArray());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }finally{
        Intent pic=new Intent(pic1.this, GameMenu.class);
        startActivity(pic);
        finish();
    }
}
else {
    Toast.makeText(getApplicationContext(), "Canceled", Toast.LENGTH_LONG).show();
    Intent pic=new Intent(pic1.this, Menu.class);
    startActivity(pic);
    finish();
}

Kindly suggest if any solution.. Thanks in advance...

EDIT 1: I have checked this code on samsung galaxy y duos (android 2.3.6) its not working properly. And checked same on galaxy pop(2.2.1) and HTC wildfire(2.3.5) its working perfect.

Nipam Shah
  • 51
  • 3

5 Answers5

2

Use below intent to capture the image and getTempFile() is where you mention the storage path

Intent photoPickerIntent= new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
                       photoPickerIntent.putExtra(MediaStore.EXTRA_OUTPUT, getTempFile());

                       photoPickerIntent.putExtra("outputFormat",Bitmap.CompressFormat.JPEG.toString());
                       photoPickerIntent.putExtra("return-data", true);
                       startActivityForResult(photoPickerIntent,TAKE_PICTURE);

getTempFile:

private Uri getTempFile() {


        File root = new File(Environment.getExternalStorageDirectory(), "Directory");
        if (!root.exists()) {
            root.mkdirs();
        }
             File file;

             file = new File(root,filename+".jpeg" );

             muri = Uri.fromFile(file);
             photopath=muri.getPath();

              Log.e("getpath",muri.getPath());
              return muri;

           }

Read here for more threads

Community
  • 1
  • 1
Abhi
  • 8,935
  • 7
  • 37
  • 60
0

If this is your whole code you have omitted setContentView(); Could be the reason it is not returning.

  @Override
protected void onCreate(Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    super.onCreate(savedInstanceState);
     // setContentView() here
    Intent in=new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(in, 0);

}

Is the onActivityResult() method called?

Lazy Ninja
  • 22,342
  • 9
  • 83
  • 103
0

I got the same problem, I have restart my device and test my application it works fine now. It might be possible with different code implementation application not responding. Try to reboot your device and test and let me know its working or not

Pratik
  • 30,639
  • 18
  • 84
  • 159
0

you should creat your file opening your camera. you give the file URI as extra to the camera intent:

/**
 * Capturing Camera Image will lunch camera app request image capture
 */
private void captureImage() {

    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    fileUri = getOutputMediaFileUri();

    intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);

    // start the image capture Intent
    startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
}

this is how I creat the file :

/**
 * Creating file uri to store image
 * 
 * 
 * @return Uri
 */
public Uri getOutputMediaFileUri() {
    return Uri.fromFile(getTempOutputMediaFile());
}

/**
 * returning File
 * 
 * 
 * @return File
 */
private File getTempOutputMediaFile() {

    File appDirectory = this.getFilesDir();

    File mediaStorageDir = new File(APP_NAME,
                YOUR_IMAGE_DIRECTORY_NAME);

    Log.i("mediaStorageDir", mediaStorageDir.getAbsolutePath());

    // Create the storage directory if it does not exist
    if (!mediaStorageDir.exists()) {
        if (!mediaStorageDir.mkdirs()) {
            Log.i(YOUR_IMAGE_DIRECTORY_NAME,
                    "Oops! Failed create "
                            + YOUR_IMAGE_DIRECTORY_NAME
                            + " directory");
            return null;
        }
    }

    // Create a media file name
    UUID temporaryImageUuid = UUID.randomUUID();
    File mediaFile;

    mediaFile = new File(mediaStorageDir.getPath() + File.separator
            + temporaryImageUuid + "." + YOUR_EXTENSION);

    return mediaFile;
}
ahmed_khan_89
  • 2,755
  • 26
  • 49
  • I need to put my Image file directly in the internal storage path - getFilesDir/Appname, not in external storage. – joao2fast4u Apr 14 '14 at 23:14
  • I updated my answer, that the picture is saved in local storage. – ahmed_khan_89 Apr 14 '14 at 23:18
  • The main issue is that not always Samsung devices are given a result in onActivityResult...Why is that? I just want to find out why. But thank you for your effort. – joao2fast4u Apr 14 '14 at 23:29
  • you are welcome :) you should may be update your question to "Why Samsung devices have sometimes a weird behavior with startActivityForResult() ? why ?" good luck :) – ahmed_khan_89 Apr 15 '14 at 00:20
0

hi check the following code.after capturing image use startPreview() to release.it works for me

        captureButton.setOnClickListener(new View.OnClickListener() {
                        @Override
            public void onClick(View v) {
                            mCamera.autoFocus(null);
                mCamera.takePicture(null, null, mPicture);
                Log.e("in capture button","onclick of this button"+ mCamera);
                mCamera.startPreview();
                                }
        });
image
  • 59
  • 1
  • 14
  • When do you use this code? Where is the captureButton? – joao2fast4u Apr 15 '14 at 10:44
  • means here i am not calling directly camera using intent.first create one button and then implement this code.it is similar to custom camera .in this we can add our required features. – image Apr 15 '14 at 10:49