我正在尝试在WallpaperManager
中使用ViewPager
。我有一个按钮,可以将ViewPager中的当前图像设置为壁纸。我的问题来自代码行wallpManager.setResource(newInt);
...它出现的整数总是0(零),因此应用程序崩溃,LogCat说ID#0x0没有资源。作为一个测试,看看我是否得到了正确的图像URL我已经这样做了:
String newStr = images[position];
CharSequence cs = newStr;
Toast.makeText(UILPager.this, cs, Toast.LENGTH_SHORT).show();
生成的Toast显示正确的图片网址。我无法弄清楚如何将“http://www.example.com/image.jpg”形式的URL转换为整数,以便WallpaperManager可以使用它。这是整个按钮代码的样子:
wallp_BTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
WallpaperManager wallpManager = WallpaperManager.getInstance(getApplicationContext());
String newStr = images[position];
int newInt = 0;
try{
newInt = Integer.parseInt(newStr);
} catch(NumberFormatException nfe) {
}
CharSequence cs = newStr;
try {
wallpManager.setResource(newInt);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
Toast.makeText(UILPager.this, cs, Toast.LENGTH_SHORT).show();
}
});
答案 0 :(得分:4)
从网址设置壁纸
try {
URL url = new URL("http://developer.android.com/assets/images/dac_logo.png");
Bitmap bitmap = BitmapFactory.decodeStream(url.openStream());
wallpaperManager.setBitmap(bitmap);
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
享受=)
答案 1 :(得分:3)
方法wallpaperManager.setResource()需要应用程序的资源ID。示例:我带有id" myImage"的ImageView然后调用该方法将看起来像wallpaperManager.setResource(R.id.myImage)
。
在您的情况下,您的身份证无效。
答案 2 :(得分:0)
您应该使用wallpManager.setResource(0)
代替wallpManager.setResource(R.drawable.yourimage)
,因为它需要drawable
而您的应用中没有{id = 0。
在您的代码中
String newStr = images[position];
int newInt = 0;
try{
newInt = Integer.parseInt(newStr);
} catch(NumberFormatException nfe) {
}
由于newStr
永远不是一个数字,因此始终是一个网址,因此始终会抓住NumberFormatException
。因此,newInt
的值始终初始化为0.因此错误。
答案 3 :(得分:0)
我意识到在将其设置为壁纸之前我必须下载图像!感谢您的帮助AndroidWarrior和Owl。当我得到下载代码时,我会发布它。猫头鹰向我展示了如何下载壁纸:
//--- Wallpaper button
wallp_BTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
try {
vpURL = new URL(images[position]);
} catch (MalformedURLException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
WallpaperManager wallpManager = WallpaperManager.getInstance(getApplicationContext());
try {
Bitmap bitmap = BitmapFactory.decodeStream(vpURL.openStream());
wallpManager.setBitmap(bitmap);
Toast.makeText(UILPager.this, "The wallpaper has been set!", Toast.LENGTH_LONG).show();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
});
//--- END Wallpaper button
...我也想出了如何下载ViewPager
图片(我需要在我的应用的不同区域同时进行):
//--- Wallpaper button
wallp_BTN.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
String vpURLStr = images[position];
GetVPImageTask downloadVPImageTask = new GetVPImageTask();
downloadVPImageTask.execute(new String[] { vpURLStr });
}
});
//--- END Wallpaper button
//--- Download ViewPager Image AsyncTask
private class GetVPImageTask extends AsyncTask<String, Void, Bitmap> {
ProgressDialog getVPImageDia;
@Override
protected void onPreExecute() {
super.onPreExecute();
getVPImageDia = new ProgressDialog(UILNPPager.this);
getVPImageDia.setMessage("Grabbing the image...");
getVPImageDia.setIndeterminate(false);
getVPImageDia.show();
}
@Override
protected Bitmap doInBackground(String... urls) {
Bitmap map = null;
for (String url : urls) {
map = downloadImage(url);
}
return map;
}
// Sets the Bitmap returned by doInBackground
@Override
protected void onPostExecute(Bitmap result) {
try {
getVPImageDia.dismiss();
getVPImageDia = null;
} catch (Exception e) {
// nothing
}
Toast.makeText(UILNPPager.this, "The image be Downloaded!", Toast.LENGTH_SHORT).show();
//downloaded_iv.setImageBitmap(result);
}
// Creates Bitmap from InputStream and returns it
private Bitmap downloadImage(String url) {
Bitmap bitmap = null;
InputStream stream = null;
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inSampleSize = 1;
try {
stream = getHttpConnection(url);
bitmap = BitmapFactory.
decodeStream(stream, null, bmOptions);
stream.close();
} catch (IOException e1) {
e1.printStackTrace();
}
return bitmap;
}
// Makes HttpURLConnection and returns InputStream
private InputStream getHttpConnection(String urlString)
throws IOException {
InputStream stream = null;
URL url = new URL(urlString);
URLConnection connection = url.openConnection();
try {
HttpURLConnection httpConnection = (HttpURLConnection) connection;
httpConnection.setRequestMethod("GET");
httpConnection.connect();
if (httpConnection.getResponseCode() == HttpURLConnection.HTTP_OK) {
stream = httpConnection.getInputStream();
}
} catch (Exception ex) {
ex.printStackTrace();
}
return stream;
}
}
//--- END Download ViewPager Image AsyncTask