java - Set image as wallpaper from url (Glide + json)
问题描述
Hi, thanks in advance to those who guide me.
I have a problem with the Set Wallpaper, that when I clicked on the button, I get the following error:
2018-12-28 22: 36: 02.801 13030-13030 /? E / AndroidRuntime: FATAL EXCEPTION: main 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
I leave the files used.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical"
android:id="@+id/thumbnail2"
android:padding="5dp">
<TextView
android:id="@+id/txtclose"
android:layout_width="30dp"
android:layout_height="30dp"
android:layout_gravity="end"
android:layout_marginTop="5dp"
android:layout_marginEnd="5dp"
android:layout_marginRight="5dp"
android:layout_marginBottom="5dp"
android:background="@drawable/circulo"
android:gravity="center"
android:text="@string/equis"
android:textColor="@android:color/background_light"
android:textStyle="bold" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="center"
android:orientation="vertical">
<FrameLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_gravity="end">
<Button
android:id="@+id/btn"
android:layout_width="159dp"
android:layout_height="wrap_content"
android:layout_gravity="bottom|center"
android:layout_marginBottom="25dp"
android:background="@drawable/borde_redondo"
android:text="Establecer como Fondo de Pantalla"
android:textColor="#ffffff" />
</FrameLayout>
</LinearLayout>
</LinearLayout>
public class infoanimales extends AppCompatActivity {
private RequestOptions options;
TextView txtclose;
LinearLayout img;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_infoanimales);
Button button = findViewById(R.id.btn);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
setWallpaper();
}
});
if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.O) {
setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
}
this.options = new RequestOptions()
.diskCacheStrategy(DiskCacheStrategy.AUTOMATIC);
String image_url = getIntent().getExtras().getString("img2");
img = findViewById(R.id.thumbnail2);
Glide.with(this).load(image_url).into(new SimpleTarget<Drawable>() {
@Override
public void onResourceReady(@NonNull Drawable fondoreceta, Transition<? super Drawable> transition) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
img.setBackground(fondoreceta);
}
}
});
TextView txtclose = findViewById(R.id.txtclose);
txtclose.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
finish();
}
});
}
private void setWallpaper() {
Bitmap bitmap = BitmapFactory.decodeFile("img2");
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
try {
manager.setBitmap(bitmap);
Toast.makeText(this, "Listo", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}
}
}
With this function, it works perfect, but that is having the images in the drawable folder, and what you want or what it is that you take the image of the json url, traide with glide
private void setWallpaper() {
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.cochinito);
WallpaperManager manager = WallpaperManager.getInstance(getApplicationContext());
try {
manager.setBitmap(bitmap);
Toast.makeText(this, "Listo", Toast.LENGTH_SHORT).show();
} catch (IOException e) {
Toast.makeText(this, "error", Toast.LENGTH_SHORT).show();
}
}
In conclusion, what I need is that when you click on the button, the displayed image is set as wallpaper
解决方案
如我所见,您没有使用完整路径来解码位图。您需要获取完整的路径名,例如:
String uri = Environment.getExternalStorageDirectory().toString() + "/" + PHOTO_DIR + "/test.jpg";
在那之后:
Bitmap bitmap = BitmapFactory.decodeFile(uri);
推荐阅读
- curl - 读取命令在脚本中抛出错误的变量名错误,但不是来自终端
- c# - Selenium c# xpath 试图获得第二个 td
- llvm - 如何使用 LLVM C++ API 进行类型检查?
- git - 两个分支之间常见提交的 Git 列表
- sql-server - SQL Server 在地理列上返回错误
- html - 背景图片没有出现在网页上
- hive - Hive 中复杂数据类型的使用
- css - 如何从 sass mixin 循环内容指令?
- powershell - 删除模块也会尝试删除 PackageManagement
- c# - 有没有办法根据 C# 对象的属性自动形成长的 MySql INSERT 命令?