android canmera相机

MainActivity.java:
package com.canmera.test;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;

public class MainActivity extends Activity {
 private Button button_openCanmera, button_openAlbum, button_gaoQing;
 private ImageView imageView;
 private String imageUrl = "";// 要打开的图片的路径

 @Override
 public void onCreate(Bundle savedInstanceState) {
  super.onCreate(savedInstanceState);
  setContentView(R.layout.activity_main);

  findViews();
  setListeners();
 }

 private void findViews() {
  // TODO Auto-generated method stub
  button_openCanmera = (Button) findViewById(R.id.button1);
  button_openAlbum = (Button) findViewById(R.id.button2);
  button_gaoQing = (Button) findViewById(R.id.button3);
  imageView = (ImageView) findViewById(R.id.imageView1);
 }

 private void setListeners() {
  // TODO Auto-generated method stub
  button_openCanmera.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    // 调用相机
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

    startActivityForResult(intent, 2);
   }
  });
  // 这种方法保存的图片质量好点
  button_gaoQing.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    // 调用相机
    Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
    // 拍完照片之后保存的路径(文件名)
    String path = "/sdcard/myImage/gaoQing.png";

    // 加上这个后,图片就不会被压缩变小了
    intent.putExtra(MediaStore.EXTRA_OUTPUT,
      Uri.fromFile(new File(path)));

    startActivityForResult(intent, 1);
   }
  });
  
  //打开相册
  button_openAlbum.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    // Intent intent = new
    // Intent(MainActivity.this,SDCARD123Activity.class);
    // startActivity(intent);

    Intent intent = new Intent(
      Intent.ACTION_PICK,
      android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    intent.setType("image/*");
    startActivityForResult(intent, 1);
    // startActivityForResult(Intent.createChooser(intent,
    // "Pick any photo"), 1);

   }
  });

  imageView.setOnClickListener(new OnClickListener() {
   @Override
   public void onClick(View v) {
    // TODO Auto-generated method stub
    if("".equals(imageUrl)){
     Intent intent = new Intent(Intent.ACTION_PICK,android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
     intent.setType("image/*");
     startActivityForResult(intent, 1);
    }else{
     Intent intent = new Intent(Intent.ACTION_VIEW);
     intent.setDataAndType(Uri.fromFile(new File(imageUrl)),"image/*");
     startActivity(intent);
    }
   }
  });
 }

 // 相机的回调函数
 @Override
 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  super.onActivityResult(requestCode, resultCode, data);
  if (data != null && data.getExtras() != null) {// 普通的拍照
   saveImage(requestCode, resultCode, data);
   Bitmap bitmap = (Bitmap) data.getExtras().get("data");
   imageView.setImageBitmap(bitmap);
  } else if (data != null && data.getData() != null) {// 打开相册回调函数
   String imagePath = getImagePath(data);
   imageUrl = imagePath;
   imageView.setImageBitmap(BitmapFactory.decodeFile(imagePath, null));
  } else {// 获取高清图片的方法
   imageUrl = "/sdcard/myImage/gaoQing.png";
   imageView.setImageBitmap(BitmapFactory.decodeFile(imageUrl, null));
  }

 }

 // 保存图片到sd卡(保存的图片不清晰)
 private void saveImage(int requestCode, int resultCode, Intent data) {
  // TODO Auto-generated method stub
  Bitmap bitmap = (Bitmap) data.getExtras().get("data");

  String sdStatus = Environment.getExternalStorageState();
  if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) {// 检查sd卡是否可用
   Toast.makeText(this, "SD卡不可用", Toast.LENGTH_LONG).show();
   return;
  }
  new File("/sdcard/myImage/").mkdirs();// 创建文件夹
  OutputStream outputStream = null;
  try {
   String imagePath = "/sdcard/myImage/11.png";
   outputStream = new FileOutputStream(imagePath);
   bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);// 把数据写入文件quality的值0...100

  } catch (FileNotFoundException e) {
   // TODO Auto-generated catch block
   e.printStackTrace();
  } finally {
   try {
    if (outputStream != null) {
     outputStream.flush();
     outputStream.close();
    }
    if (bitmap != null) {
     bitmap.recycle();//清理图片内存
    }
   } catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
   }
  }

 }

 // 返回图片的绝对路径
 private String getImagePath(Intent data) {
  // TODO Auto-generated method stub
  Uri uri = data.getData();// 可以得到图片在Content://。。。中的地址,把它转化成绝对地址如下
  String[] proj = { MediaStore.Images.Media.DATA };
  String imagePath = "";
  Cursor cursor = managedQuery(uri, proj,null, null, null);// 查哪一列
  if (cursor != null) {
   int column_index = cursor
     .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
   if (cursor.getCount() > 0 && cursor.moveToFirst()) {
    imagePath = cursor.getString(column_index);
   }
  }
  return imagePath;
 }

 @Override
 public boolean onCreateOptionsMenu(Menu menu) {
  getMenuInflater().inflate(R.menu.activity_main, menu);
  return true;
 }
}



manifest.xml:
   
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>    
<uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>

 

你可能感兴趣的:(canmera,相机)