Bitmap is returning null from BitmapFactory.decodeFile(filename)
Hi it is null
because may be the image size is big and getting exception please check your log and see is there any error of outofmemory bitmap if yes then use options for that:
BitmapFactory.Options options;
try {
String imageInSD = "/sdcard/UserImages/" + userImageName;
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD);
return bitmap;
} catch (OutOfMemoryError e) {
try {
options = new BitmapFactory.Options();
options.inSampleSize = 2;
Bitmap bitmap = BitmapFactory.decodeFile(imageInSD, null, options);
return bitmap;
} catch(Exception excepetion) {
Log.e(excepetion);
}
}
android 10 BitmapFactory.decodeFile(ImageFilePath) return null
InputStream is = getContentResolver().openInputStream(data.getData());
Bitmap bitmap = BitmapFactory.decodeStream(is);
Use this for all Android versions.
Stop with trying to get a path for an uri.
BitmapFactory.decodeFile() returning null for existing image
Try adding bmOptions.inMutable = true after you have created it or try this
answer.
BitmapFactory.decodeFile return null
You should load the image first, then open the output stream, as this erases the existing content.
Log.d("startedfromthebottom", file.getAbsolutePath()); //show the same path as in the activityresult above
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath()); //bitmap is always null
OutputStream os = new BufferedOutputStream(new FileOutputStream(file));
bitmap.compress(Bitmap.CompressFormat.JPEG,100,os);
This is a bit dangerous because if something goes wrong the image is lost. It could be a good idea to use an AtomicFile.
Edit (based on your updated code) :
String imgPath = rep.getImgList().get(0);
File file = new File(imgPath);
AtomicFile atomicFile = new AtomicFile(file);
FileOutputStream fos = null;
try {
// read the current image
Bitmap bitmap = BitmapFactory.decodeFile(file.getAbsolutePath());
// open the stream (backup the current content)
// from now on (and until finishWrite/failWrite) we cannot read the file directly
fos = atomicFile.startWrite();
Log.d("showmethebitmap", bitmap.toString()); //Error: bitmap is null !
OutputStream oos = new BufferedOutputStream(fos);
bitmap.compress(Bitmap.CompressFormat.JPEG,0,oos);
// flush but do not close the stream (@see AtomicFile doc)
oos.flush();
// close the stream, remove the backup
atomicFile.finishWrite(fos);
...
} catch (IOException e) {
// recover the content from the backup
atomicFile.failWrite(fos);
throw e;
}
Why Bitmap.decodeFile() method is returning null?
Create/Save filePath : mCurrentPhotoPath
like this in your createImageFile() Method.
File imageFile = new File(image.getAbsolutePath());
mCurrentPhotoPath = imageFile.toString();
BitmapFactory.decodeFile returns null with inJustDecodeBounds set to false
First, create a bitmap from file path
File imgFile = new File("/storage/emulated/0/Hootout/HootImages/Profilepic/user_profile_photo.jpg");
if(imgFile.exists()){
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath(),bmOptions);
bitmap = Bitmap.createScaledBitmap(bitmap,parent.getWidth(),parent.getHeight(),true);
imageView.setImageBitmap(bitmap);
}
I think you are giving the wrong file path.
Let me know if any error again.
Android: decodeFile always returns null for file in internal storage
This question has been answered before such as here:
BitmapFactory.decodeFile returns null even image exists
This was exactly what I needed:
String fname=new File(getFilesDir(), "test.png").getAbsolutePath();
Related Topics
Android:Static Variable Null on Low Memory
How Does Bitmap Allocation Work on Oreo, and How to Investigate Their Memory
Understanding Android's <Layer-List>
How to Put the Text on the Left of a Radio Button in Android
How to Use Google Login API with Cordova/Phonegap
How to Use Recyclerview.Scrolltoposition() to Move the Position to the Top of Current View
Android Progressbar UI Custom Layout
The Application Icon Does Not Show on Action Bar
Best Way to Update Data with a Recyclerview Adapter
Justify Text in an Android App Using a Webview But Presenting a Textview-Like Interface
Emulator Appearing Offline on M1 MAC After the Last Update of Arm64-V8A
How to Play Audio File in Android
How to Make an Intent with Multiple Actions
Mylib.So Has Text Relocations. This Is Wasting Memory and Is a Security Risk. Please Fix
Android Finish() Method Doesn't Clear App from Memory
Android Studio Unit Testing: Read Data (Input) File
Phonegap - Navigator.App.Backhistory() Not Working on HTML Back Button