Android 调用系统相机和图片-轻松带你搞定一切需求

主界面    MainActivity

[html]  view plain  copy
  1. /**  
  2.  * 一共分为 四部分 ,    1.   自带 压缩的照片  
  3.  * 2.   原图大小的图片  
  4.  * 3.   可以   裁剪一部分  
  5.  * 4   .调用 手机相册  
  6.  */  
  7. public class MainActivity extends AppCompatActivity implements View.OnClickListener {  
  8.     private Button mBtn, mBtn2, mBtn3, mBtn4;  
  9.     private ImageView mImg;  
  10.     private final static int REQUEST_THUMBNAIL = 1;//请求略缩图 标识  
  11.     private final static int REQUEST_ORIGINAL = 2;//请求原图标识  
  12.     private static final int CROP_SMALL_PICTURE = 3;//最后都得图片的返回码  
  13.     public final static int CAMERA_REQUEST_CODE = 4;//相机的返回码  
  14.     public final static int ALBUM_REQUEST_CODE = 5;//相册的返回码  
  15.     private String sdPath;//Sd卡路径  
  16.     private String picPath;//图片存放路径  
  17.     protected static Uri tempUri;// 按钮 3 的 拍照存放路径  
  18.     @Override  
  19.     protected void onCreate(Bundle savedInstanceState) {  
  20.         super.onCreate(savedInstanceState);  
  21.         setContentView(R.layout.activity_main);  
  22.         initView();  
  23.     }  
  24.     private void initView() {  
  25.         mBtn = (Button) findViewById(R.id.mBtn);  
  26.         mBtn2 = (Button) findViewById(R.id.mBtn2);  
  27.         mBtn3 = (Button) findViewById(R.id.mBtn3);  
  28.         mBtn4 = (Button) findViewById(R.id.mBtn4);  
  29.         mImg = (ImageView) findViewById(R.id.mImg);  
  30.         mBtn.setOnClickListener(this);  
  31.         mBtn2.setOnClickListener(this);  
  32.         mBtn3.setOnClickListener(this);  
  33.         mBtn4.setOnClickListener(this);  
  34.         sdPath = Environment.getExternalStorageDirectory().getPath() + "/";  
  35.         picPath = sdPath + System.currentTimeMillis() + ".png";  
  36.         Log.e("sdPath", sdPath);  
  37.     }  
  38.   
  39.     @Override  
  40.     public void onClick(View v) {  
  41.         switch (v.getId()) {  
  42.             /**  
  43.              * 第一种  获取压缩图  
  44.              * */  
  45.             case R.id.mBtn:  
  46.                 Intent intent = new Intent();  
  47.                 intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);  
  48.                 //启动相机  
  49.                 startActivityForResult(intent, REQUEST_THUMBNAIL);  
  50.                 break;  
  51.             /**  
  52.              * 第二种  获取原图  
  53.              * */  
  54.             case R.id.mBtn2:  
  55.                 Intent intent2 = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);  
  56.                 Uri uri = Uri.fromFile(new File(picPath));  
  57.                 //为拍摄的图片指定一个存储的路径  
  58.                 intent2.putExtra(MediaStore.EXTRA_OUTPUT, uri);  
  59.                 startActivityForResult(intent2, REQUEST_ORIGINAL);  
  60.                 break;  
  61.             /**  
  62.              * 第三种  获取裁剪之后的照片  
  63.              * */  
  64.             case R.id.mBtn3:  
  65.                 startCamera();  
  66.                 break;  
  67.             /**  
  68.              * 第四种  调用手机相册  
  69.              * */  
  70.             case R.id.mBtn4:  
  71.                 StartXC();  
  72.                 break;  
  73.         }  
  74.     }  
  75.     /**  
  76.      * 返回应用时调用此方法  
  77.      */  
  78.     @Override  
  79.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  80.         if (resultCode == RESULT_OK) {  
  81.             switch (requestCode) {  
  82.                 /**通过这种方法取出的拍摄效果会默认压缩,因为如果相机的像素比较高拍摄出来的图会比较高清  
  83.                  如果图片过大会造成内存溢出(OOM),因此这种方法会默认给图片尽心压缩*/  
  84.                 case REQUEST_THUMBNAIL:  
  85.   
  86.                     Bundle bundle = data.getExtras();  
  87.                     Bitmap bitmap = (Bitmap) bundle.get("data");  
  88.                     mImg.setImageBitmap(bitmap);  
  89.                     break;  
  90.                 /**这种方法是通过内存卡的路径进行读取图片,所以得到的图片是拍摄的原图*/  
  91.                 case REQUEST_ORIGINAL:  
  92.                     FileInputStream fis = null;  
  93.                     try {  
  94.                         //把图片转换成字节流  
  95.                         fis = new FileInputStream(picPath);  
  96.                         //把流转换成图片  
  97.                         Bitmap bitmap1 = BitmapFactory.decodeStream(fis);  
  98.                         mImg.setImageBitmap(bitmap1);  
  99.                         fis = new FileInputStream(sdPath);  
  100.                     } catch (FileNotFoundException e) {  
  101.                         e.printStackTrace();  
  102.                     } finally {  
  103.                         try {  
  104.                             //关闭流  
  105.                             fis.close();  
  106.                         } catch (IOException e) {  
  107.                             e.printStackTrace();  
  108.                         }  
  109.                     }  
  110.                     break;  
  111.                 case CAMERA_REQUEST_CODE:  
  112.                     startPhotoZoom(tempUri); // 开始对相机图片进行裁剪处理  ,里面有返回值的跳转 会进行  CROP_SMALL_PICTURE  
  113.                     break;  
  114.                 case CROP_SMALL_PICTURE:  
  115.                     if (data != null) {  
  116.                         setImageToView(data); // 让刚才选择裁剪得到的图片显示在界面上  
  117.                     }  
  118.                     break;  
  119.                 //相册  
  120.                 case ALBUM_REQUEST_CODE:  
  121.                     startPhotoZoom(data.getData()); // 开始对相册图片进行裁剪处理 ,里面有返回值的跳转 会进行  CROP_SMALL_PICTURE  
  122.                     break;  
  123.             }  
  124.         }  
  125.     }  
  126.     /**  
  127.      * 裁剪图片方法实现  
  128.      *  
  129.      * @param uri  
  130.      */  
  131.     protected void startPhotoZoom(Uri uri) {  
  132.         Intent intent = new Intent("com.android.camera.action.CROP");  
  133.         intent.setDataAndType(uri, "image/*");  
  134.         // 设置裁剪(用intent传值通知来做一些系统操作)  
  135.         intent.putExtra("crop", "true");  
  136.         // aspectX aspectY 是宽高的比例  
  137.         intent.putExtra("aspectX", 1);  
  138.         intent.putExtra("aspectY", 1);  
  139.         // outputX outputY 是裁剪后图片宽高  
  140.         intent.putExtra("outputX", 150);  
  141.         intent.putExtra("outputY", 150);  
  142.         intent.putExtra("return-data", true);  
  143.         startActivityForResult(intent, CROP_SMALL_PICTURE);  
  144.     }  
  145.     /**  
  146.      * 保存裁剪之后的图片数据  
  147.      *  
  148.      * @param  
  149.      * @param  
  150.      */  
  151.     protected void setImageToView(Intent data) {  
  152.         //获取Bundle对象的值  
  153.         Bundle extras = data.getExtras();  
  154.         if (extras != null) {  
  155.             Bitmap photo = extras.getParcelable("data");  
  156.             mImg.setImageBitmap(photo);  
  157.         }  
  158.     }  
  159.     //调用相机的方法  
  160.     public void startCamera() {  
  161.         String state = Environment.getExternalStorageState();  
  162.         if (state.equals(Environment.MEDIA_MOUNTED)) {  
  163.             Intent intent = new Intent();  
  164.             // 指定开启系统相机的Action  
  165.             intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);  
  166.             String out_file_path = sdPath;  
  167.             File dir = new File(out_file_path);  
  168.             if (!dir.exists()) {  
  169.                 dir.mkdirs();  
  170.             }  
  171.             // 把文件地址转换成Uri格式  
  172.             tempUri = Uri.fromFile(new File(picPath));  
  173.             // 设置系统相机拍摄照片完成后图片文件的存放地址  
  174.             intent.putExtra(MediaStore.EXTRA_OUTPUT, tempUri);  
  175.             startActivityForResult(intent, CAMERA_REQUEST_CODE);  
  176.         } else {  
  177.             Toast.makeText(MainActivity.this, "请确认已经插入SD卡", Toast.LENGTH_LONG).show();  
  178.         }  
  179.     }  
  180.     /*  
  181.    * 调用系统相册的方法  
  182.    */  
  183.     public void StartXC() {  
  184.     /*//调用手机里所有图片  
  185.         Intent intent = new Intent(Intent.ACTION_PICK, null);  
  186.         intent.setDataAndType(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, "image/*");  
  187.         intent.setAction(Intent.ACTION_GET_CONTENT);  
  188.         startActivityForResult(intent, ALBUM_REQUEST_CODE);*/  
  189.         //只调用相册的图片  
  190.         Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);  
  191.         startActivityForResult(intent, ALBUM_REQUEST_CODE);  
  192.     }  
  193. }  

