【问题标题】:Set image as wallpaper from url (Glide + json)从 url 将图像设置为墙纸(Glide + json)
【发布时间】:2018-12-29 02:50:44
【问题描述】:

app app

您好,提前感谢指导我的人。

我在设置墙纸时遇到问题,当我点击按钮时,出现以下错误:

2018-12-28 22:36:02.801 13030-13030 /? E / AndroidRuntime:致命例外:主要 java.lang.NullPointerException:尝试在空对象引用上调用虚拟方法 'boolean android.graphics.Bitmap.compress (android.graphics.Bitmap $ CompressFormat, int, java.io.OutputStream)'

我保留使用的文件。

<?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>

公共类 infoanimales 扩展 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();
            }
        }

    }

使用此功能,它可以完美运行,但是将图像放在可绘制文件夹中,以及您想要什么或获取json url的图像,用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();
        }
    }

总之,我需要的是当你点击按钮时,显示的图片设置为壁纸

【问题讨论】:

    标签: java android set android-glide wallpaper


    【解决方案1】:

    正如我所见,您没有使用完整路径来解码位图。您需要获取完整的路径名,例如:

    String uri =  Environment.getExternalStorageDirectory().toString() + "/" + PHOTO_DIR  + "/test.jpg";
    

    之后:

    Bitmap bitmap = BitmapFactory.decodeFile(uri);
    

    Reference

    【讨论】:

    • 嗨,艾库特。我进行了更改,当我单击按钮时,应用程序关闭,日志显示以下内容: java.lang.NullPointerException: on a null object reference com.vinotintoplayer.wallpaperhd4k.infoanimales.setWallpaper(infoanimales.java:87 ) 尝试 { manager.setBitmap(bitmap);
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多