In android there have several application where you need to work with Camera Intent. There have two ways to complete your task :
1. Use the default camera intent to your application or
2. Customize your own camera intent .
I have worked with android default camera where capture the Image of user and sent that picture to the server and detect the mood from the image of user. After sending image to the server I need to delete the Image from the Phone Memory. Otherwise, application would take the maximum storage of phone memory and that Image is not necessary for the Users.
So lets start coding. Here is the cameraActivity.java which will do the task.
public class cameraActivity extends MainActivity{
private static final int CAMERA_REQUEST_CODE = 100;
boolean _taken;
public void startCameraActivity() {
Intent cameraIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult( cameraIntent , CAMERA_REQUEST_CODE);
}//end of startCameraActivity()
/**
* onActivityResult for capturing image
* reqestCode is the CAMERA_REQUEST_CODE
* resultCode is the result of camera
*/
public void onActivityResult(int requestCode, int resultCode, Intent intent) {
if(requestCode == CAMERA_REQUEST_CODE) {
if(resultCode == RESULT_OK) {
onPhotoTaken(intent);
}else {
Toast.makeText(getApplicationContext(), "Camera request rejected!", Toast.LENGTH_LONG).show();
}//end of if else condition
}//end of if condition
}//end of onActivityResult
/**
* In this method what will do after capturing image
* @param i is the image sending from camera intent
*/
public void onPhotoTaken(Intent i) {
_taken = true;
serverActivity server = new serverActivity();
Bitmap bitmap = (Bitmap) i.getExtras().get("data");
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 0, bos);
byte[] imageData = bos.toByteArray();
server.insertImageIntoServer(imageData); //passing image to save image into server
}//end of onPhotoTaken()
/**
* Last Image will be delete from the Image Gallery after Sending Image to the Server
*/
public void deleteLastImageFromGallery() {
int i=0;
String[] projection = {MediaStore.Images.Media.DATA};
Cursor cursor = this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, projection, null, null, null);
ArrayList<String> imagePath = new ArrayList<String>();
while(cursor.moveToNext()) {
imagePath.add(cursor.getString(0));
i++;
}//end of while loop
cursor.close();
i--;
File file = new File(imagePath.get(i));
file.delete();
Toast.makeText(getApplicationContext(), "Last image has been deleted!", Toast.LENGTH_LONG).show();
}//end of deleteLastImageFromGallery()
}//end of main class
I hope you have got the point how it is working. Let start to code yourself after getting the main theme of using camera intent . In coding time you will face different problem after that you will learn. If you won't face any bugs/errors then you will not be able to learn because after solving bugs we will learn to solve bugs. And I think this is the best way to learn by solving errors.
This below link will help you to learn in depth of camera intent :
Camera Intent Android Developer
This comment has been removed by the author.
ReplyDelete