Thanks for reading.
[EDIT] If I omit the options, i.e.,
Bitmap bmp = BitmapFactory.decodeFile(picturePath);
Then it returns a bitmap.
But with the options as argument, i.e.,
bmp = BitmapFactory.decodeFile(picturePath, options);
it returns null.
Anyone can divine what is missing or wrong with options?
Thanks again,
[end of EDIT]
The goal is to select an image, downsample it if needed, save it, then load it into a imageview.
The BitmapFactory decode always returns null. I do have permissions set in manifest, the path appears complete.. /storage/emulated/0/aerg.png
See below:
if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data)
{
Uri selectedImage = data.getData();
String[] filePathColumn = {MediaStore.Images.Media.DATA};
Cursor cursor = getContentResolver().query(selectedImage,
filePathColumn, null, null, null);
cursor.moveToFirst();
int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
String picturePath = cursor.getString(columnIndex);
cursor.close();
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
String imageType = options.outMimeType;
// Raw height and width of image
final int height = options.outHeight;
final int width = options.outWidth;
BitmapFactory.decodeFile(picturePath, options);
int inSampleSize = 1;
if (height > mheight || width > mWidth)
{
final int halfHeight = height / 2;
final int halfWidth = width / 2;
// Calculate the largest inSampleSize value that is a power of 2 and keeps both
// height and width larger than the requested height and width.
while ((halfHeight / inSampleSize) > mheight
&& (halfWidth / inSampleSize) > mWidth)
{
inSampleSize *= 2;
}
}
options.inSampleSize = inSampleSize;
Bitmap bmp = BitmapFactory.decodeFile(picturePath,options);
String downsampledPicturePath = saveImage(bmp, picturePath, "PrinterImages");
Bitmap b = BitmapFactory.decodeFile(downsampledPicturePath);
iv.setImageBitmap(b);
//ImageView imageView = (ImageView) findViewById(R.id.theImageView);
//imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));
}
}
private String saveImage(Bitmap bmp, String path, String folderName)
{
Context context = getApplicationContext();
File folder = context.getDir(folderName, Context.MODE_PRIVATE); //Creating an internal dir;
String pictureName = path.substring(path.lastIndexOf("/") + 1);
String picture = new File(folder, pictureName).getAbsolutePath();
FileOutputStream fos = null;
try
{
fos = new FileOutputStream(picture);
// Use the compress method on the BitMap object to write image to the OutputStream
bmp.compress(Bitmap.CompressFormat.PNG, 100, fos);
fos.close();
} catch (Exception e)
{
e.printStackTrace();
}
return picture;
}
I'll just start with the entry in the docs for decodeFile, as that is obviously very important;
"
public static Bitmap decodeFile (String pathName, BitmapFactory.Options opts)
Added in API level 1 Decode a file path into a bitmap. If the specified file name is null, or cannot be decoded into a bitmap, the function returns null.
Parameters pathName complete path name for the file to be decoded. opts null-ok; Options that control downsampling and whether the image should be completely decoded, or just is size returned. Returns The decoded bitmap, or null if the image data could not be decoded, or, if opts is non-null, if opts requested only the size be returned (in opts.outWidth and opts.outHeight)
"
I don't quite understand this description, but it appears to me that you never set a width or height in the options from looking at your code? I might be completely off with this though.
Note also; http://developer.android.com/reference/android/graphics/BitmapFactory.Options.html
"public boolean inJustDecodeBounds If set to true, the decoder will return null (no bitmap), but the out..."
I've sort of written this response on the go. I think the quote above is your problem however - you set this to true in your code.