When I am calling this function there is no image in image view
bitmapFactory.decodefile(filename)
showing null .. please help for this.
Here is my cod
if you are trying in android 10 try adding this to your application in manifest
android:requestLegacyExternalStorage="true"
it may be a permission issue with android 10 since it doesn't allow direct access to files
I'm building an image processing app and have also returned null when trying these methods. My console returned not an error but a degub statement like this
D/skia: --- decoder->decode returned false
Here is what I found that will get any bitmap to load throughout my application
1) correct file name with appropriate permissions
2) Scale image down when appropriate
3) If you need a large image set it under you manifest like so
<application
android:largeHeap="true"
</application>
Using a large heap is not a substitute for displaying the correct image size. Here is an example of the code I use directly from androids docs
public Bitmap getRawImageThumb(Context mContext)
{
Bitmap b = null;
int reqHeight = 100, reqWidth = 100;
String filename = mContext.getFilesDir().toString() + "/" + rawFileName;
Log.d(TAG, "processRawReceipt: " + rawFileName);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
//options.inPreferredConfig = Bitmap.Config.ARGB_8888;
BitmapFactory.decodeFile(filename, options);
int height = options.outHeight;
int width = options.outWidth;
int inSampleSize = 1;
if (height > reqHeight || width > reqWidth) {
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) >= reqHeight
&& (halfWidth / inSampleSize) >= reqWidth) {
inSampleSize *= 2;
}
}
options.inSampleSize = inSampleSize;
options.inJustDecodeBounds = false;
Log.d(TAG, "getRawImage: " + String.valueOf(height) + " " + String.valueOf(width) + " " + String.valueOf(inSampleSize));
b = BitmapFactory.decodeFile(filename, options);
return b;
}
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
This returns null, if
if the above two point are okay in your code, it means you are getting a null value because its taken time for android to render the image.
add a dummy imageviewer in your code to speed up the render process.
bm=BitmapFactory.decodeFile(dir.getAbsolutePath());
imgPassport.setImageBitmap(bm); // This imageviewer is not visible in my App
Also ensure you are not calling the method decodeFile(dir.getAbsolutePath()) from background thread
A clear explanation is given in google documentation https://developer.android.com/topic/performance/graphics/load-bitmap#java
Setting the inJustDecodeBounds property to true while decoding avoids memory allocation, returning null
for the bitmap object.
as per @Jeroen VL
changing options.inJustDecodeBounds = false solved the problem
Try to add SD card access permissions READ_EXTERNAL_STORAGE and/or WRITE_EXTERNAL_STORAGE. It works for me.
Why are you doing this String imageInSD = "/sdcard/UserImages/"+userImageName;
I think If you get a .png file is present then just,
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
NOTE: Also check you have a Android supported image file is present in that location..