
Start Camera Intent without Specified File Path
Basically, all you need to do is create an Intent indicating that you want to capture and image. The value indicating that you would like to capture an image is MediaStore.ACTION_IMAGE_CAPTURE. After when you have created the Intent, we then would want to Activity.startActivityForResult().
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Start camera intent without specified filepath. | |
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
startActivityForResult(cameraIntent, CAMERA_NO_FILEPATH); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (resultCode == RESULT_OK) { | |
if (requestCode == CAMERA_NO_FILEPATH) { | |
// Get the image from intent data. | |
Bundle bundle = data.getExtras(); | |
// This Bundle object will contain the Bitmap image, | |
// so no Bitmap decoding will be required. | |
Bitmap img = (Bitmap) bundle.get("data"); | |
mImage.setImageBitmap(img); | |
} | |
} |
Start Camera Intent with Specified File Path
In order to provide the Intent with a file path, we must come-up with a file path that we would like it to save it to and the filename. Different device have different starting file paths and to do this safely, I first check there is internal storage available and if there is then I retrieve the root public folder of the device's internal storage. This can be done using Android's Environment class. Then in that public root directory I create a standard Android pictures folder using Environment.DIRECTORY_PICTURES. This code is shown below:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* | |
* Creates a new file path into the standard Android pictures directory. | |
*/ | |
private String getOutputLink(String filename) { | |
String directory = ""; | |
// Check if storage is mounted. | |
if (Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)) { | |
File mediaStorageDir = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES), | |
getResources().getString(R.string.app_name)); | |
// Create the storage directory if it does not exist. | |
if (!mediaStorageDir.exists()) { | |
if (!mediaStorageDir.mkdirs()) { | |
return null; | |
} | |
} | |
directory = mediaStorageDir.getPath() + File.separator + filename; | |
} | |
return directory; | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Create a new File object with specified filepath, where the | |
// captured image will be located. | |
File file = new File(getOutputLink(TEMP_JPEG_FILENAME)); | |
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | |
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, Uri.fromFile(file)); | |
startActivityForResult(cameraIntent, CAMERA_WITH_FILEPATH); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
if (resultCode == RESULT_OK) { | |
... | |
else if (requestCode == CAMERA_WITH_FILEPATH) { | |
// Get the image from the filepath you specified when you | |
// started the camera intent. | |
String filepath = getOutputLink(TEMP_JPEG_FILENAME); | |
Bitmap img = BitmapFactory.decodeFile(filepath); | |
mImage.setImageBitmap(img); | |
Toast.makeText(this, "Image is saved in: " + filepath, Toast.LENGTH_SHORT).show(); | |
} | |
} |
You can view my Android Camera Intent example HERE.
Relevant files are:
src/com/choiboi/apiexamples/camera/CameraMainActivity.java
res/layout/activity_camera_main.xml
Hope this was helpful!!