Take a Still Photo

Press the camera button to take a photo and enable the other buttons. See Taking Photos Simply. This app lets you add the photo to your Gallery, but it doesn’t let you delete a photo from the Gallery. You can always go to the Gallery and delete it from there.

With a sampleSize of 1, the Bitmap we display in the ImageView would have one pixel for each pixel in the photograph. In other words, the Bitmap would have the same number of rows and columns of pixels as the photo. With a sampleSize of 2, the Bitmap would have one pixel for each 2 × 2 block of pixels in the photo. The Bitmap would have half the number of rows and half the number of columns as the photo. It would have one fourth the number of pixels as the photo. With a sampleSize of 4, the Bitmap would have one pixel for each 4 × 4 block of pixels in the photo. It would have 1/16th the number of pixels as the photo. This saves us memory.

Source code in Photo.zip

  1. MainActivity.java launches other activities.
  2. activity_main.xml consists of a vertical LinearLayout.
  3. border.xml is the background for the ImageView.
  4. strings.xml
  5. AndroidManifest.xml has <uses-feature> and <uses-permission>.
  6. build.gradle (Module: app)

Create the project

Select the folder app/res/drawable in the Android Studio project view and pull down
File → New → Drawable resource file
New Resource File
File name: border
OK

Run the project on the Genymotion emulator

To turn the camera on, click on the camera icon (a bagel resting on a horizontal line) in the right margin of the emulator, or press command-3 (on Mac). Turn the camera on and select Webcam0 for the front and back cameras.

Things to try

  1. The android:src attribute of the ImageButton is an icon file on the hard disk of your Mac or PC. Find it.
    find ~/Library/Android/sdk/platforms -type f -name ic_menu_camera.png
    /Users/myname/Library/Android/sdk/platforms/android-23/data/res/drawable-hdpi/ic_menu_camera.png
    /Users/myname/Library/Android/sdk/platforms/android-23/data/res/drawable-ldpi/ic_menu_camera.png
    /Users/myname/Library/Android/sdk/platforms/android-23/data/res/drawable-mdpi/ic_menu_camera.png
    /Users/myname/Library/Android/sdk/platforms/android-23/data/res/drawable-xhdpi/ic_menu_camera.png
    /Users/myname/Library/Android/sdk/platforms/android-23/data/res/drawable-xxhdpi/ic_menu_camera.png
    
    drawable-ldpi
    drawable-mdpi
    drawable-hdpi
    drawable-xhdpi
    drawable-xxhdpi

    What other icons are there?


  2. The app says that the photos are stored in the directory /storage/emulated/0/Pictures/; see the above screenshots. But they’re really somewhere else:
    adb devices
    adb -s 192.168.57.101:5555 shell
    
    cd /storage/emulated
    pwd
    /storage/emulated
    
    ls -l
    lrwxrwxrwx root     root              2015-08-20 19:38 legacy -> /mnt/shell/emulated/0
    
    find --help
    find / -type f -name '*.jpg'
    /mnt/shell/emulated/0/Pictures/image_0.jpg
    /data/media/0/Pictures/image_0.jpg
    
    ls -l `find / -type f -name '*.jpg'`
    -rw-rw---- root     sdcard_r   107845 2015-08-20 15:43 image_0.jpg
    -rw-rw-r-- media_rw media_rw   107845 2015-08-20 15:43 image_0.jpg
    
    ls -li `find / -type f -name '*.jpg'`
    3082844544 -rw-rw---- root     sdcard_r   107845 2015-08-20 15:43 image_0.jpg
        229423 -rw-rw-r-- media_rw media_rw   107845 2015-08-20 15:43 image_0.jpg
    
    exit
    

  3. Go to your launcher and launch the File Manager app. Your .jpg file(s) are stored in the Pictures folder even if you don’t store them in your Gallery.

  4. Give the user a chance to cancel a delete. Change
                    deleteButton.setEnabled(false);
                    fullsizeButton.setEnabled(false);
                    galleryButton.setEnabled(false);
                    if (!file.delete()) {
                        Toast toast = Toast.makeText(MainActivity.this, "\nCould not delete file " + file, Toast.LENGTH_LONG);
                        toast.show();
                    }
                    imageView.setImageBitmap(null);
    
    to the following. And put the strings into strings.xml.
                    AlertDialog.Builder builder = new AlertDialog.Builder(MainActivity.this);
                    builder.setTitle("Delete image");
                    builder.setMessage("Are you sure you want to delete this image?");
                    builder.setNegativeButton("Cancel", null);
                    builder.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
                        @Override
                        public void onClick(DialogInterface dialog, int which) {
                            deleteButton.setEnabled(false);
                            fullsizeButton.setEnabled(false);
                            galleryButton.setEnabled(false);
                            if (!file.delete()) {
                                Toast toast = Toast.makeText(MainActivity.this, "\nCould not delete file " + file, Toast.LENGTH_LONG);
                                toast.show();
                            }
                            imageView.setImageBitmap(null);
                        }
                    });
                    builder.show();
    

  5. Incorporate a timestamp into the photo’s name. See Save the Full-size Photo.