我正在尝试从网址下载图片并将其转换为位图
相应的代码是:
Bitmap pinbit=ImageDownloader.DownloadImage(URL_image+s.category+".png");
pinbit=Bitmap.createScaledBitmap(pinbit,75,56,false);
marker.icon(BitmapDescriptorFactory.fromBitmap(pinbit));
我用来下载图片的课程是:
package com.igloo.classes;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
public class ImageDownloader {
public static InputStream OpenHttpConnection(String urlString)
throws IOException
{
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try{
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
}
catch (Exception ex)
{
throw new IOException("Error connecting");
}
return in;
}
public static Bitmap DownloadImage(String URL)
{
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
该代码适用于像Android 2.6这样的旧设备,但是当我在新的三星平板电脑上运行时,该行:
Bitmap pinbit=ImageDownloader.DownloadImage(URL_image+s.category+".png");'
显示NullPointerException
。请帮忙!