无法在图像视图中看到我原来拍摄的照片

时间:2018-05-17 21:34:04

标签: android

我是android的新手。在我的应用程序中,我有一个图像视图。我希望通过相机看到拍摄的照片。但是当我拍照时它会自动旋转并在图像视图中显示。我使用旋转功能但是时间会出错。

  

主要活动

image.setOnClickListener(new View.OnClickListener() {
        @Override
        public void onClick(View view) {
            Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
            startActivityForResult(intent,101 );

        }
    });
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {

    try {
        switch (requestCode) {
            case 101:
                if (resultCode == Activity.RESULT_OK) {
                    if (data != null) {
                        selectedImage = data.getData(); // the uri of the image taken
                        if (String.valueOf((Bitmap) data.getExtras().get("data")).equals("null")) {
                            bitmap = MediaStore.Images.Media.getBitmap(this.getContentResolver(), selectedImage);
                        } else {
                            bitmap = (Bitmap) data.getExtras().get("data");
                        }
                        if (Float.valueOf(getImageOrientation()) >= 0) {
                            bitmapRotate = rotateImage(bitmap, Float.valueOf(getImageOrientation()));
                        } else {
                            bitmapRotate = bitmap;
                            bitmap.recycle();
                        }


                        image.setImageBitmap(bitmapRotate);

                       //   Saving image to mobile internal memory for sometime
                        String root = getApplicationContext().getFilesDir().toString();
                        File myDir = new File(root + "/androidlift");
                        myDir.mkdirs();

                        Random generator = new Random();
                        int n = 10000;
                        n = generator.nextInt(n);

                        //   Give the file name that u want
                        fname = "null" + n + ".jpg";

                        imagepath = root + "/androidlift/" + fname;
                        file = new File(myDir, fname);
                        upflag = true;
                    }
                }
        }
    } catch (Exception e) {
        e.printStackTrace();
    }
    super.onActivityResult(requestCode, resultCode, data);

}

public static Bitmap rotateImage(Bitmap source, float angle) {
    Bitmap retVal;

    Matrix matrix = new Matrix();
    matrix.postRotate(angle);
    retVal = Bitmap.createBitmap(source, 0, 0, source.getWidth(), source.getHeight(), matrix, true);

    return retVal;
}

//    In some mobiles image will get rotate so to correting that this code will help us
private int getImageOrientation() {
    final String[] imageColumns = {MediaStore.Images.Media._ID, MediaStore.Images.ImageColumns.ORIENTATION};
    final String imageOrderBy = MediaStore.Images.Media._ID + " DESC";
    Cursor cursor = getContentResolver().query(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
            imageColumns, null, null, imageOrderBy);// error line

    if (cursor.moveToFirst()) {
        int orientation = cursor.getInt(cursor.getColumnIndex(MediaStore.Images.ImageColumns.ORIENTATION));
        System.out.println("orientation===" + orientation);
        cursor.close();
        return orientation;
    } else {
        return 0;
    }
}
  

错误:java.lang.SecurityException:Permission Denial:从pid = 16783读取com.android.providers.media.MediaProvider uri content:// media / external / images / media,uid = 10085需要android.permission.READ_EXTERNAL_STORAGE或grantUriPermission()

当我添加此旋转功能时,图像视图中没有显示图片。显示此错误。请帮帮我。

0 个答案:

没有答案