domingo, 26 de mayo de 2013

Gallery and camera image/file pick up!

What is it about?

Let start this blog with my first post and one that is easy to understand and very helpfull. Today I want to share with you a simple apps or example that it could be used in your app to pick images or use the camera to take a new one (like whatsapp, facebook, twitter) and use it for example in an ImageView or even save it into the SD Card of your Android.

The Source Code

GalleryCameraDemo source code

Discovering the code

The most importante part of the code is this one:

private View.OnClickListener buttonListener = new View.OnClickListener() {
    public void onClick(View v) {
    // Determine Uri of camera image to save.
    final File file = FileUtils.createFile(FileUtils.IMAGE_FILE);
    outputFileUri = Uri.fromFile(file);

    // Camera.
    final Intent captureIntent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
    captureIntent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);

    final Intent galleryIntent = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    // Filesystems
    // galleryIntent.setAction(Intent.ACTION_GET_CONTENT); // To allow file managers or any other app that are not gallery app.

    final Intent chooserIntent = Intent.createChooser(galleryIntent, "Select Image");
    // Add the camera options.
    chooserIntent.putExtra(Intent.EXTRA_INITIAL_INTENTS, new Intent[] { captureIntent });
    startActivityForResult(chooserIntent, YOUR_SELECT_PICTURE_REQUEST_CODE);

If you are an Android developer it is very simple to read the code. Basically, the View.OnClickListener attribute create two intent, one for the camera option and the other for the Image galleries and finally insert both intent into a third and final intent which is the "intentChooser".
It is pretty simple, in fact you can reuse this code and use it in your project.

But! There is always a but... you need some more code to make this really works:

public void onActivityResult(int requestCode, int resultCode, Intent data) {
    try {
        if (resultCode == Activity.RESULT_OK) {
            if (requestCode == YOUR_SELECT_PICTURE_REQUEST_CODE) {
                if (data != null) {
                    outputFileUri = data.getData();
                bmp = ImageUtils.getThumbnail(this, outputFileUri, THUMBNAIL_SIZE);
                if(check.isChecked()) {
                    FileUtils.saveImageFile(this, outputFileUri);
    } catch (IOException e) {

You need to override the onActivityResult in order to handle the data that is inside the Intent data. By calling data.getData() you get the URI which is the location where the image is.
Finally you need to convert that URI to a Bitmap, for this purpose you can use the ImageUtils class which is in the source code and put into an ImageView.

To save that Bitmap in the SD Card you can use the FileUtils.saveImageFile(Context context, Uri uri). All the related code is inside FileUtils to create the path, get the file and create folders.

You can download the Demo app from the Google Play:

Google Play Link

Remember! All this code is public and you can download and use it from github.

1 comentario: