Taking pictures with camera on Android programmatically
Look at following demo code.
Here is your XML file for UI,
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btnCapture" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Camera" /></LinearLayout>
And here is your Java class file,
public class CameraDemoActivity extends Activity { int TAKE_PHOTO_CODE = 0; public static int count = 0; /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.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(); Button capture = (Button) findViewById(R.id.btnCapture); capture.setOnClickListener(new View.OnClickListener() { public void onClick(View v) { // Here, the 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(MediaStore.ACTION_IMAGE_CAPTURE); cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri); startActivityForResult(cameraIntent, TAKE_PHOTO_CODE); } }); } @Override protected void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); if (requestCode == TAKE_PHOTO_CODE && resultCode == RESULT_OK) { Log.d("CameraDemo", "Pic saved"); } }}
Note:
Specify the following permissions in your manifest file,
<uses-permission android:name="android.permission.CAMERA"/><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
There are two ways to take a photo:
1 - Using an Intent to make a photo
2 - Using the camera API
I think you should use the second way and there is a sample code here for two of them.
You can use Magical Take Photo library.
1. try with compile in gradle
compile 'com.frosquivel:magicaltakephoto:1.0'
2. You need this permission in your manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/><uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /><uses-permission android:name="android.permission.CAMERA"/>
3. instance the class like this
// "this" is the current activity param
MagicalTakePhoto magicalTakePhoto = new MagicalTakePhoto(this,ANY_INTEGER_0_TO_4000_FOR_QUALITY);
4. if you need to take picture use the method
magicalTakePhoto.takePhoto("my_photo_name");
5. if you need to select picture in device, try with the method:
magicalTakePhoto.selectedPicture("my_header_name");
6. You need to override the method onActivityResult of the activity or fragment like this:
@Overridepublic void onActivityResult(int requestCode, int resultCode, Intent data) { super.onActivityResult(requestCode, resultCode, data); magicalTakePhoto.resultPhoto(requestCode, resultCode, data); // example to get photo // imageView.setImageBitmap(magicalTakePhoto.getMyPhoto());}
Note: Only with this Library you can take and select picture in the device, this use a min API 15.