0

I am using the following code for clicking a photo on Button click.

package com.example.clickpic;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends Activity {
int TAKE_PHOTO_CODE = 0;
public static int count=0;
private ImageView imageView;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState)
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);

//here,we are making a folder named picFolder to store pics taken by the camera using  this application
    final String dir =  Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES) +  "/picFolder/"; 
    File newdir = new File(dir); 
    newdir.mkdirs();
    this.imageView = (ImageView)this.findViewById(R.id.imageView1);
  Button capture = (Button) findViewById(R.id.btnCapture);
  capture.setOnClickListener(new View.OnClickListener() {
    public void onClick(View v) {

        // here,counter will be incremented each time,and the picture taken by camera  will be stored as 1.jpg,2.jpg and likewise.
        count++;
        String file = dir+count+".jpg";
        File newfile = new File(file);
        try {
            newfile.createNewFile();
        } catch (IOException e) {}       

        Uri outputFileUri = Uri.fromFile(newfile);

        Intent cameraIntent = new  Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE); 
        startActivityForResult(cameraIntent, 1888); 
    }
});
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
    if (requestCode == 1888 && resultCode == RESULT_OK) {  
        Bitmap photo = (Bitmap) data.getExtras().get("data"); 
        imageView.setImageBitmap(photo);
    }  
} 

} 

The code works perfectly well but it just opens up the camera activity. What must I do so that on button click, it clicks a picture and saves it as well?

Right now, this just opens the camera app of the phone, and expects me to click the picture and click on OK. I don't want these things.

All I want is, when I click on the button from my app it should click a picture without expecting any additional input from me.

Edit

I have already tried to create a camera app of my own, but I ran into some issues. That's why I am trying this approach. Camera API not working on KITKAT

halfer
  • 19,824
  • 17
  • 99
  • 186
SoulRayder
  • 5,072
  • 6
  • 47
  • 93

2 Answers2

1

All I want is, when I click on the button from my app it should click a picture without expecting any additional input from me. -

Its not achieved by Camera Intent like android.provider.MediaStore.ACTION_IMAGE_CAPTURE. You have to implement camera interface portion in your code without using native camera application of Device.

Look at portion of Building Camera App

user370305
  • 108,599
  • 23
  • 164
  • 151
  • I have tried doing this as well.. but I ran into some issues stackoverflow.com/questions/22625904/…. Please help me solve this – SoulRayder Mar 26 '14 at 09:33
  • This post may also help http://androideasylessons.blogspot.in/2012/09/take-photo-without-intent-in-android.html – Droidman Mar 26 '14 at 09:35
  • Also http://developer.android.com/training/camera/cameradirect.html and http://www.tutorialspoint.com/android/android_camera.htm – user370305 Mar 26 '14 at 09:37
  • Just follow the instructions given in posts you will definitely able to build your camera app. – user370305 Mar 26 '14 at 09:38
1

I have written a camera class which takes picture, arranges the orientation (some devices takes photo horizontal as default) and saves the photo taken. You can check it from the link below:

Camera capture orientation on samsung devices in android

Edit: Sorry, savePhoto functions are not written in my example. Adding them now.

savePhoto function:

public void savePhoto(Bitmap bmp) {

    imageFileFolder = new File(Environment.getExternalStorageDirectory(),
            cc.getDirectoryName());
    imageFileFolder.mkdir();
    FileOutputStream out = null;
    Calendar c = Calendar.getInstance();
    String date = fromInt(c.get(Calendar.MONTH))
            + fromInt(c.get(Calendar.DAY_OF_MONTH))
            + fromInt(c.get(Calendar.YEAR))
            + fromInt(c.get(Calendar.HOUR_OF_DAY))
            + fromInt(c.get(Calendar.MINUTE))
            + fromInt(c.get(Calendar.SECOND));
    imageFileName = new File(imageFileFolder, date.toString() + ".jpg");
    try {
        out = new FileOutputStream(imageFileName);
        bmp.compress(Bitmap.CompressFormat.JPEG, 70, out);
        out.flush();
        out.close();
        scanPhoto(imageFileName.toString());
        out = null;
    } catch (Exception e) {
        e.printStackTrace();
    }
}

scanPhoto function:

public void scanPhoto(final String imageFileName) {
    geniusPath = imageFileName;
    msConn = new MediaScannerConnection(MyClass.this,
            new MediaScannerConnectionClient() {
                public void onMediaScannerConnected() {
                    msConn.scanFile(imageFileName, null);

                }

                @Override
                public void onScanCompleted(String path, Uri uri) {

                    msConn.disconnect();

                }
            });
    msConn.connect();
}

SavePhotoTask class:

class SavePhotoTask extends AsyncTask<byte[], String, String> {
    @Override
    protected String doInBackground(byte[]... jpeg) {
        File photo = new File(Environment.getExternalStorageDirectory(),
                "photo.jpg");
        if (photo.exists()) {
            photo.delete();
        }
        try {
            FileOutputStream fos = new FileOutputStream(photo.getPath());
            fos.write(jpeg[0]);
            fos.close();
        } catch (java.io.IOException e) {
        }
        return (null);
    }
}
Community
  • 1
  • 1
canova
  • 3,965
  • 2
  • 22
  • 39