Android中系统中,调用系统相机拍照并取得照片的文件做进一步的使用(裁剪、显示在View控件上、上传文件等)。

第一种情况:调用系统相机拍照保存在系统中并在onActvitiyResult()方法中,获取文件的UriBitmap对象。

                          代码如下: 

                          ------------------------------------------------------------------------------------------------

                                  // 调用系统相机拍照

             Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);

             //设置日期的转换格式

             SimpleDateFormat timeStampFormat =new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");

                //设置文件名

             String filename =timeStampFormat.format(new Date());

             //使用ContentValues保存文件名

             ContentValues values = newContentValues();

            values.put(MediaStore.Images.Media.TITLE, filename);

               //使用内容提供者,定义照片保存的Uri

             photoUri =getContentResolver().insert(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,values);                      

             intent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri);

             startActivityForResult(intent, CAMREA_REQUEST_CODE);

         -------------------------------------------------------------------------------------------------

          onActivityResult()方法中获取数据

                          -------------------------------------------------------------------------------------------------

                                    //请求码等于相机的请求码,执行相应的操作

              if (requestCode== CAMREA_REQUEST_CODE && requestcode ==RESULT_OK) {

                   //判断返回的数据data是否为空,在三星s5、华为p7等机型上面有data为空的现象

                  if (data != null &&data.getData() != null) {

                      uri = data.getData();

                     if(data.hasExtra("data")){

                         Bitmap thunbnail = data.getParcelableExtra("data"); //获取照片的Bitmap对象,并设置

                      }

                   }

                      //如果data数据为空,就令uri==之前指定保存的照片的photoUri

                  if(uri == null) {

                      if(photoUri != null) {

                          uri= photoUri;

                         startPhotoZoom1(uri);//裁剪照片的方法

                      }

                  }

              }

         -------------------------------------------------------------------------------------------------

第二种情况:调用系统相机拍照,将照片保存在固定的文件目录下,在onActivityResult()方法中获取图片信息。这种情况下就不会                      发生datanull 的现象。

                          代码如下:

                         ------------------------------------------------------------------------------------------------         

             Intent intentFromCapture = newIntent(MediaStore.ACTION_IMAGE_CAPTURE);

             File file = newFile(Environment.getExternalStorageDirectory().getPath()+"/wood/head/");

             //是否是文件夹,不是就创建文件夹

             if (!file.exists()) file.mkdirs();

              //指定保存路径

             cameraPath = Environment.getExternalStorageDirectory().getPath()+"/wood/head/" +

                          format.format(new Date()) + ".jpg";

             File imageFile = new File(cameraPath);

             //创建一个图片保存的Uri

             Uri imageFileUri = Uri.fromFile(imageFile);

             intentFromCapture.putExtra(MediaStore.Images.Media.ORIENTATION, 0);

             //设置MediaStore.EXTRA_OUTPUT的输出路径为imageFileUri

             intentFromCapture.putExtra(MediaStore.EXTRA_OUTPUT, imageFileUri);

             startActivityForResult(intentFromCapture, CAMERA_REQUEST_CODE);

                         

                          -------------------------------------------------------------------------------------------------

          onActivityResult()方法中获取数据

                          -------------------------------------------------------------------------------------------------

            /相机的请求编码

                case CAMERA_REQUEST_CODE:

                 isCamera =true;

                 startPhotoZoom(Uri.fromFile(new File(cameraPath)));

                 break;

            //相机、相册的图片再 剪辑完 在这地方上传

          case RESULT_REQUEST_CODE:

                if (isCamera){

                   upLoadPictrue(new File(cameraPath));

                } else {

                   String path = null;

                    if (android.os.Build.VERSION_CODES.KITKAT >= 19) {

                        path = new GetPicPath().getPath_above19(getActivity(), uri1);

                    }else {

                        path = getFilePath_below19(uri1);

                    }

                   upLoadPictrue(new File(path));

               }

               break;

 

          -------------------------------------------------------------------------------------------------

裁剪图片: 代码如下:

            public void startPhotoZoom1(Uri uri) {

                SimpleDateFormat timeStampFormat = new SimpleDateFormat("yyyy_MM_dd_HH_mm_ss");

                Filefile = new File(Environment.getExternalStorageDirectory().getPath()+"/wood/head/");

                if (!file.exists())file.mkdirs();

                //指定保存路径

                              String photoPath = Environment.getExternalStorageDirectory().getPath()+ "/wood/head/"+

                                   timeStampFormat.format(newDate()) + ".jpg";

                if (uri ==null) {

                   System.out.println("The uri is not exist.");

                }

                Filefile1 = new File(photoPath);

                uri1 =Uri.fromFile(file1);

                Intent intent = new Intent("com.android.camera.action.CROP");

                intent.setDataAndType(uri, "image/*");

                intent.putExtra("crop", "true");//可裁剪

                                        intent.putExtra("aspectX", 1);

                intent.putExtra("aspectY", 1);

                intent.putExtra("outputX", 320);

                intent.putExtra("outputY", 320);

                intent.putExtra("scale", true);

                intent.putExtra(MediaStore.EXTRA_OUTPUT, uri1);//输出到指定的uri

                intent.putExtra("return-data", false);//若为false则表示不返回数据

                                        intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

                intent.putExtra("noFaceDetection", true);

                startActivityForResult(intent, RESULT_REQUEST_CODE);

              }

          -------------------------------------------------------------------------------------------------

              public void startPhotoZoom(Uri uri){

                  if (uri ==null) {

                   //System.out.println("The uri is null.");

                  }

                 Intent intent = new Intent("com.android.camera.action.CROP");

                 intent.setDataAndType(uri, "image/*");

                 intent.putExtra("crop", "true");//可裁剪

                  intent.putExtra("aspectX", 1);

                 intent.putExtra("aspectY", 1);

                 intent.putExtra("outputX", 320);

                 intent.putExtra("outputY", 320);

                 intent.putExtra("scale", true);

                 intent.putExtra(MediaStore.EXTRA_OUTPUT,uri);

                 intent.putExtra("return-data", false);//若为false则表示不返回数据

                  intent.putExtra("outputFormat", Bitmap.CompressFormat.JPEG.toString());

                 intent.putExtra("noFaceDetection", true);

                 startActivityForResult(intent, RESULT_REQUEST_CODE);

             }

 

更多相关文章

  1. android opengl 实现相机的实时滤镜
  2. Android菜鸟笔记-调用相机拍照后返回照片过小的问题
  3. Android(安卓)图片的裁剪与相机调用
  4. Android(安卓)surfaceview 自定义相机 拍照(闪光灯、前后摄像头)
  5. 【Android(安卓)Studio】Camera.Parameters parameters = mCamer
  6. android 实现APP中改变头像图片
  7. Android获得相机的所有信息
  8. OKhttp上传头像,调用相机相册进行裁剪
  9. 使用照相机开启闪光灯

随机推荐

  1. 系出名门Android(3) - 对话框(Dialog)和
  2. Android 利用JNI调用Android Java代码函
  3. Android(安卓)8.0通知不显示
  4. Android中Drawable分类汇总
  5. Android调用系统设置
  6. 关于Android发送邮件
  7. Android(安卓)混淆问题排查
  8. Ubuntu下使用adb和USB连接的方式进行andr
  9. 在android media framework中添加播放器
  10. android2.2中EditText不可编辑的问题