android 调用系统的照相机和图库

 

                 android手机有自带的照相机和图库,我们做的项目中有时用到上传图片到服务器,今天做了一个项目用到这个功能,所以把我的代码记录下来和大家分享,第一次写博客希望各位大神多多批评。

             首先上一段调用android相册和相机的代码:

  

Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);//调用android自带的照相机
   photoUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
  startActivityForResult(intent, 1);

 

Intent i = new Intent(Intent.ACTION_PICK,
    android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);//调用android的图库
  startActivityForResult(i, 2);

 

@Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
//完成照相后回调用此方法
super.onActivityResult(requestCode, resultCode, data); case 1: switch (resultCode) { case Activity.RESULT_OK://照相完成点击确定
String sdStatus = Environment.getExternalStorageState();    

              if (!sdStatus.equals(Environment.MEDIA_MOUNTED)) { // 检测sd是否可用     

                  Log.v("TestFile",        "SD card is not avaiable/writeable right now.");     

                     return;     }

               Bundle bundle = data.getExtras();    

                   Bitmap bitmap = (Bitmap) bundle.get("data");// 获取相机返回的数据,并转换为Bitmap图片格式    

                    FileOutputStream b = null;    

                        File file = new File("/sdcard/pk4fun/");    

                         file.mkdirs();// 创建文件夹,名称为pk4fun     // 照片的命名,目标文件夹下,以当前时间数字串为名称,即可确保每张照片名称不相同。网上流传的其他Demo这里的照片名称都写死了,则会发生无论拍照多少张,后一张总会把前一张照片覆盖。细心的同学还可以设置这个字符串,比如加上“IMG”字样等;然后就会发现sd卡中myimage这个文件夹下,会保存刚刚调用相机拍出来的照片,照片名称不会重复。    

                    String str = null;    

                   Date date = null;    

                    SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmss");// 获取当前时间,进一步转化为字符串    

                       date = new Date(resultCode);    

                 str = format.format(date);    

                     String fileName = "/sdcard/myImage/" + str + ".jpg";    

                  sendBroadcast(fileName);    

                   try {     

                     b = new FileOutputStream(fileName);     

                    bitmap.compress(Bitmap.CompressFormat.JPEG, 100, b);// 把数据写入文件   

                 } catch (FileNotFoundException e) {     

                   e.printStackTrace();    

                    } finally {    

                      try {    

                      b.flush();     

                             b.close();     

                            } catch (IOException e) {   

                         e.printStackTrace();   

                   }    

             }   

break;
            case Activity.RESULT_CANCELED:// 取消
            
                break;
            }
            break;
        case 2:
            switch (resultCode) {
            case Activity.RESULT_OK: {
                Uri uri = data.getData();
                Cursor cursor = mActivity.getContentResolver().query(uri, null,
                        null, null, null);
                cursor.moveToFirst();
                String imgNo = cursor.getString(0); // 图片编号
                String imgPath = cursor.getString(1); // 图片文件路径
                String imgSize = cursor.getString(2); // 图片大小
                String imgName = cursor.getString(3); // 图片文件名
                cursor.close();
                // Options options = new BitmapFactory.Options();
                // options.inJustDecodeBounds = false;
                // options.inSampleSize = 10;
                // Bitmap bitmap = BitmapFactory.decodeFile(imgPath, options);
                
            }
                break;
            case Activity.RESULT_CANCELED:// 取消
                
                break;
            }
            break;
        
    }

最后记得加权限

    <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <uses-permission android:name="android.permission.CAMERA" />
    <uses-permission android:name="android.permission.RECORD_AUDIO" />
    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
   

posted @ 2012-12-06 16:15  唯美坏少  阅读(667)  评论(0编辑  收藏  举报