android 拍照及本地获取图片,并显示

直接上代码,代码中注释挺多的。

拍照选择

//选择图片方式

   public void showImagePickDialog() {

      String title ="获取图片方式";

      String[] choices =new String[]{"拍照","从手机中选择"};

     

      new AlertDialog.Builder(this)

        .setTitle(title)

        .setItems(choices,new DialogInterface.OnClickListener() {

           public void onClick(DialogInterface dialog, intwhich) {

              dialog.dismiss();

              switch (which) {

              case 0://拍照

                 try {

                    String state = Environment.getExternalStorageState();

                    if (state.equals(Environment.MEDIA_MOUNTED)) {//判断sd卡是否插入

                      //设置路径

                      mPhotoPath = Environment.getExternalStorageDirectory().getPath()

                            + "//patrol//" +"//" + photoUtil.getPhotoFileName();

                      mPhotoFile =new File(mPhotoPath);

                      if (!mPhotoFile.exists()) {

                         mPhotoFile.createNewFile();

                      }

                      Intent intent =new Intent("android.media.action.IMAGE_CAPTURE");

                      startActivityForResult(intent,GET_IMAGE_BY_CAMERA);

                    }else {

                        appContext.showInfo("请插入SD");

                    }

                 } catch (Exceptione) {

                 }

                 break;

              case 1://从手机选择

                 Intent intent2 =new Intent();

                 intent2.setType("image/*");

                 intent2.setAction(Intent.ACTION_GET_CONTENT);

                 startActivityForResult(intent2,GET_IMAGE_FROM_PHONE);

                 break;

              }

           }

        })

        .setNegativeButton("返回",null)

        .show();

   }

   /**

    * 拍照返回

    * 注意读取大图片时会停止程序,所以要对拍照或从本地读取的图片进行压缩。

    * 拍照:bitmap.compress(CompressFormat.JPEG,30, fos);

    * 本地图片:

    */

   @SuppressWarnings("deprecation")

   @Override

   protected void onActivityResult(intrequestCode, intresultCode, Intent data) {

      super.onActivityResult(requestCode,resultCode, data);

      switch (requestCode) {

      case GET_IMAGE_BY_CAMERA:

          Bitmap bitmap =data.getParcelableExtra("data");

          if(bitmap!=null){

           try {

                  FileOutputStream fos =new FileOutputStream(mPhotoFile);

                   bitmap.compress(CompressFormat.JPEG, 30,fos);//30 是压缩率,表示压缩70%;如果不压缩是100,表示压缩率为

                   fos.flush();

                   fos.close();

                   Bitmap newBitmap =photoUtil.rotaingImageView(photoUtil.readPictureDegree(mPhotoPath),bitmap);

                   circleImage.setImageBitmap(newBitmap);

                 String base64 =photoUtil.Bitmap2StrByBase64(newBitmap);

               appContext.setCircleimageuri(base64);

               mPhotoFile.delete();

              } catch (IOExceptione) {

                 appContext.showInfo("获取图片失败");

              }

          }

        break;

      case GET_IMAGE_FROM_PHONE:

        ContentResolver resolver =getContentResolver();

        try {

           Uri originalUri =data.getData();//获得图片的uri

           //一。对图片进行压缩

           BitmapFactory.Options options =new BitmapFactory.Options();

           // 设置options.inJustDecodeBoundstrue

           options.inJustDecodeBounds =true;

           // 节约内存

           options.inPreferredConfig = Bitmap.Config.RGB_565;// 默认是Bitmap.Config.ARGB_8888

           /* 下面两个字段需要组合使用 */

           options.inPurgeable =true;

           options.inInputShareable =true;

           //此时不会把图片读入内存,只会获取图片宽高等信息

           Bitmap bm = BitmapFactory.decodeStream(resolver.openInputStream(originalUri),null, options);

           int heitht = options.outHeight;

           Log.i("path","" + heitht);

           // 根据需要设置压缩比例

           int size = heitht / 800;

           if (size <= 0) {

           size = 1;

           }

           options.inSampleSize =size;

           // 设置options.inJustDecodeBounds重新设置为false

           options.inJustDecodeBounds =false;

           //此时图片会按比例压缩后被载入内存中

           bm = BitmapFactory.decodeStream(resolver.openInputStream(originalUri),null, options);

           //bm =MediaStore.Images.Media.getBitmap(resolver, originalUri); //显得到bitmap图片

           //二。获取图片的路径:

           String[] proj = {MediaStore.Images.Media.DATA};

           //好像是android多媒体数据库的封装接口,具体的看Android文档

           Cursor cursor =managedQuery(originalUri, proj, null, null,null);

           //按我个人理解这个是获得用户选择的图片的索引值

           int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

           //将光标移至开头,这个很重要,不小心很容易引起越界

           cursor.moveToFirst();

           //最后根据索引值获取图片路径

           String path =cursor.getString(column_index);

           //三。调整图片的角度,显示出来并保存base64

           Bitmap newBitmap =photoUtil.rotaingImageView(photoUtil.readPictureDegree(path),bm);

           circleImage.setImageBitmap(newBitmap);

           String base64 =photoUtil.Bitmap2StrByBase64(newBitmap);

              appContext.setCircleimageuri(base64);

        }catch (IOExceptione) {

           appContext.showInfo("获取图片失败");

        }

        break;

      default:

        break;

      }

   }

你可能感兴趣的:(Android--基础)