Android:加载照片时出错

时间:2015-09-11 10:14:16

标签: java android

拍照后我试图从相机中裁剪图像,所以我有这个解决方案:

这是启动相机的onClick事件

public void onClick(View v) {

  PackageManager pm = getPackageManager();

    if (pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) {

   Intent i = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);

   i.putExtra(MediaStore.EXTRA_OUTPUT, MyFileContentProvider.CONTENT_URI);

   startActivityForResult(i, CAMERA_RESULT);

  } else {

   Toast.makeText(getBaseContext(), "Camera is not available", Toast.LENGTH_LONG).show();

  }   }

//这是onActivityResult

@Override

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {

  super.onActivityResult(requestCode, resultCode, data);

  Log.i(Tag, "Receive the camera result");

    if (resultCode == RESULT_OK && requestCode == CAMERA_RESULT) {

        performCrop();

   }else if( requestCode == 5){

       String path = this.imageFileUri.getPath();
       //Bitmap bit = decodeSampledBitmapFromPath(path, 600, 600);


     int rotate = 0;
     try {
         ExifInterface exif = new ExifInterface(
                 this.imageFileUri.getPath());
         int orientation = exif.getAttributeInt(
                 ExifInterface.TAG_ORIENTATION,
                 ExifInterface.ORIENTATION_NORMAL);

         switch (orientation) {
         case ExifInterface.ORIENTATION_ROTATE_270:
             rotate = 270;
             break;
         case ExifInterface.ORIENTATION_ROTATE_180:
             rotate = 180;
             break;
         case ExifInterface.ORIENTATION_ROTATE_90:
             rotate = 90;
             break;
         }
     } catch (Exception e) {
         e.printStackTrace();
     }


     Matrix matrix = new Matrix();
     matrix.postScale(0.5f, 0.5f);
     matrix.postRotate(rotate);

    // Bitmap bitmap = decodeFile(out);
     Bitmap bitmap = decodeSampledBitmapFromPath(path, 600, 600);

     //Bitmap mBitmap = BitmapFactory.decodeFile(out.getAbsolutePath());
    bitmap = Bitmap.createBitmap(bitmap, 1, 1, bitmap.getWidth() - 1, bitmap.getHeight() - 1,matrix,true);



   imageView1.setImageBitmap(bitmap);

  }

 }

这是执行裁剪方法

private void performCrop() {
     Intent var1 = new Intent("com.android.camera.action.CROP");
     File out = new File(getFilesDir(), "newImage.jpg");
     if(!out.exists()) {

            Toast.makeText(getBaseContext(),

              "Error while capturing image", Toast.LENGTH_LONG)

              .show();

            return;}
     this.imageFileUri = Uri.fromFile(out);
     var1.setDataAndType(this.imageFileUri, "image/*");
     System.out.println(this.imageFileUri.getPath());
     var1.putExtra("crop", "true");
     var1.putExtra("scale", true);
     var1.putExtra("return-data", false);
     var1.putExtra("output", this.imageFileUri);
     this.startActivityForResult(var1, 5);
  }

目前的问题是,在从相机获取结果然后加载裁剪方法后,它会在加载照片时显示错误。无法启动照片编辑器。

我查看了我的代码,从这里看,一切似乎都没问题。 请帮助我解决我的错误。

感谢。

1 个答案:

答案 0 :(得分:0)