Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using phones camera , after capturing image activity doesn't return back to particular activity

Tags:

android

camera

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.

like image 307
Nipam Shah Avatar asked Nov 04 '22 17:11

Nipam Shah


1 Answers

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

like image 128
Abhi Avatar answered Nov 15 '22 01:11

Abhi