原文: 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)
相关推荐
Android的Intent方式拍照源代码,具体文章请参考:http://blog.csdn.net/gobitan/article/details/9158825
该资源是一个完整的Android应用,演示了如何在Android中利用Intent进行拍照、摄像、打电话、发短信、发邮件等
应开发需要,不想调用第三方库实现功能。调用系统相机拍照,系统相册进行多选。注意多选照片时要长按一张图片变为多选。
主要介绍了Android 通过Intent调用系统拍照程序出现图片太小的问题解决办法的相关资料,需要的朋友可以参考下
添加本地图片和调用系统拍照图片主要是通过调用acitivity跳转startActivityForResult Intent intent int requestCode 方法和activity返回结果onActivityResult int requestCode int resultCode Intent data 方法来...
android 通过Intent意图实现系统调用摄像机拍照的 Demo,并且实现获取路径,将拍摄照片display出来。
在Android中,调用系统相机拍照时,将会接收到返回的图像数据,但是这些图片并不是全尺寸的图像,而是系统给的缩略图,当对拍照的图片进行裁切后显示时,得到的却是模糊的图片。下面针对这个问题提出解决的方法。 ...
一般而言,在需要用到摄像头拍照或摄像的时候,均会直接调用系统现有的相机应用,去进行拍照或摄像,我们只取它拍摄的结果进行处理,这样避免了不同设备的摄像头的一些细节问题。本篇博客将介绍在Android应用中,...
首先上一段调用android相册和相机的代码: 代码如下: Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用android自带的照相机 photoUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; ...
android 安卓 系统 调用相机拍照
对于拍摄照片我们可以直接调用系统自带的相机拍照,一般情况下无需我们自己开发相机拍照。 1、当点击按钮后我们可以通过Intent意图启动系统相机 @Override public void onClick(View v) { Intent intent=new ...
本文主要介绍 Android 调用摄像头拍照并对照片进行裁剪和压缩,文中给出了主要步骤和关键代码。 调用摄像头拍照,对拍摄照片进行裁剪,代码如下。 /** * 调用摄像头拍照,对拍摄照片进行裁剪 */ private void ...
1、调用系统拍照 Intent intent = new Intent(android.media.action.IMAGE_CAPTURE); //保存到指定目录 File file = new File(/mnt/sdcard/picture); if (!file.exists()) { file.mkdirs(); } File out =...
本例子通过一个Intent打开系统的自带照相机照相,并把图片显示出来
本文实例讲述了Android调用手机拍照功能的方法。分享给大家供大家参考。具体如下: 一、main.xml布局文件: <?xml version=1.0 encoding=utf-8?> <LinearLayout xmlns:android=...
本文实例为大家分享了Intent如何实现一个简单的记事本功能的演示过程,供大家参考,具体内容如下 1、运行截图 单击右上角【…】会弹出【添加】菜单项,长按某条记录会弹出快捷菜单【删除】项。 2、主要设计步骤 (1...
首先创建一个文件,用于保存拍照图像,然后根据不同系统版本获取Uri,传递给Intent,然后调起相机(可以考虑将outputImage、imageUri设置为全局变量)。 3、处理回调 使用BitmapFactory读取imageUri,得到bitmap,...
本文实现android系统照相机的调用来拍照 项目的布局相当简单,只有一个Button: <RelativeLayout xmlns:android=http://schemas.android.com/apk/res/android xmlns:tools=http://schemas.android.com/tools ...
Android中实现拍照有两种方法,一种是调用系统自带的相机,然后使用其返回的照片数据。 还有一种是自己用Camera类和其他相关类实现相机功能,这种方法定制度比较高,洗染也比较复杂,一般平常的应用只需使用第一种...