布局 activity_main

[html]  view plain  copy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout  
  3.     xmlns:android="http://schemas.android.com/apk/res/android"  
  4.     xmlns:tools="http://schemas.android.com/tools"  
  5.     android:id="@+id/activity_main"  
  6.     android:layout_width="match_parent"  
  7.     android:layout_height="match_parent"  
  8.   
  9.     tools:context="cn.bgs.camera.MainActivity">  
  10.   
  11.     <LinearLayout  
  12.         android:layout_width="match_parent"  
  13.         android:layout_height="match_parent"  
  14.         android:orientation="vertical"  
  15.         >  
  16.   
  17.         <Button  
  18.             android:id="@+id/mBtn"  
  19.   
  20.             android:layout_width="match_parent"  
  21.             android:layout_height="wrap_content"  
  22.             android:text="获取压缩图片"  
  23.             />  
  24.   
  25.         <Button  
  26.             android:id="@+id/mBtn2"  
  27.             android:layout_width="match_parent"  
  28.             android:layout_height="wrap_content"  
  29.             android:text="获取原图"  
  30.             />  
  31.         <Button  
  32.             android:id="@+id/mBtn3"  
  33.             android:layout_width="match_parent"  
  34.             android:layout_height="wrap_content"  
  35.             android:text="获取剪切图片"  
  36.             />  
  37.         <Button  
  38.             android:id="@+id/mBtn4"  
  39.             android:layout_width="match_parent"  
  40.             android:layout_height="wrap_content"  
  41.             android:text="调用手机相册"  
  42.             />  
  43.   
  44.   
  45.         <TextView  
  46.             android:layout_width="wrap_content"  
  47.             android:layout_height="wrap_content"  
  48.             android:layout_gravity="center_horizontal"  
  49.             android:text="图片"  
  50.             />  
  51.   
  52.         <ImageView  
  53.             android:id="@+id/mImg"  
  54.             android:layout_width="match_parent"  
  55.             android:layout_height="match_parent"  
  56.   
  57.             />  
  58.   
  59.     </LinearLayout>  
  60.   
  61. </RelativeLayout>  

配置清单  AndroidMainifest

[html]  view plain  copy
  1.      <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"></uses-permission>  
  2.     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"></uses-permission>  
  3.     <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>

猜你喜欢

转载自blog.csdn.net/qq_36488374/article/details/80735768