Android: BitmapFactory.decodeFile / OpenCV does return invaild Bitmap
-
03-12-2019 - |
Question
I'm taking a picture with the camera activity and then try to read it from the saved location. Unfortunately the Bitmap returnd by:
Bitmap bimage = BitmapFactory.decodeFile( path );
has height and width = -1. I'm using OpenCV and when I read the image with
Mat img = Highgui.imread(path);
I get a proper matrix. Altough I cannot convert it to a bitmap. When doing
bmp = Bitmap.createBitmap(img.cols(), img.rows(), Config.ARGB_8888);
Utils.matToBitmap(img, bmp);
I get the same result again: A Bitmap with width and height = -1. Do I need to watch out for the image format? Can I set that on the camera intent? Shouldn't
BitmapFactory.decodeFile( path );
automatically realize what the format is?
I'm running this on a Samsung galaxy S with Android 2.3.1 and OpenCV 2.3.1
Thanks for your help.
Solution
Samsung has some issues with camera intent Photo capture Intent causes NullPointerException on Samsung phones only
Try this way
// to call camera
String _path = Environment.getExternalStorageDirectory()
+ File.separator + "TakenFromCamera.png";
File file = new File(_path);
Uri outputFileUri = Uri.fromFile(file);
Intent intent = new Intent(
android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
intent.putExtra(MediaStore.EXTRA_OUTPUT, outputFileUri);
startActivityForResult(intent, 1212);
// in activity onResult
if (requestCode == 1212) {
String _path = Environment.getExternalStorageDirectory()
+ File.separator + "TakenFromCamera.png";
mBitmap = BitmapFactory.decodeFile(_path);
if (mBitmap == null) {
} else {
Intent intent = new Intent(AYGCamActivity.this,
myGalleryImage.class);
startActivity(intent);
}
}