0

I know this question has been asked already, but none of the answers helped me. In my gallery app there are special jpg files that i can not decode. No matter where i copy those files, the method BitmapFactory.decodeFile() always return null

 if(file.exists()) {
     // val bitmap = BitmapFactory.decodeStream(file.inputStream())
     val bitmap = BitmapFactory.decodeFile(file.absolutePath)
     if (bitmap != null) {
         return bitmap
     }
 }
 createEmptyBitmap()

But it must be possible, because other gallery apps can show those jpg files. Is there any alternative for creating a Bitmap from a File in Android?

richard
  • 724
  • 2
  • 14
  • 36
  • 1
    If those files are not null, get `Uri`s of the files. `val uri = Uri.fromFile(your_file)` then decode bitmaps from the `Uri`s https://stackoverflow.com/a/58008340/8531215 – TaQuangTu Sep 20 '20 at 10:36
  • Thanks, but the method MediaStore.Images.Media.getBitmap(context.contentResolver, uri) returned this exception: java.lang.NullPointerException: MediaStore.Images.Media.…ext.contentResolver, uri) must not be null – richard Sep 20 '20 at 10:49
  • 1
    Make sure that READ_EXTERNAL_STORAGE permission is granted, if the issue still present, use Glide library instead – Mouaad Abdelghafour AITALI Sep 20 '20 at 10:56
  • If the resolution of those files is high the bitmap could become too big for available memory. Then null is returned. – blackapps Sep 20 '20 at 13:35
  • Meanwhile i found out that the images that create the problem seem to be corrupt. The file manager shows me for one of the images the size 5,84MB but the resolution -1 x -1 Pixel, and i can not open them in any editor. But still other apps can show a thumbnail of those images. – richard Sep 21 '20 at 16:10

0 Answers0