Java BitmapFactory.decodeFile NullPointerException

Java BitmapFactory.decodeFile NullPointerException,java,android,bitmap,Java,Android,Bitmap,我只从安卓8.0版本的华硕ZenFone 3(ZE552KL)(华硕Z012D)中得到这个错误 java.lang.NullPointerException: 位于android.graphics.Bitmap.createScaledBitmap(Bitmap.java:714) 有人遇到过同样的问题吗?试试这个: public void showImage(string filename) { analytics.LocalLog ("ShowImage");

我只从安卓8.0版本的华硕ZenFone 3(ZE552KL)(华硕Z012D)中得到这个错误

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

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

试试这个:

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

你检查过BitmapFactory.decodeFile(…)没有返回空值吗?@Tyler V,问题可能是cellfone离我7000公里,我没有华硕ZenFone 3。png文件是正常的,并且在适当的位置。在另一篇文章中,我看到使用R.drawable.imgagename作为整数而不是保存路径解决了问题,但在我的例子中,图像是从服务器下载的。您可以在单独的步骤中返回它,并在使用它之前至少检查空值。那你就需要一个好的退路了。抱歉,我不知道具体是什么原因导致了它,可能是各种原因。这不是java。
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");
        }
    }