`
dadi520
  • 浏览: 139509 次
  • 性别: Icon_minigender_1
  • 来自: 重庆
社区版块
存档分类
最新评论

Android 调用intent 拍照(转)

 
阅读更多

原文: http://achorniy.wordpress.com/2010/04/26/howto-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.

//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 callingMediaStore.Images.Thumbnails.getThumbnail (this is the blocking method and was introduced in API-5)

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics