Android开发之系统相机相册使用

获取图片后进行处理,对分辨率较大的进行缩放,较小的原图显示

1、调用系统相机

[java]  view plain copy print ?
  1. Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 调用系统相机  
  2. new DateFormat();  
  3. name = DateFormat.format("yyyyMMdd_hhmmss",  
  4.                 Calendar.getInstance(Locale.CHINA))  
  5.                 + ".jpg";  
  6. Uri imageUri = Uri.fromFile(new File(PATH, name));  
  7.   
  8. intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);//把照片保存在sd卡中指定位置。  
  9.   
  10. startActivityForResult(intent, CAMERA_TAKE);  

2、调用系统相册

[java]  view plain copy print ?
  1. Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
  2. intent.setType("image/*");  
  3. startActivityForResult(intent, CAMERA_SELECT);  

3、在onActivityResult方法处理得到的图片
看源码吧。。。

首先简单的布局文件
[java]  view plain copy print ?
  1. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  2.     xmlns:tools="http://schemas.android.com/tools"  
  3.     android:layout_width="match_parent"  
  4.     android:layout_height="match_parent"  
  5.     android:orientation="vertical" >  
  6.   
  7.     <Button  
  8.         android:id="@+id/btn_take"  
  9.         android:layout_width="match_parent"  
  10.         android:layout_height="wrap_content"  
  11.         android:text="@string/take" />  
  12.   
  13.     <Button  
  14.         android:id="@+id/btn_select"  
  15.         android:layout_width="match_parent"  
  16.         android:layout_height="wrap_content"  
  17.         android:text="@string/selectFrom" />  
  18.   
  19.     <ScrollView  
  20.         android:id="@+id/sv_show"  
  21.         android:layout_width="match_parent"  
  22.         android:layout_height="match_parent" >  
  23.   
  24.         <LinearLayout  
  25.             android:id="@+id/ll_show"  
  26.             android:layout_width="match_parent"  
  27.             android:layout_height="match_parent"  
  28.             android:orientation="vertical" >  
  29.         </LinearLayout>  
  30.     </ScrollView>  
  31.   
  32. </LinearLayout>  

