RSS구독하기:SUBSCRIBE TO RSS FEED
즐겨찾기추가:ADD FAVORITE
글쓰기:POST
관리자:ADMINISTRATOR

How to launch android camera using intents

Instead of writing your own activity to capture the pictures you probably prefer (in most cases)  to use existent Camera activity which actually have good UI and features. It’s really easy to do, just launch it with Intent like in the code below.

You should be notified that using that approach doesn’t work well on API before 2.0 (at least on G1 with 1.6 it save with pictures with 512*384 resolution). On phones with API 2.0+ it save full-sized picture.  So you could use the “How to use autofocus in Android” as the starting point to create your own Camera application.

//private static final int CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE = 0;
//Uri imageUri;

//define the file-name to save photo taken by Camera activity
String fileName = "new-photo-name.jpg";
//create parameters for Intent with filename
ContentValues values = new ContentValues();
values.put(MediaStore.Images.Media.TITLE, fileName);
values.put(MediaStore.Images.Media.DESCRIPTION,"Image capture by camera");
//imageUri is the current activity attribute, define and save it for later usage 
//(also in onSaveInstanceState)
imageUri = getContentResolver().insert(
		MediaStore.Images.Media.EXTERNAL_CONTENT_URI, values);
//create new Intent
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
intent.putExtra(MediaStore.EXTRA_VIDEO_QUALITY, 1);
startActivityForResult(intent, CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE);


the code above should start the default Camera activity on your phone, now lets define the code to handle results returned by this Intent. Please take a notice that “imageUri” is the activity attribute, define and save it for later usage (also in onSaveInstanceState) 

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (requestCode == CAPTURE_IMAGE_ACTIVITY_REQUEST_CODE) {
    if (resultCode == RESULT_OK) {
        //use imageUri here to access the image

    } else if (resultCode == RESULT_CANCELED) {
        Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
    } else {
        Toast.makeText(this, "Picture was not taken", Toast.LENGTH_SHORT);
    }
}
}
to get the reference to File object from imageUri you can use the following code.

public static File convertImageUriToFile (Uri imageUri, Activity activity)  {
Cursor cursor = null;
try {
    String [] proj={MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID, 
MediaStore.Images.ImageColumns.ORIENTATION};
    cursor = activity.managedQuery( imageUri,
            proj, // Which columns to return
            null,       // WHERE clause; which rows to return (all rows)
            null,       // WHERE clause selection arguments (none)
            null); // Order-by clause (ascending by name)
    int file_ColumnIndex=cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    int orientation_ColumnIndex = 
        cursor.getColumnIndexOrThrow(MediaStore.Images.ImageColumns.ORIENTATION);
    if (cursor.moveToFirst()) {
        String orientation =  cursor.getString(orientation_ColumnIndex);
        return new File(cursor.getString(file_ColumnIndex));
    }
    return null;
} finally {
    if (cursor != null) {
        cursor.close();
    }
}
}

As you see you may get some more information about the image, like orientation, etc, but for things like Thumbnail – you will get null results in most cases, since the thumbnail is not yet created and you should either open Gallery to create the thumbnail or initiate it by calling MediaStore.Images.Thumbnails.getThumbnail (this is the blocking method and was introduced in API-5)

'Android' 카테고리의 다른 글

[안드로이드] 화면 고정  (0) 2010.11.26
[안드로이드]BitmapFactory의 메모리 누수 막는 법  (0) 2010.11.24
[안드로이드]다국어 지원  (2) 2010.11.22

Trackback
Reply
Moss:
Root (119)
Dev (14)
Life (29)
Programming (2)
Music (2)
Android (4)
Tip (2)
Java (11)
Creative (3)
Lyrics (2)
Windows 7 (2)
Etc (7)
C# (8)
Spring (1)
jQuery (2)
Web (4)
Travel (10)
Cook (0)
«   2024/04   »
1 2 3 4 5 6
7 8 9 10 11 12 13
14 15 16 17 18 19 20
21 22 23 24 25 26 27
28 29 30