Working with Camera in Android

//
//Taking a photo using camera, save in default camera location and show in ImageView 
control-
//

<uses-feature
        android:name="android.hardware.camera"
        android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE" />

public void TakePhoto(View view)
{
    Intent imageIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    startActivityForResult(imageIntent, REQUEST_IMAGE_CAPTURE);
}

@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) 
{
    if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK)
    {
        Bundle bundle = data.getExtras();
        Bitmap bitmap = (Bitmap) bundle.get("data");

        ImageView imageView=(ImageView)findViewById(R.id.ivPhoto);
        imageView.setImageBitmap(bitmap);
    }
}

//
//Display photo from default Camera directory with a given name in an ImageView 
//

//Modify manifest file
<uses-feature
        android:name="android.hardware.camera"
        android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE" />

//Modify build.gradle(Module:app) as follows-
compile 'com.squareup.picasso:picasso:2.5.2'

public void ShowPhoto(View view)
{
    EditText PhotoNameTextbox=(EditText)findViewById(R.id.txtPhotoName);
    String PhotoName= PhotoNameTextbox.getText().toString() + ".jpg";

    //Get default camera directory
    File CameraDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString(), "Camera"); // DIRECTORY_PICTURES

    //Photo path
    File photoFile= new File(CameraDirectory + File.separator + PhotoName);

    if (photoFile.exists())
    {
        ImageView imageView=(ImageView)findViewById(R.id.ivPhoto);

       //Following lines can be another option to show photo. Currently not required.
       /*Bitmap bitmap = BitmapFactory.decodeFile(photoFile.getAbsolutePath());
        imageView.setImageBitmap(bitmap);*/

       //Picasso is a third party library. http://square.github.io/picasso/
       Picasso.with(getApplicationContext()).load(photoFile).fit()
              .centerCrop().into(imageView);

    }
    else
    {
       Toast.makeText(DisplayPhoto.this, "Photo not found", Toast.LENGTH_SHORT).show();
     }
}

//
//Take a photo, Save with custom name, then display in ImageView
//

//Modify manifest file
<uses-feature
        android:name="android.hardware.camera"
        android:required="true" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="ANDROID.PERMISSION.READ_EXTERNAL_STORAGE" />

//Modify build.gradle(Module:app) as follows-
compile 'com.squareup.picasso:picasso:2.5.2'

public class TakePhotoAndSaveWithCustomNameAndPath extends AppCompatActivity
{
    File CameraDirectory;
    String photoName;
    static final int REQUEST_IMAGE_CAPTURE = 101;
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_take_photo_and_save_with_custom_name_and_path);
        CameraDirectory = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM).toString(), "Camera");
    }
    
    public void TakePhoto(View view){
        Intent cameraIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
        
        photoName = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
        Log.d("CustomPhotoName",photoName);
        File photo = new File(CameraDirectory, photoName + ".jpg");
        Uri uriSavedImage = Uri.fromFile(photo);
        cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, uriSavedImage);
        startActivityForResult(cameraIntent, REQUEST_IMAGE_CAPTURE);
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        if (requestCode == REQUEST_IMAGE_CAPTURE && resultCode == RESULT_OK) {

            try{
                ImageView imageView=(ImageView)findViewById(R.id.imageviewImage);
                File photo = new File(CameraDirectory, photoName + ".jpg");
                if(photo.exists()){
                    //Picasso is a third party library. http://square.github.io/picasso/
                    Picasso.with(getApplicationContext()).load(photo).fit().centerCrop().into(imageView);
                }
                else{
                    Log.d("PhotoFileStatus","Not found");
                }
            }
            catch (Exception ex){
                Log.d("PhotoError",ex.getMessage().toString());
                Toast.makeText(TakePhotoAndSaveWithCustomNameAndPath.this, ex.getMessage().toString(), Toast.LENGTH_LONG).show();
            }

        }
    }
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s