现在已经好几天了,试着想一下这个功能是如何工作的...... 我已经在堆栈溢出中搜索了它,当然, Downloading and setting a wallpaper 尝试过,但得到doInBackground错误。 以及我尝试过的更多“似乎合法”的答案。但最后,我仍然不知道如何改变我的
final String url2 = "http://icons.iconarchive.com/icons/3xhumed/mega-games-pack-26/256/Call-of-Duty-World-at-War-5-icon.png";
displaynya.setImageUrl(url2);
在我按下按钮时使用
case R.id.BTsetWalp:
WallpaperManager myWallpaperManager
= WallpaperManager.getInstance(getApplicationContext());
try {
myWallpaperManager.setResource(// what should i do with this?);
令人困惑...... 在过去的几天里,我试图只使用已存储在应用程序中的图像,使用该案例2
case R.id.ivwall02:
displaynya.setImageResource(R.drawable.a2);
setWallp = R.drawable.a2;
break;
此代码正常运行。但如果我使用它,我的应用程序的大小将变得巨大,因为壁纸的所有巨大图像都存储在应用程序中。所以我想在案例1中使用一种方法(首先下载图像,然后将其设置为壁纸),但我卡在那里..
=============================================== ===
编辑2,到目前为止更新了我的代码
这是完整的代码
public class CopyOfGallery extends Activity implements View.OnClickListener {
private SmartImageView displaynya;
Button bWalp;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.gallery);
bWalp = (Button) findViewById(R.id.BTsetWalp);
displaynya = (SmartImageView)findViewById(R.id.iVDisplay);
ImageView image01 = (ImageView) findViewById(R.id.ivwall01);
ImageView image02 = (ImageView) findViewById(R.id.ivwall02);
image01.setOnClickListener(this);
image02.setOnClickListener(this);
bWalp.setOnClickListener(this);
}
@Override
public void onClick(View vImage) {
// TODO Auto-generated method stub
switch (vImage.getId()){
case R.id.ivwall01:
final String url1 ="http://i100.photobucket.com/albums/m21/keitaro3660/komari/Komari02/km_happy.jpg" ;
displaynya.setImageUrl(url1);
break;
case R.id.ivwall02:
final String url2 ="http://i100.photobucket.com/albums/m21/keitaro3660/komari/Komari02/km_awkward.jpg" ;
displaynya.setImageUrl(url2);
break;
case R.id.BTsetWalp:
// get the Image to as Bitmap
Bitmap bitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.id.iVDisplay));
DisplayMetrics metrics = new DisplayMetrics();
getWindowManager().getDefaultDisplay().getMetrics(metrics);
// get the height and width of screen
int height = metrics.heightPixels;
int width = metrics.widthPixels;
WallpaperManager wallpaperManager = WallpaperManager.getInstance(this);
try {
wallpaperManager.setBitmap(bitmap);
wallpaperManager.suggestDesiredDimensions(width, height);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}}
至于
Bitmap bitmap = BitmapFactory.decodeStream(getResources().openRawResource(R.id.iVDisplay));
这个函数用于获取我的R.id.iVDisplay中显示的任何图像不是??正是我需要的。但为什么会崩溃? :'(
这是我的日志
01-08 23:39:51.529:E / AndroidRuntime(22803):致命异常:主
01-08 23:39:51.529:E / AndroidRuntime(22803): 显示java.lang.NullPointerException
01-08 23:39:51.529:E / AndroidRuntime(22803):at android.content.res.Resources.openRawResource(Resources.java:951)
01-08 23:39:51.529:E / AndroidRuntime(22803):at android.content.res.Resources.openRawResource(Resources.java:927)
01-08 23:39:51.529:E / AndroidRuntime(22803):at preff.Gallery.onClick(Gallery.java:355)01-08 23:39:51.529: E / AndroidRuntime(22803):at android.view.View.performClick(View.java:3526)
01-08 23:39:51.529:E / AndroidRuntime(22803):at android.view.View $ PerformClick.run(View.java:14133)
错位或我错过了什么? :(