Растровое фабричное декодирование Файл возвращает ноль и не работает на Android Nougat
BitmapFactory.decodeFile возвращает значение Null, где работают другие версии Android. В чем дело? Я хочу захватить изображение, а затем просмотреть изображение в режиме просмотра изображений, а затем преобразовать в 64-битную и сохранить путь к изображению.
private void captureImage() {
// Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
// fileUri = getOutputMediaFileUri(MEDIA_TYPE_IMAGE);
// intent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
// startActivityForResult(intent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
try {
Intent cameraIntent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
cameraIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
fileUri = FileProvider.getUriForFile(getApplicationContext(), BuildConfig.APPLICATION_ID + ".provider", createImageFile());
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
} else {
File imagesFolder = new File(Environment.getExternalStorageDirectory(), "Images");
imagesFolder.mkdirs();
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "Img_" + timeStamp;
String fname = imageFileName + ".png";
File file = new File(imagesFolder, fname);
fileUri = Uri.fromFile(file);
cameraIntent.putExtra(MediaStore.EXTRA_OUTPUT, fileUri);
}
startActivityForResult(cameraIntent, CAMERA_CAPTURE_IMAGE_REQUEST_CODE);
} catch (Exception e) {
e.printStackTrace();
}
}
private File createImageFile() throws IOException {
// Create an image file name
String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());
String imageFileName = "Img_" + timeStamp;
File storageDir = new File(Environment.getExternalStorageDirectory(), "Images");
File file = new File(storageDir, imageFileName + ".png");
// Save a file: path for use with ACTION_VIEW intents
temptextString = file.getAbsolutePath();//"file:" + image.getAbsolutePath();
return file;
}
public Bitmap decodeFile(String path) {
int orientation;
try {
if (path == null) {
return null;
}
// decode image size
BitmapFactory.Options o = new BitmapFactory.Options();
o.inJustDecodeBounds = true;
// Find the correct scale value. It should be the power of 2.
final int REQUIRED_SIZE = 70;
int width_tmp = o.outWidth, height_tmp = o.outHeight;
int scale = 0;
while (true) {
if (width_tmp / 2 < REQUIRED_SIZE
|| height_tmp / 2 < REQUIRED_SIZE)
break;
width_tmp /= 2;
height_tmp /= 2;
scale++;
}
// decode with inSampleSize
BitmapFactory.Options o2 = new BitmapFactory.Options();
// o2.inSampleSize = scale;
o2.inSampleSize = 8;
Bitmap bm = BitmapFactory.decodeFile(path, o2);
Bitmap bitmap = bm;
ExifInterface exif = new ExifInterface(path);
orientation = exif
.getAttributeInt(ExifInterface.TAG_ORIENTATION, 1);
Log.e("ExifInteface .........", "rotation =" + orientation);
// exif.setAttribute(ExifInterface.ORIENTATION_ROTATE_90, 90);
Log.e("orientation", "" + orientation);
Matrix m = new Matrix();
if ((orientation == ExifInterface.ORIENTATION_ROTATE_180)) {
m.postRotate(180);
// m.postScale((float) bm.getWidth(), (float) bm.getHeight());
// if(m.preRotate(90)){
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
return bitmap;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_90) {
m.postRotate(90);
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
return bitmap;
} else if (orientation == ExifInterface.ORIENTATION_ROTATE_270) {
m.postRotate(270);
Log.e("in orientation", "" + orientation);
bitmap = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), m, true);
return bitmap;
}
return bitmap;
} catch (Exception e) {
return null;
}
}
Здесь Bitmap bm = BitmapFactory.decodeFile(путь, o2); вернуть ноль, как можно решить? Android 4.0 работает