BitmapFactory 在 .png 图像上完美工作,但当我从 url 加载 jpeg 图像时返回 null。
BitmapFactory.decodeStream( (InputStream) new URL(URL).getContent(), null, options);
我使用此代码,但当我提供 jpeg 图像 url 时,它返回 null。
我应该做什么?
最佳答案
尝试这种方式可能会有效,它对我来说适用于 .jpg 图像
Bitmap bitmaps = LoadImage(strMainImageLink, new BitmapFactory.Options()); where **LoadImage** method is as shown below
LoadImage方法
private Bitmap LoadImage(String URL, BitmapFactory.Options options)
{
Bitmap bitmap = null;
InputStream in = null;
try {
// in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(OpenHttpConnection(URL), null, options);
// in.close();
} catch (IOException e1) {
Log.e("erererere", e1.toString());
}
return bitmap;
}
private InputStream OpenHttpConnection(String strURL) throws IOException{
InputStream inputStream = null;
URL url = new URL(strURL);
URLConnection conn = url.openConnection();
try{
HttpURLConnection httpConn = (HttpURLConnection)conn;
httpConn.setRequestMethod("GET");
httpConn.connect();
if (httpConn.getResponseCode() == HttpURLConnection.HTTP_OK) {
inputStream = httpConn.getInputStream();
}
}
catch (Exception ex)
{
Log.e("error",ex.toString());
}
return inputStream;
}
关于java - BitmapFactory 在 .png 图像上完美工作,但当我从 url 加载 jpeg 图像时返回 null,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12245255/