【发布时间】:2014-03-23 07:28:11
【问题描述】:
我的项目中有一个 Imageview 和一个 Button,我希望该按钮将图像设置为主屏幕背景,但出现错误“imageView”无法在“Bitmap bitmap=((BitmapDrawable)imageView.getDrawable ()).getBitmap();"
package com.example.androidhive;
import java.io.IOException;
import android.app.Activity;
import android.app.WallpaperManager;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class FullImageActivity extends Activity {
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.full_image);
// get intent data
Intent i = getIntent();
// Selected image id
int position = i.getExtras().getInt("id");
ImageAdapter imageAdapter = new ImageAdapter(this);
ImageView imageView = (ImageView) findViewById(R.id.full_image_view);
imageView.setImageResource(imageAdapter.mThumbIds[position]);
}
public void setaswall(View view) { // SET AS WALLPAPER BUTTON
// TODO Auto-generated method stub
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
Bitmap bitmap=((BitmapDrawable)imageView.getDrawable()).getBitmap();
if(bitmap!=null)
myWallpaperManager.setBitmap(bitmap);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
public void setaswall(View view) { // SET AS WALLPAPER BUTTON
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
};
}
【问题讨论】: