0

I get this error only from Asus ZenFone 3 (ZE552KL) (ASUS_Z012D), Android 8.0

java.lang.NullPointerException: at android.graphics.Bitmap.createScaledBitmap (Bitmap.java:714)

File imageFile = new File(
    Environment.getExternalStorageDirectory().getAbsolutePath()+"/"+imageBlocksFolder, "img122.png");

if(imageFile.exists()){

    bitmap = Bitmap.createScaledBitmap(BitmapFactory.decodeFile(imageFile.getAbsolutePath()), 50, 50, false);

    paint.setColor(cor);
    canvas.drawBitmap(bitmap, 100, 100, paint);
}

Has anyone ever encountered the same problem?

DCD
  • 325
  • 6
  • 25
  • Have you checked that BitmapFactory.decodeFile(...) isn't returning null? – Tyler V Jul 04 '18 at 01:07
  • @Tyler V, It is possible the problem is that but cellfone is 7000kms away and I have no Asus ZenFone 3. The png file is ok and in place. In other post, I saw that with R.drawable.imgagename as integer rather than saving path this solved the problem but in my case the image is downloaded from a server. – DCD Jul 04 '18 at 01:17
  • https://stackoverflow.com/questions/47788215/how-to-get-path-from-image-on-drawable-folder-and-set-as-image-view-bitmap – DCD Jul 04 '18 at 01:25
  • Well you could return it in a separate step and check for null before using it at least. You'd need a good fallback behavior then. Sorry, I don't know what specifically would cause it though, could be all sorts of things. – Tyler V Jul 04 '18 at 01:34

1 Answers1

0

Try this:

public void showImage(string filename)
    {
        analytics.LocalLog ("ShowImage");
        if (!string.IsNullOrEmpty (filename))
        {
            analytics.LocalLog ("Showing File: " + filename);

            BitmapFactory.Options options = new BitmapFactory.Options ();
            options.InSampleSize = 4;

            try {
                using(var fs = new System.IO.FileStream(filename, System.IO.FileMode.Open))
                {
                    using(Bitmap bitmap = BitmapFactory.DecodeStream (fs, null, options))
                    //using(Bitmap bitmap = BitmapFactory.DecodeFile (filename, options))
                    {
                        if(bitmap != null)
                        {
                            try {
                                momentImage.SetImageBitmap (bitmap);
                                momentImage.Visibility = ViewStates.Visible;
                            }catch{
                                GC.Collect ();
                            }
                        }else{
                            Utilities.DisplayMessage ("Could not load Image", "Please use a locally stored image, and check permissions", this);
                        }
                    }
                    fs.Close ();
                }
            }catch{
                Utilities.DisplayMessage ("Could not load Image", "Access denied to image, sorry", this);
            }

        }else{
            analytics.LocalLog ("No Filename passed");
        }
    }
Android Geek
  • 8,956
  • 2
  • 21
  • 35