Flickr Pickr
Cameron Webb and I have been working on this Android project for our independent study. It is now complete!
Cameron Webb and I have been working on this Android project for our independent study. It is now complete!
This tutorial will show you how to use the built in image gallery in your Android application to access pictures on the SD card. You will learn how to start the activity, setup the activity callback, and retrieve an image file path.
private static final int ACTIVITY_SELECT_IMAGE=3;
First it is good Android programming practice to declare a constant for your activity callbacks. I declare “ACTIVITY_SELECT_IMAGE” as 3 because it is just my third activity callback within my program. The number doesn’t matter at all because you will always refer to an activity by name; just make sure the number is unique.
Intent i = new Intent(Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.INTERNAL_CONTENT_URI); startActivityForResult(i, ACTIVITY_SELECT_IMAGE);
This code will start the image gallery activity.
protected void onActivityResult(int requestCode, int resultCode, Intent intent) {
super.onActivityResult(requestCode, resultCode, intent);
switch(requestCode) {
case ACTIVITY_SELECT_IMAGE:
if(resultCode == RESULT_OK){
Uri selectedImage = intent.getData();
String [] proj={MediaStore.Images.Media.DATA};
cursor = managedQuery(selectedImage,proj,null,null,null);
column_index = cursor.getColumnIndexOrThrow(proj[0]);
cursor.moveToNext();
filePath = cursor.getString(column_index);
System.out.println("image filepath = "+filePath);
When you use startActivityForResult(), a callback occurs in onActivityResult() when the activity returns. You will potentially have many different activities on your app which is why there is a switch statement.
Finally, we want to do something with the image that was chosen. First, retrieve the image URI. Working with an Android URI is like working with a database handle because you can query, modify, add, and delete data. In this example we are interested in retrieving the file path of the image that the user selected. We are querying the URI with a projection string and obtaining the file path to the image in the device.
When you have the file path you could use it to post an image to a URL using the Flickr API.
My first Android tutorial will show you how to easily implement application preferences that persist after the program is closed. The Android platform provides all the necessary functionality to get this working without the need to create a new SQLite database or use a text file.
import android.content.SharedPreferences; public static final String PREFS_NAME = "MyPrefsFile"; public static final String KEY_DEFAULT = "-1"; public static final String KEY_MINI_AUTH_TOKEN = "miniauthtok"; SharedPreferences settings = getSharedPreferences(PREFS_NAME,MODE_PRIVATE); String miniAuthTok = settings.getString(KEY_MINI_AUTH_TOKEN, KEY_DEFAULT
Android has a preference database created already for each application. It stores information as a key/value pair. KEY_DEFAULT defines the return value when no key is found. In our Flickr application, this code is run when the app starts to see if the user has entered their 9 digit Flickr authentication token. If they have not (getString returns -1), I display an activity where they can enter one.
//add the miniTok string to preferences SharedPreferences settings = getSharedPreferences(PREFS_NAME,MODE_PRIVATE); SharedPreferences.Editor editor = settings.edit(); editor.putString(KEY_MINI_AUTH_TOKEN, miniTok); //commit changes editor.commit();
To add key/value pairs to your preferences database, simply create a new SharedPreferences.Editor object and use one of the various put methods. When you are done, make sure to commit your changes just like you would for a database.
Me and my partner Cameron Webb meet last night to brainstorm ideas for our Android project. We had many good ideas but we quickly decided to create a Flickr app that will allow a user to upload and view Flickr pictures. We also came up with rough designs for the applications menus and what features we where going to include. We still need a cool name!
Features: