原文: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 activityString fileName = "new-photo-name.jpg";//create parameters for Intent with filenameContentValues 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 IntentIntent 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)

更多相关文章

  1. Android Fresco图片处理库用法API英文原文文档3(Facebook开源Andr
  2. OpenGL ES教程I之创建OpenGL视图(原文对照)
  3. OpenGL ES教程V之更多3D模型(原文对照)
  4. 【Android开发学习43】OpenGL ES教程VI之纹理贴图(原文对照)
  5. OpenGL ES教程II之创建多边形(原文对照)
  6. OpenGL ES教程IV之着色(原文对照)
  7. OpenGL ES教程III之移动变换(原文对照)
  8. OpenGL ES教程VI之纹理贴图(原文对照)

随机推荐

  1. 开箱即用!Android四款系统架构工具
  2. Android对不同DPI的dimen选择优先级问题
  3. Android AIDL服务学习笔记
  4. 福利来了!带你“爱上Android”
  5. selector背景以及android的透明色
  6. Android启动过程以及各个镜像的关系
  7. 如何移植ROS程序到Android
  8. Android第三章 (UI)
  9. android studio gradle 多渠道打包之完全
  10. Android Animation浅析