martes, 28 de mayo de 2013

Navigation Drawer and ActionBarSherlock, working in Devices pre HC

Something you really enjoy!


Today I want to share my latest project, one that you really need it. Recently Google update the support package to revision 13. With this new revision google introduce a new UX Component that was approved by Matias Duarte days ago and that is why they included in latest support library.

ActionBarSherlock and support library

ActionBarSherlock (ABS) is an Android library developed by Jake Wharton, if you don't know about him or his projects check his github user:


The goal of this project is to provide the Action Bar component, introduced in Honeycomb and Ice Cream Sandwich, for pre Honeycomb devices.
As is expected to achieve this goal ActionBarSherlock use the Android support package, and here lies the problem: the Android Support package that use is old and is not up to date.

Sherlock Navigation Drawer

To solve this problem I developed this project. The goal of this project is to use the new Navigation Drawer that comes with Android Support revision 13 and make it available for pre Honeycomb Devices.
It should work exactly as Google Drive app works, for Android 2.2+.

As usual the project is hosted in Github with Apache Licence. Feel free to use it, leave comments and everything you want.

SherlockNavigationDrawer link

You can download the Demo app from the Google Play:

Google Play Link

NOTE: I will update this post in these days to show and explain a little bit the code.

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() {
    @Override
    public void onClick(View v) {
    // Determine Uri of camera image to save.
    FileUtils.createDefaultFolder(MainActivity.this);
    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);
    galleryIntent.setType("image/*");
    // 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:

@Override
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);
                image.setImageBitmap(bmp);
                if(check.isChecked()) {
                    FileUtils.saveImageFile(this, outputFileUri);
                }
            }
        }
    } catch (IOException e) {
        System.out.println(e.getMessage());
    }
}

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.

The aim of this blog

The goal of this blog is to provide simple and useful examples of different topics related to coding in Android. To achieve this goal all of this examples, libraries and whatever you see here is open source and could be downloaded from my github's repository. All the source code is under Apache Licence so you could use it in your own app or project.