Android之如何打开系统相机

为什么要打开系统相机呢?

因为现在的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();
			}
		}

(2)打开系统相机--代码如下:

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>

效果图:




        

  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值