主界面 MainActivity
- /**
- * 一共分为 四部分 , 1. 自带 压缩的照片
- * 2. 原图大小的图片
- * 3. 可以 裁剪一部分
- * 4 .调用 手机相册
- */
- public class MainActivity extends AppCompatActivity implements View.OnClickListener {
- private Button mBtn, mBtn2, mBtn3, mBtn4;
- private ImageView mImg;
- private final static int REQUEST_THUMBNAIL = 1;//请求略缩图 标识
- private final static int REQUEST_ORIGINAL = 2;//请求原图标识
- private static final int CROP_SMALL_PICTURE = 3;//最后都得图片的返回码
- public final static int CAMERA_REQUEST_CODE = 4;//相机的返回码
- public final static int ALBUM_REQUEST_CODE = 5;//相册的返回码
- private String sdPath;//Sd卡路径
- private String picPath;//图片存放路径
- protected static Uri tempUri;// 按钮 3 的 拍照存放路径
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
- initView();
- }
- private void initView() {
- mBtn = (Button) findViewById(R.id.mBtn);
- mBtn2 = (Button) findViewById(R.id.mBtn2);
- mBtn3 = (Button) findViewById(R.id.mBtn3);
- mBtn4 = (Button) findViewById(R.id.mBtn4);
- mImg = (ImageView) findViewById(R.id.mImg);
- mBtn.setOnClickListener(this);
- mBtn2.setOnClickListener(this);
- mBtn3.setOnClickListener(this);
- mBtn4.setOnClickListener(this);
- sdPath = Environment.getExternalStorageDirectory().getPath() + "/";
- picPath = sdPath + System.currentTimeMillis() + ".png";
- Log.e("sdPath", sdPath);
- }
- @Override
- public void onClick(View v) {
- switch (v.getId()) {
- /**
- * 第一种 获取压缩图
- * */
- case R.id.mBtn:
- Intent intent = new Intent();
- intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
- //启动相机
- startActivityForResult(intent, REQUEST_THUMBNAIL);
- break;
- /**
- * 第二种 获取原图
- * */
- case R.id.mBtn2:
- Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
- Uri uri = Uri.fromFile(new File(picPath));
- //为拍摄的图片指定一个存储的路径
- intent2.putExtra(MediaStore.EXTRA_OUTPUT, uri);
- startActivityForResult(intent2, REQUEST_ORIGINAL);
- break;
- /**
- * 第三种 获取裁剪之后的照片
- * */
- case R.id.mBtn3:
- startCamera();
- break;
- /**
- * 第四种 调用手机相册
- * */
- case R.id.mBtn4:
- StartXC();
- break;
- }
- }
- /**
- * 返回应用时调用此方法
- */
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (resultCode == RESULT_OK) {
- switch (requestCode) {
- /**通过这种方法取出的拍摄效果会默认压缩,因为如果相机的像素比较高拍摄出来的图会比较高清
- 如果图片过大会造成内存溢出(OOM),因此这种方法会默认给图片尽心压缩*/
- case REQUEST_THUMBNAIL:
- Bundle bundle = data.getExtras();
- Bitmap bitmap = (Bitmap) bundle.get("data");
- mImg.setImageBitmap(bitmap);
- break;
- /**这种方法是通过内存卡的路径进行读取图片,所以得到的图片是拍摄的原图*/
- case REQUEST_ORIGINAL:
- FileInputStream fis = null;
- try {
- //把图片转换成字节流
- fis = new FileInputStream(picPath);
- //把流转换成图片
- Bitmap bitmap1 = BitmapFactory.decodeStream(fis);
- mImg.setImageBitmap(bitmap1);
- fis = new FileInputStream(sdPath);
- } catch (FileNotFoundException e) {
- e.printStackTrace();
- } finally {
- try {
- //关闭流
- fis.close();
- } catch (IOException e) {
- e.printStackTrace();
- }
- }
- break;
- case CAMERA_REQUEST_CODE:
- startPhotoZoom(tempUri); // 开始对相机图片进行裁剪处理 ,里面有返回值的跳转 会进行 CROP_SMALL_PICTURE
- break;
- case CROP_SMALL_PICTURE:
- if (data != null) {
- setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上
- }
- break;
- //相册
- case ALBUM_REQUEST_CODE:
- startPhotoZoom(data.getData()); // 开始对相册图片进行裁剪处理 ,里面有返回值的跳转 会进行 CROP_SMALL_PICTURE
- break;
- }
- }
- }
- /**
- * 裁剪图片方法实现
- *
- * @param uri
- */
- protected void startPhotoZoom(Uri uri) {
- Intent intent = new Intent("com.android.camera.action.CROP");
- intent.setDataAndType(uri, "image/*");
- // 设置裁剪(用intent传值通知来做一些系统操作)
- intent.putExtra("crop", "true");
- // aspectX aspectY 是宽高的比例
- intent.putExtra("aspectX", 1);
- intent.putExtra("aspectY", 1);
- // outputX outputY 是裁剪后图片宽高
- intent.putExtra("outputX", 150);
- intent.putExtra("outputY", 150);
- intent.putExtra("return-data", true);
- startActivityForResult(intent, CROP_SMALL_PICTURE);
- }
- /**
- * 保存裁剪之后的图片数据
- *
- * @param
- * @param
- */
- protected void setImageToView(Intent data) {
- //获取Bundle对象的值
- Bundle extras = data.getExtras();
- if (extras != null) {
- Bitmap photo = extras.getParcelable("data");
- mImg.setImageBitmap(photo);
- }
- }
- //调用相机的方法
- public void startCamera() {
- String state = Environment.getExternalStorageState();
- if (state.equals(Environment.MEDIA_MOUNTED)) {
- Intent intent = new Intent();
- // 指定开启系统相机的Action
- intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
- String out_file_path = sdPath;
- File dir = new File(out_file_path);
- if (!dir.exists()) {
- dir.mkdirs();
- }
- // 把文件地址转换成Uri格式
- tempUri = Uri.fromFile(new File(picPath));
- // 设置系统相机拍摄照片完成后图片文件的存放地址
- intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);
- startActivityForResult(intent, CAMERA_REQUEST_CODE);
- } else {
- Toast.makeText(MainActivity.this, "请确认已经插入SD卡", Toast.LENGTH_LONG).show();
- }
- }
- /*
- * 调用系统相册的方法
- */
- public void StartXC() {
- /*//调用手机里所有图片
- Intent intent = new Intent(Intent.ACTION_PICK, null);
- intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");
- intent.setAction(Intent.ACTION_GET_CONTENT);
- startActivityForResult(intent, ALBUM_REQUEST_CODE);*/
- //只调用相册的图片
- Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
- startActivityForResult(intent, ALBUM_REQUEST_CODE);
- }
- }
布局 activity_main
- <?xml version="1.0" encoding="utf-8"?>
- <RelativeLayout
- xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:tools="http://schemas.android.com/tools"
- android:id="@+id/activity_main"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- tools:context="cn.bgs.camera.MainActivity">
- <LinearLayout
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:orientation="vertical"
- >
- <Button
- android:id="@+id/mBtn"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="获取压缩图片"
- />
- <Button
- android:id="@+id/mBtn2"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="获取原图"
- />
- <Button
- android:id="@+id/mBtn3"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="获取剪切图片"
- />
- <Button
- android:id="@+id/mBtn4"
- android:layout_width="match_parent"
- android:layout_height="wrap_content"
- android:text="调用手机相册"
- />
- <TextView
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center_horizontal"
- android:text="图片"
- />
- <ImageView
- android:id="@+id/mImg"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- />
- </LinearLayout>
- </RelativeLayout>
配置清单 AndroidMainifest
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>
- <uses-permission android:name="android.permission.CAMERA"></uses-permission>
<!-- 具有相机功能 --> <intent-filter > <action android:name="android.media.action.IMAGE_CAPTURE"/> <category android:name="android.intent.category.DEFAULT" /> </intent-filter>