Android使用Intent调用摄像头并获取照片
来源:互联网 发布:js和ajax点赞功能代码 编辑:程序博客网 时间:2024/06/02 00:01
使用Android的Intent调用另外一个activity的时候,采用的是异步方式。startActivityForResult之后被调用activity并没有马上返回结果给调用activity,Android的Acitivity对象中startActivityForResult的源代码中有相关的解释。
可以重载activity的onActivityResult() 方法,完成事件的处理。
/**
* 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
}
});
//处理返回的结果,requestCode,resultCode判定是哪个传回的结果,data记录了结果数据
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开发精要》有非常全面详细的介绍
- Android使用Intent调用摄像头并获取照片
- android 调用CAMERA并获取照片
- Android使用摄像头,分别获取照片的缩略图与原图
- android调用照相机拍照获取照片并做简单剪裁
- android调用照相机拍照获取照片并做简单剪裁
- android调用照相机拍照获取照片并做简单剪裁
- android 实现调用相机拍照 获取照片路径并上传
- android调用照相机拍照获取照片并做简单剪裁
- android调用照相机拍照获取照片并…
- android调用照相机拍照获取照片并…
- Android客户端之“微服私访”App的系统学习(八)调用系统摄像头拍照并管理照片并使用Okhttp上传文件至后台
- android调用摄像头拍照,从相册中选择照片并裁剪
- Android 获取摄像头拍摄的照片
- 使用已有的摄像头程序获取照片
- Android使用相机获取照片并显示的代码
- java 如何调用摄像头并拍照保存照片
- Android Camera学习总结:如何在android中使用摄像头获取照片
- IOS调用摄像头或照片库获取资源
- 有负边路径的话用SPFA算法
- EL表达式的详细用法
- C++ STL编程轻松入门
- Python进阶10-标准库介绍01
- linux 网络编程---->多路复用:select实例!
- Android使用Intent调用摄像头并获取照片
- Python进阶11-标准库介绍02
- java冒泡算法
- [BZOJ4094] [Usaco2013 Dec]Optimal Milking
- Linux网络编程简单示例
- 显示和隐藏菜单的做法
- android 学习
- no android sdk found
- Floyd、Dijkstra、Bellman-Ford、SPFA的比较