я пытаюсь получить imageview
из drawable и изменить его размер, мой текущий код работает на всех устройствах, кроме устройств 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, 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
Utils.scaleDown()
возвращаетnull
. - person CommonsWare   schedule 08.02.2021Utils.scaleDown()
и отладить, почему он возвращаетnull
на устройствах Oppo. - person c0delama   schedule 08.02.2021getApplicationContext()
вместо (или вместо)Shared.context
. Вполне может быть, что используемый вами контекст недоступен на этих конкретных устройствах. - person c0delama   schedule 08.02.2021