简体   繁体   中英

How can i solve ResourceType problem in android?

I am developing an application that shows a image and when it's button is pressed then the image should display in the wallpaper of the phone. but in j code a error(ResourceType) shows in the mainActivity. error -expected resource type of raw Here are java code

 public void click(View view){
        Button button =(Button) findViewById(R.id.button);
        final ImageView imageView =(ImageView) findViewById(R.id.de);
        imageView.setImageResource(R.drawable.de);
        button.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                WallpaperManager myWallpaper =WallpaperManager.getInstance(getApplicationContext());
                try {
                    myWallpaper.setResource(R.drawable.de);
                }catch (IOException e){
                    e.printStackTrace();
                }

            }
        });

    }
layout file

   <Button
        android:id="@+id/button"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:text="Setimage"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ImageView
        android:id="@+id/de"
        android:layout_width="238dp"
        android:layout_height="273dp"
        android:layout_marginStart="8dp"
        android:layout_marginTop="8dp"
        android:layout_marginEnd="8dp"
        android:layout_marginBottom="8dp"
        android:src="@drawable/de"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toBottomOf="@+id/button" />
</android.support.constraint.ConstraintLayout>

change

imageView.setImageResource(R.drawable.de);

to

imageView.setImageDrawable(R.drawable.de);

or

imageView.setImageResource(R.raw.de);

you need to create a raw file in res directory of you project and put your image there.

res/raw/de.jpg

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM