【安卓知识点汇总】拍照/访问系统相册小Demo

系统已经有的东西,如果我们没有新的需求的话,直接调用是最直接的。下面讲讲调用系统相机拍照并保存图片和如何调用系统相册的方法。

1.首先看看调用系统相机的核心方法:

Intent camera = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(camera, CAMERA);


相机返回的数据通过下面的回调方法取得,并处理:

@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  if(requestCode == CAMERA && resultCode == Activity.RESULT_OK && null != data){
   String sdState=Environment.getExternalStorageState();
   if(!sdState.equals(Environment.MEDIA_MOUNTED)){
    GameLog.log(Tag, "sd card unmount");
    return;
   }
   new DateFormat();
   String name= DateFormat.format("yyyyMMdd_hhmmss", Calendar.getInstance(Locale.CHINA))+".jpg";
   Bundle bundle = data.getExtras();
   //获取相机返回的数据,并转换为图片格式
   Bitmap bitmap = (Bitmap)bundle.get("data");
   FileOutputStream fout = null;
   File file = new File("/sdcard/pintu/");
   file.mkdirs();
   String filename=file.getPath()+name;
   try {
    fout = new FileOutputStream(filename);
    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, fout);
   } catch (FileNotFoundException e) {
    e.printStackTrace();
   }finally{
    try {
     fout.flush();
     fout.close();
    } catch (IOException e) {
     e.printStackTrace();
    }
   }
   //显示图片
   
  }
}



2.下面是调用系统相册并取得照片的方法:

Intent picture = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
startActivityForResult(picture, PICTURE);


调用示例:


@Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  super.onActivityResult(requestCode, resultCode, data);
  
  if(requestCode == CAMERA && resultCode == Activity.RESULT_OK && null != data){
  
   Uri selectedImage = data.getData();
   String[] filePathColumns={MediaStore.Images.Media.DATA};
   Cursor c = this.getContentResolver().query(selectedImage, filePathColumns, null,null, null);
   c.moveToFirst();
   int columnIndex = c.getColumnIndex(filePathColumns[0]);
   String picturePath= c.getString(columnIndex);
   c.close();
   //获取图片并显示
   
  }




----------->                     以下为调用实现demo                 < ------------------------


public class MainActivity extends Activity {
	private Button button;
	private Button button2;
	private ImageView imageView;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		button = (Button) this.findViewById(R.id.button1);
		button2 = (Button) this.findViewById(R.id.button2);
		imageView = (ImageView) this.findViewById(R.id.imageView1);
		button.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(
						android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
				startActivityForResult(intent, 1000);
			}
		});

		button2.setOnClickListener(new View.OnClickListener() {
			@Override
			public void onClick(View v) {
				// TODO Auto-generated method stub
				Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
				intent.setType("image/*");
				intent.putExtra("crop", true);
				intent.putExtra("return-data", true);
				startActivityForResult(intent, 1001);
			}
		});
	}

	@Override
	protected void onActivityResult(int requestCode, int resultCode, Intent data) {
		// TODO Auto-generated method stub
		super.onActivityResult(requestCode, resultCode, data);
		// 从拍照的手机中获得照片
		if (requestCode == 1000 && resultCode == RESULT_OK) {
			Bundle bundle = data.getExtras();
			Bitmap bm = (Bitmap) bundle.get("data");
			imageView.setImageBitmap(bm);
		} else if (requestCode == 1001 && resultCode == RESULT_OK) {
			Uri uri = data.getData();
			ContentResolver resolver = getContentResolver();
			try {
				Bitmap bitmap = BitmapFactory.decodeStream(resolver
						.openInputStream(uri));
				imageView.setImageBitmap(bitmap);
			} catch (Exception e) {
				// TODO: handle exception
				e.printStackTrace();
			}
		}
	}

	@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;
	}

}

布局文件很简单,如下:


<RelativeLayout 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"
    tools:context=".MainActivity" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentTop="true"
        android:layout_centerHorizontal="true"
        android:src="@drawable/ic_launcher" />

    <Button
        android:id="@+id/button2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignTop="@+id/button1"
        android:layout_centerHorizontal="true"
        android:layout_marginTop="55dp"
        android:text="浏览本地图片" />

    <Button
        android:id="@+id/button1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="56dp"
        android:text="打开照相机" />

</RelativeLayout>


知识参考来自网络,整合之,有什么问题可以联系我。蟹蟹。


---------------------------系统中的直接调用与映射调用,看具体情况---------------------------------------------




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值