gpt4 book ai didi

java - 将 imageView 的内容设置为墙纸(Picasso)

转载 作者:搜寻专家 更新时间:2023-11-01 08:46:29 24 4
gpt4 key购买 nike

我正在为我的应用程序使用 picasso 库,到目前为止它运行良好。我有两个 Activity ,一个显示所有图像,另一个显示一个图像,当我在第一个 Activity 中单击它时。图像被加载到 imageView 中。现在我想将此 imageView 的内容设置为我的主屏幕壁纸。到目前为止我有这个:

if (id == R.id.set_wall) {


Bitmap mBitmap = BitmapFactory.decodeResource(getResources(), R.id.image);
WallpaperManager myWallpaperManager = WallpaperManager
.getInstance(getApplicationContext());

try {
myWallpaperManager.setBitmap(mBitmap);
Toast.makeText(DetailActivity.this, "Wallpaper set",
Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(DetailActivity.this,
"Error setting wallpaper", Toast.LENGTH_SHORT)
.show();
}
return true;


}

但这给了我这个错误:

 java.lang.NullPointerException: Attempt to invoke virtual method 'boolean android.graphics.Bitmap.compress(android.graphics.Bitmap$CompressFormat, int, java.io.OutputStream)'  on a null object reference

有谁知道如何将此 imageView 的内容设置为我的墙纸?

非常感谢!!

最佳答案

替换 位图 mBitmap = BitmapFactory.decodeResource(getResources(), R.id.image);

通过 位图 mBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.image);

如果你没有 Drawable 那么,

从imageview获取位图的代码

Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();

关于java - 将 imageView 的内容设置为墙纸(Picasso),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27767299/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com