在空对象引用上的Bitmap.getWidth()' [英] Bitmap.getWidth()' on a null object reference
问题描述
我刚刚在CreateScaledBitmap行上遇到了这个问题,我试图将此图像设置为设备的墙纸,并且需要将该图像缩放到设备上,这就是为什么我要使用此方法,但是不幸的是我无法修复此Bitmap宽度()错误
I just got into this problem on the line CreateScaledBitmap, I am trying to set this image as device's wallpaper and I need to scale this image to the device, thats why I am doing this method but unfortunately I cant fix this Bitmap width() error
setWall.setOnClickListener(new View.OnClickListener() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onClick(View view) {
Picasso.with(getApplicationContext()).load(imageBrought).into(new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
bitmap = Bitmap.createScaledBitmap(bitmap, width, height, true);
WallpaperManager wallpaperManager = WallpaperManager.getInstance(AppMomentSelected.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
try {
wallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
@Override
public void onBitmapFailed(Drawable errorDrawable) {
}
@Override
public void onPrepareLoad(Drawable placeHolderDrawable) {
}
});
推荐答案
您从Firebase
中获取了图像的Url
,但是用于从Url
中获取Bitmap
的方法效率不高,并且可能无法实现.您需要做的简单事情是使用一些自定义library
来下载图像,例如毕加索
http://square.github.io/picasso/
You have Url
of an image from your Firebase
but approach you use to get Bitmap
from Url
is not efficient and probably not possible. Simple thing you need to do is to use some custom library
for downloading images for example Picasso
http://square.github.io/picasso/
添加到您的app gradle
:compile 'com.squareup.picasso:picasso:2.5.2'
现在您可以使用Picasso
从Url
下载图像并转换为Bitmap
:
And now you can use Picasso
to download image from Url
and convert to Bitmap
:
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
int height = metrics.heightPixels;
int width = metrics.widthPixels;
WallpaperManager wallpaperManager = WallpaperManager.getInstance(AppMomentSelected.this);
wallpaperManager.setWallpaperOffsetSteps(1, 1);
wallpaperManager.suggestDesiredDimensions(width, height);
Picasso.with(this)
.load(imageBrought)
.resize(width, height)
.into(new Target() {
@Override
public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from) {
/* Save the bitmap or do something with it here */
wallpaperManager.setBitmap(bitmap);
}
});
这篇关于在空对象引用上的Bitmap.getWidth()'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!