说道我们的智能手机,相机是必不可少的组件了,今天我们就以一个简单地例子来介绍我们的相机操作,直接上干货
1.首先打开相机,我们需要一定的权限,所以首先我们要声明一下权限.
2.下面我们进行了一下简单的布局:
<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"
tools:context="com.android.caremadck.MainActivity" >
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="0dp"
android:layout_weight="1"
android:scaleType="fitXY"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="horizontal">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/ss"
android:layout_weight="1"
android:id="@+id/buttonGet"/>
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:text="@string/cc"
android:layout_weight="1"
android:id="@+id/buttonForm"/>
</LinearLayout>
</LinearLayout>
3.下面是主程序的代码
package com.android.caremadck;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Bundle;
import android.view.Menu;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
//注意全局变量与局部变量的区别
private Button buttonGet;
private Button buttonFrom;
private ImageView imageView;
private final int OPEN_REQUES=1;
private final int GET_REQUES=2;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
buttonGet=(Button)findViewById(R.id.buttonGet);
buttonFrom=(Button)findViewById(R.id.buttonForm);
imageView=(ImageView)findViewById(R.id.imageView);
buttonGet.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(android.provider.
MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent,OPEN_REQUES);
}
});
buttonFrom.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
// TODO Auto-generated method stub
Intent intent=new Intent(Intent.ACTION_PICK,
android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(intent,GET_REQUES);
}
});
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
// TODO Auto-generated method stub
super.onActivityResult(requestCode, resultCode, data);
switch (requestCode) {
case OPEN_REQUES:{
if(resultCode==RESULT_OK){
Bundle bundle=data.getExtras();
Bitmap bitmap=(Bitmap) bundle.get("data");
imageView.setImageBitmap(bitmap);
}
break;
}
case GET_REQUES:{
if(resultCode==RESULT_OK){
Uri uri=data.getData();
imageView.setImageURI(uri);
}
break;
}
default:
break;
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
该demo可以实现两个功能,可以拍照片,也可以从相册里面进行选取
最后将其显示到屏幕上
4.下面是运行结果