赞
踩
为什么要打开系统相机呢?
因为现在的APP差不多都要求打开系统相机来拍照,这个需求还是蛮广的~所以就有了调用系统相机这一个功能~
如何打开系统相机呢?
步骤如下:
1.先加权限吧~
- <span style="font-size:24px;"><uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.MEDIA_CONTENT_CONTROL"/></span>
2.写了俩个不同的打开相机的方法:
(1)传回原图--代码如下:
- public void btn(View view){
- Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- Uri uri=Uri.fromFile(new File(path));
- intent.putExtra(MediaStore.EXTRA_OUTPUT, uri);
- startActivityForResult(intent, REQUESTCODE2);
- }
path写个绝对路径:
private String path=Environment.getExternalStorageDirectory().getAbsolutePath()+"/hehe.png";
这个路径是可以把图保存下来的~
并且把图展现出来~因为拍照了不可能不把图弄出来吧~在MainActivity中重写的onActivityResult方法中:
- if(requestCode==REQUESTCODE2&&resultCode==RESULT_OK){
- try {
- FileInputStream fileInputStream=new FileInputStream(path);
- Bitmap bitmap=BitmapFactory.decodeStream(fileInputStream);
- iv.setImageBitmap(bitmap);
- } catch (FileNotFoundException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- public void startCamera(View view) {
- // TODO Auto-generated method stub
- Intent intent=new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- //startActivity(intent);
- startActivityForResult(intent, REQUESTCODE);
- }
- if(requestCode==REQUESTCODE&&resultCode==RESULT_OK){
- Bundle bundle=data.getExtras();
- Bitmap bitmap=(Bitmap) bundle.get("data");
- iv.setImageBitmap(bitmap);
- }
(3)布局:
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical">
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:onClick="btn"
- android:text="传回原图"/>
-
- <Button
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="打开系统相机"
- android:onClick="startCamera" />
- <ImageView
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:id="@+id/iv"/>
-
-
- </LinearLayout>
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。