首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >BitmapFactory.decodeFile NullPointerException

BitmapFactory.decodeFile NullPointerException
EN

Stack Overflow用户
提问于 2018-07-04 07:25:57
回答 1查看 385关注 0票数 0

我只在安卓8.0的华硕ZenFone 3 (ZE552KL) (ASUS_Z012D)上看到这个错误

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

代码语言:javascript
复制
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);
}

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

EN

回答 1

Stack Overflow用户

发布于 2018-07-04 13:25:29

试试这个:

代码语言:javascript
复制
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");
        }
    }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/51163986

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档