Android使用Intent调用摄像头并获取照片

使用Android的Intent调用另外一个activity的时候,采用的是多线程机制,异步方式。startActivityForResult之后被调用activity并没有马上返回结果给调用activity,Android的Acitivity对象中startActivityForResult的源代码中有相关的解释。

/**

* Launch an activity for which you would like a result?????? when it finished.??????

* When this activity exits, your

     * onActivityResult() method will be called with the given requestCode.

* Using a negative requestCode is the same as calling

* {@link #startActivity} (the activity is not launched as a sub-activity).

*

* <p>Note that this method should only be used with Intent protocols

* that are defined to return a result.  In other protocols (such as

* {@link Intent#ACTION_MAIN} or {@link Intent#ACTION_VIEW}), you may

* not get the result when you expect.  For example, if the activity you

* are launching uses the singleTask launch mode, it will not run in your

* task and thus you will immediately receive a cancel result.

*

* <p>As a special case, if you call startActivityForResult() with a requestCode

* >= 0 during the initial onCreate(Bundle savedInstanceState)/onResume() of your

* activity, then your window will not be displayed until a result is

* returned back from the started activity.  This is to avoid visible

* flickering when redirecting to another activity.

*

* <p>This method throws {@link android.content.ActivityNotFoundException}

* if there was no Activity found to run the given Intent.

*

* @param intent The intent to start.

* @param requestCode If >= 0, this code will be returned in

*                    onActivityResult() when the activity exits.

* @param options Additional options for how the Activity should be started.

* See {@link android.content.Context#startActivity(Intent, Bundle)

* Context.startActivity(Intent, Bundle)} for more details.

*

* @throws android.content.ActivityNotFoundException

*

* @see #startActivity

*/

public void startActivityForResult(Intent intent, int requestCode, @Nullable Bundle options)

》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》》

Button pButton = (Button) findViewById(R.id.btn_return);

pButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {

// TODO Auto-generated method stub

Intent pIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用摄像头action

startActivityForResult(pIntent,INTENT_CODE_IMAGE_CAPTURE);//requestcode

//startActivityForResult如果就马上获取intent对象的结果中很多成员是null

}

});

protected void onActivityResult(int requestCode, int resultCode, Intent data) {

// TODO Auto-generated method stub

super.onActivityResult(requestCode, resultCode, data);

if (requestCode==INTENT_CODE_IMAGE_CAPTURE && data != null) {

final ImageView pImageView =(ImageView)findViewById(R.id.imageview1);

Bundle pBundle = data.getExtras(); //从intent对象中获取数据,

if (pBundle != null) {

Bitmap pBitmap = (Bitmap) pBundle.get("data");

if (pBitmap !=null) {

pImageView.setImageBitmap(pBitmap);

pImageView.refreshDrawableState();

Log.i("Result", "capture picture succeed");

}

else {

Log.i("Result", "capture picture failure");

}

}

}

else if (requestCode == 0) {

Toast.makeText(this, "te", Toast.LENGTH_LONG).show();

Log.i("other", "result");

}

}

关于Android的intent机制可参考:

http://www.oschina.net/question/565065_67909

《Android开发精要》有非常全面详细的介绍

时间: 2024-11-10 11:23:36

Android使用Intent调用摄像头并获取照片的相关文章

Android 通过Intent调用系统功能和Action动作和服务广播【大全】

1.从google搜索内容 Intent intent = new Intent(); intent.setAction(Intent.ACTION_WEB_SEARCH);intent.putExtra(SearchManager.QUERY,"searchString")startActivity(intent); 2.浏览网页 Uri uri = Uri.parse("http://www.baidu.com");Intent it = new Intent(

Android实例-从照相机或图库获取照片(XE8+小米2)

结果: 1.如果要取本地相删除的话,小米手机要注意一下,不能取网络相册. 操作: 1.两个 TButton (Button1 和 Button2) , 一个 TActionList(ActionList1) ,一个 TImage(Image1). 2.Button1 的 stylelookup 选 择 cameratoolbutton , Button1 的 stylelookup 选择organizetoolbutton. 3.双击 ActionList1,在弹出的对话框中点击右键菜单中的ne

麦子学院Android实战调用摄像头代码分享

Android如何实现调用摄像头?在安卓APP开发的过程中,经常会需要调用手机自身摄像头拍照的代码,那么android调用摄像头的代码是什么呢?现在麦子学院android开发老师告诉大家,android调用摄像头这个方法十分的简单实用,有需要的小伙伴可以参考下. 应用场景: 在Android开发过程中,有时需要调用手机自身设备的功能,本文侧重摄像头拍照功能的调用. 知识点介绍: 使用权限:调用手机自身设备功能(摄像头拍照功能),应该确保已经在AndroidManifest.xml中正确声明了对摄

Android入门(十六)调用摄像头相册

原文链接:http://www.orlion.ga/665/ 一.调用摄像头 创建一个项目ChoosePicDemo,修改activity_main.xml: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent" android:layout_height="match_parent&qu

android: 调用摄像头拍照

很多应用程序都可能会使用到调用摄像头拍照的功能,比如说程序里需要上传一张图片 作为用户的头像,这时打开摄像头拍张照是最简单快捷的.下面就让我们通过一个例子来学 习一下,如何才能在应用程序里调用手机的摄像头进行拍照. 新建一个 ChoosePicTest 项目,然后修改 activity_main.xml 中的代码,如下所示: <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" andro

调用摄像头进行拍照获取图片

这里为了得到清晰的图像,不适用返回的data,因为这个得到的是缩略图.这里的做法是在调用摄像头之前指定图片的保存位置,然后通过调用结果函数和指定的图片存储路径,从文件中获取图片. 启动摄像头的代码: 1 intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); 2 File file = new File(Environment.getExternalStorageDirectory() 3 + "/tempImage.jpg"); 4

android调用摄像头拍照

调用手机摄像头拍照,获取拍照后的图片数据.以下代码是在activity中:     // 调用摄像头         Button b = (Button) findViewById(R.id.btn1);     b.setOnClickListener(new View.OnClickListener() {         @Override         public void onClick(View v) {             // Here we fire the inte

android 调用系统相机获取图片、调用系统相册获取图片,并对图片进行截取

打开系统相册获取图片并截取,代码相对简单 1 Intent intent = new Intent(Intent.ACTION_GET_CONTENT,null); 2 intent.setType("image/*"); 3 intent.putExtra("crop", "true"); 4 5 //WIDTH 和 HEIGHT指的是截取框的宽高比例,如设WIDTH = 1,HEIGHT = 1,截取框就为正方形 6 intent.putEx

Unity 3D 调用摄像头捕获照片 录像

1,要想调用摄像头首先要打开摄像头驱动,如果用户允许则可以使用. 2,定义WebCamTexture的变量用于捕获单张照片. 3,连续捕获须启用线程. 实现代码: using UnityEngine; using System.Collections; using System.IO; using System.Runtime.Serialization; using System.Runtime .Serialization.Formatters.Binary; using System.Th