NullPointerException 无法从 oppo 设备上的 drawable 中找到位图

如何解决NullPointerException 无法从 oppo 设备上的 drawable 中找到位图

我试图从 drawable 中获取 imageview 并调整它的大小我当前的代码适用于所有设备,除了 oppo 设备它在 crop() 方法内崩溃

我遇到的崩溃是在crop()方法的第一行,你可以在下面找到

Caused by: java.lang.NullPointerException: 

我目前的代码是:

public Bitmap getTileBitmap(int id,int size) {
    String string = tileUrls.get(id);
    if (string.contains(Themes.URI_DRAWABLE)) {
        String drawableResourceName = string.substring(Themes.URI_DRAWABLE.length());
        int drawableResourceId = Shared.context.getResources().getIdentifier(drawableResourceName,"drawable",Shared.context.getPackageName());
        Bitmap bitmap = Utils.scaleDown(drawableResourceId,size,size);
        return Utils.crop(bitmap,size);

    }
    return null;
}


public static Bitmap crop(Bitmap source,int newHeight,int newWidth) {
    int sourceWidth = source.getWidth();
    int sourceHeight = source.getHeight();
    float xScale = (float) newWidth / sourceWidth;
    float yScale = (float) newHeight / sourceHeight;
    float scale = Math.max(xScale,yScale);
    float scaledWidth = scale * sourceWidth;
    float scaledHeight = scale * sourceHeight;
    float left = (newWidth - scaledWidth) / 2;
    float top = (newHeight - scaledHeight) / 2;
    RectF targetRect = new RectF(left,top,left + scaledWidth,top + scaledHeight);
    Bitmap dest = Bitmap.createBitmap(newWidth,newHeight,source.getConfig());
    Canvas canvas = new Canvas(dest);
    canvas.drawBitmap(source,null,targetRect,null);

    return dest;
}

编辑:添加了可能导致问题的缩放方法

public static Bitmap scaleDown(int resource,int reqWidth,int reqHeight) {
    BitmapFactory.Options options = new BitmapFactory.Options();
    options.inJustDecodeBounds = true;
    BitmapFactory.decodeResource(Shared.context.getResources(),resource);

    // Calculate inSampleSize
    options.inSampleSize = calculateInSampleSize(options,reqWidth,reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    return BitmapFactory.decodeResource(Shared.context.getResources(),resource,options);
}

到目前为止,我不确定是什么导致了仅在 oppo 设备上崩溃

解决方法

试试这个:

options.inJustDecodeBounds = true;
options.inScaled = false;
options.inDensity = 0;
options.inMutable = true; //API 11. Pass to canvas? Might crash without this.
//Load the image here... BitmapFactory.decodeResource()...

... //Some calculations.

options.inJustDecodeBounds = false;
options.inPreferredConfig = Bitmap.Config.ARGB_8888;

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?
Java在半透明框架/面板/组件上重新绘画。
Java“ Class.forName()”和“ Class.forName()。newInstance()”之间有什么区别?
在此环境中不提供编译器。也许是在JRE而不是JDK上运行?
Java用相同的方法在一个类中实现两个接口。哪种接口方法被覆盖?
Java 什么是Runtime.getRuntime()。totalMemory()和freeMemory()?
java.library.path中的java.lang.UnsatisfiedLinkError否*****。dll
JavaFX“位置是必需的。” 即使在同一包装中
Java 导入两个具有相同名称的类。怎么处理?
Java 是否应该在HttpServletResponse.getOutputStream()/。getWriter()上调用.close()?
Java RegEx元字符(。)和普通点?