Getting photos with intents on Android

During my class for app development, the instructor showed us how to get a photo from the phone’s gallery with intents. I wanted to take that one step farther, so I did. I also added getting a photo from the camera. To keep things small, I set the camera still to be returned as a thumbnail view, per the android instructions here:

https://developer.android.com/training/camera/photobasics.html

Here is what I have:

MainActivity.java

[CODE]

package com.example.alaskalinuxuser.photohowto;

import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;

public class MainActivity extends AppCompatActivity {

    // Define our image view.
    ImageView myImage;

    @Override // On create, do this....
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Declare our image view.
        myImage = (ImageView)findViewById(R.id.imageView);

    }

    // If they click to choose a picture from the gallery....
    public void choosePic (View picView) {

        // Call the intent for the gallery.
        Intent i = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
        // And start that intent for the result number 1.
        startActivityForResult(i, 1);

    }

    // If they click to take a picture button....
    public void takePic (View takeView) {

        // Call the intent for the camera.
        Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
        // And start that intent for the result number 2.
        if (takePictureIntent.resolveActivity(getPackageManager()) != null) {
            startActivityForResult(takePictureIntent, 2);
        }

    }

    @Override // Listen for the results from intents.
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        // If it is result number 1, and it was ok, and they chose something, then...
        if (requestCode == 1 && resultCode == RESULT_OK && data != null) {

            // Get the uri.
            Uri myChosenImage = data.getData();

            // Try in case it fails.
            try {

                // Make a bitmap from the uri.
                Bitmap myBitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), myChosenImage);

                // Set our chosen image to the view.
                myImage.setImageBitmap(myBitmap);

                // Give a catch in case there is a problem.
            } catch (IOException e) {
                e.printStackTrace();
            }

            // But if it is result number 2, and it is okay, and there is data, then....
        } else if (requestCode == 2 && resultCode == RESULT_OK && data != null) {

            try { // In case it fails.

                // Get the extras (a small thumbnail in this case).
                Bundle extras = data.getExtras();
                // Set our bitmap to that extra.
                Bitmap camImage = (Bitmap) extras.get("data");
                // Set our image with that bitmap.
                myImage.setImageBitmap(camImage);
                // A catch in case it fails.
            } catch (Exception e) {
                e.printStackTrace();
            }

        }


    }
}

[/CODE]

 

Pretty neat, huh? You can check this out on my GitHub under the small apps repository.

Linux – keep it simple.

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