源码
[java]  view plain copy print ?
  1. public class MainActivity extends Activity {  
  2.   
  3.     private Button btn_take, btn_select;  
  4.     private LinearLayout ll_show;  
  5.   
  6.     // 请求  
  7.     private static final int CAMERA_TAKE = 1;  
  8.     private static final int CAMERA_SELECT = 2;  
  9.   
  10.     // 图片名  
  11.     public String name;  
  12.   
  13.     // 存储路径  
  14.     private static final String PATH = Environment  
  15.             .getExternalStorageDirectory() + "/DCIM";  
  16.   
  17.     private boolean isBig = false;  
  18.   
  19.   
  20.     @Override  
  21.     protected void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.activity_main);  
  24.   
  25.         btn_take = (Button) findViewById(R.id.btn_take);  
  26.         btn_select = (Button) findViewById(R.id.btn_select);  
  27.         ll_show = (LinearLayout) findViewById(R.id.ll_show);  
  28.   
  29.         btn_take.setOnClickListener(new OnClickListener() {  
  30.   
  31.             @Override  
  32.             public void onClick(View v) {  
  33.                 takePhoto();  
  34.             }  
  35.         });  
  36.   
  37.         btn_select.setOnClickListener(new OnClickListener() {  
  38.   
  39.             @Override  
  40.             public void onClick(View v) {  
  41.                 Intent intent = new Intent(Intent.ACTION_GET_CONTENT);  
  42.                 intent.setType("image/*");  
  43.                 startActivityForResult(intent, CAMERA_SELECT);  
  44.             }  
  45.         });  
  46.   
  47.     }  
  48.   
  49.     public void takePhoto() {  
  50.         Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);// 调用系统相机  
  51.         new DateFormat();  
  52.         name = DateFormat.format("yyyyMMdd_hhmmss",  
  53.                 Calendar.getInstance(Locale.CHINA))  
  54.                 + ".jpg";  
  55.         Uri imageUri = Uri.fromFile(new File(PATH, name));  
  56.   
  57.         intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);  
  58.   
  59.         startActivityForResult(intent, CAMERA_TAKE);  
  60.     }  
  61.   
  62.     @Override  
  63.     protected void onActivityResult(int requestCode, int resultCode, Intent data) {  
  64.         super.onActivityResult(requestCode, resultCode, data);  
  65.   
  66.         if (resultCode == RESULT_OK) {  
  67.             switch (requestCode) {  
  68.             case CAMERA_TAKE:  
  69.                 Bitmap bitmap = BitmapFactory.decodeFile(PATH + "/" + name);  
  70.                 Toast.makeText(this, name, Toast.LENGTH_LONG).show();  
  71.                 System.out.println(bitmap.getHeight() + "======"  
  72.                         + bitmap.getWidth());  
  73.   
  74.                 // 获取屏幕分辨率  
  75.                 DisplayMetrics dm = new DisplayMetrics();  
  76.                 getWindowManager().getDefaultDisplay().getMetrics(dm);  
  77.   
  78.                 // 图片分辨率与屏幕分辨率  
  79.                 float scale = bitmap.getWidth() / (float) dm.widthPixels;  
  80.   
  81.                 Bitmap newBitMap = null;  
  82.                 if (scale > 1) {  
  83.                     newBitMap = zoomBitmap(bitmap, bitmap.getWidth() / scale,  
  84.                             bitmap.getHeight() / scale);  
  85.                     bitmap.recycle();  
  86.                     isBig = true;  
  87.                 }  
  88.                 //添加relative布局放置img,btn  
  89.                 final RelativeLayout rl_show_2 = new RelativeLayout(this);  
  90.                 rl_show_2.setLayoutParams(new LayoutParams(  
  91.                         RelativeLayout.LayoutParams.WRAP_CONTENT,  
  92.                         RelativeLayout.LayoutParams.WRAP_CONTENT));  
  93.   
  94.                 ImageButton imgBtn_del_2 = new ImageButton(this);  
  95.                 imgBtn_del_2.setBackgroundResource(android.R.drawable.ic_delete);  
  96.   
  97.                 //设置按钮的布局规则   
  98.                 RelativeLayout.LayoutParams rl_2 = new RelativeLayout.LayoutParams(  
  99.                         new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
  100.                                 RelativeLayout.LayoutParams.WRAP_CONTENT));  
  101.                 rl_2.addRule(RelativeLayout.ALIGN_PARENT_TOP);  
  102.                 rl_2.addRule(RelativeLayout.ALIGN_RIGHT, 2);  
  103.                   
  104.                   
  105.                 // 将图片显示到界面  
  106.                 ImageView img = new ImageView(this);  
  107.                 img.setId(2);  
  108.                 img.setLayoutParams(new LayoutParams(  
  109.                         LinearLayout.LayoutParams.WRAP_CONTENT,  
  110.                         LinearLayout.LayoutParams.WRAP_CONTENT));  
  111.                 img.setScaleType(ImageView.ScaleType.CENTER_CROP);  
  112.                 img.setPadding(2005);  
  113.                 if (isBig) {  
  114.                     img.setImageBitmap(newBitMap);  
  115.                     isBig = false;  
  116.                 } else  
  117.                     img.setImageBitmap(bitmap);  
  118.                   
  119.                 rl_show_2.addView(img);  
  120.                 rl_show_2.addView(imgBtn_del_2,rl_2);  
  121.                   
  122.                   
  123.                 ll_show.addView(rl_show_2);  
  124.                   
  125.                 imgBtn_del_2.setOnClickListener(new OnClickListener() {  
  126.                       
  127.                     @Override  
  128.                     public void onClick(View v) {  
  129.                         ll_show.removeView(rl_show_2);  
  130.                     }  
  131.                 });  
  132.                   
  133.                 break;  
  134.   
  135.             case CAMERA_SELECT:  
  136.                 ContentResolver resolver = getContentResolver();  
  137.   
  138.                 // 照片的原始资源地址  
  139.                 Uri imgUri = data.getData();  
  140.   
  141.                 try {  
  142.                     // 使用ContentProvider通过Uri获取原始图片  
  143.                     Bitmap photo = MediaStore.Images.Media.getBitmap(resolver,  
  144.                             imgUri);  
  145.   
  146.                     // 获取屏幕分辨率  
  147.                     DisplayMetrics dm_2 = new DisplayMetrics();  
  148.                     getWindowManager().getDefaultDisplay().getMetrics(dm_2);  
  149.   
  150.                     // 图片分辨率与屏幕分辨率  
  151.                     float scale_2 = photo.getWidth() / (float) dm_2.widthPixels;  
  152.   
  153.                     Bitmap newBitMap_2 = null;  
  154.                     if (scale_2 > 1) {  
  155.                         newBitMap_2 = zoomBitmap(photo, photo.getWidth()  
  156.                                 / scale_2, photo.getHeight() / scale_2);  
  157.                         photo.recycle();  
  158.                         isBig = true;  
  159.                     }  
  160.   
  161.                     final RelativeLayout rl_show = new RelativeLayout(this);  
  162.                     rl_show.setLayoutParams(new LayoutParams(  
  163.                             RelativeLayout.LayoutParams.WRAP_CONTENT,  
  164.                             RelativeLayout.LayoutParams.WRAP_CONTENT));  
  165.   
  166.                     ImageButton imgBtn_del = new ImageButton(this);  
  167.                     imgBtn_del.setBackgroundResource(android.R.drawable.ic_delete);  
  168.   
  169.                     //设置按钮的布局规则   
  170.                     RelativeLayout.LayoutParams rl = new RelativeLayout.LayoutParams(  
  171.                             new LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,  
  172.                                     RelativeLayout.LayoutParams.WRAP_CONTENT));  
  173.                     rl.addRule(RelativeLayout.ALIGN_PARENT_TOP);  
  174.                     rl.addRule(RelativeLayout.ALIGN_RIGHT, 1);  
  175.   
  176.                     // 将图片显示到界面  
  177.                     ImageView img_2 = new ImageView(this);  
  178.                     img_2.setId(1);  
  179.                     img_2.setLayoutParams(new LayoutParams(  
  180.                             LinearLayout.LayoutParams.WRAP_CONTENT,  
  181.                             LinearLayout.LayoutParams.WRAP_CONTENT));  
  182.                     img_2.setScaleType(ImageView.ScaleType.CENTER_CROP);  
  183.                     img_2.setPadding(2005);  
  184.                     if (scale_2 > 1) {  
  185.                         img_2.setImageBitmap(newBitMap_2);  
  186.                         isBig = false;  
  187.                     } else  
  188.                         img_2.setImageBitmap(photo);  
  189.                       
  190.                     //将img,btn添加  
  191.                     rl_show.addView(img_2);  
  192.                     rl_show.addView(imgBtn_del,rl);  
  193.   
  194.                     ll_show.addView(rl_show);  
  195.   
  196.                     imgBtn_del.setOnClickListener(new OnClickListener() {  
  197.   
  198.                         @Override  
  199.                         public void onClick(View v) {  
  200.                             ll_show.removeView(rl_show);  
  201.                         }  
  202.                     });  
  203.   
  204.                 } catch (Exception e) {  
  205.                     // TODO: handle exception  
  206.                 }  
  207.   
  208.                 break;  
  209.             }  
  210.         }  
  211.     }  
  212.   
  213.     // 对分辨率较大的图片进行缩放  
  214.     public Bitmap zoomBitmap(Bitmap bitmap, float width, float height) {  
  215.   
  216.         int w = bitmap.getWidth();  
  217.   
  218.         int h = bitmap.getHeight();  
  219.   
  220.         Matrix matrix = new Matrix();  
  221.   
  222.         float scaleWidth = ((float) width / w);  
  223.   
  224.         float scaleHeight = ((float) height / h);  
  225.   
  226.         matrix.postScale(scaleWidth, scaleHeight);// 利用矩阵进行缩放不会造成内存溢出  
  227.   
  228.         Bitmap newbmp = Bitmap.createBitmap(bitmap, 00, w, h, matrix, true);  
  229.   
  230.         return newbmp;  
  231.   
  232.     }  
  233. }  

ok,结束。。

你可能感兴趣的:(Android系统相机,Android系统相册)