BitmapFactory.decodeFile NullPointerException

时间:2018-07-03 23:25:57

标签: java android bitmap

我仅从华硕ZenFone 3(ZE552KL)(ASUS_Z012D),Android 8.0收到此错误

java.lang.NullPointerException:   在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);
}

有人遇到过同样的问题吗?

1 个答案:

答案 0 :(得分:0)

尝试一下:

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");
        }
    }