【发布时间】:2019-04-14 02:45:16
【问题描述】:
我正在开发一个显示图像的应用程序,当按下它的按钮时,图像应该显示在手机的壁纸中。但在 j 代码中,mainActivity 中显示错误(ResourceType)。 错误 - 预期的原始资源类型 这里是java代码
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>
【问题讨论】:
-
请发布错误堆栈跟踪
-
stackoverflow.com/a/29090292/2919483 这个链接会帮助你,只需将你的图片资源转换为位图并在壁纸管理器中设置位图。 :)
标签: java android android-resources