BitmapFactory在.png图像上完美运行,但是当我从url加载jpeg图像时返回null。
BitmapFactory.decodeStream( (InputStream) new URL(URL).getContent(), null, options);
我使用此代码但是当我提供jpeg图像url时,它返回null。
我该怎么办?
答案 0 :(得分:1)
尝试这种方式可能它适用于我的.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;
}
答案 1 :(得分:0)
检查您的jpg是否设置了颜色配置文件。 由于这个原因,Android位图解码器失败了。 可悲的是,我没有时间或资源为此找到解决方案。
要知道jpg中是否设置了颜色配置文件,请尝试使用photoshop打开它,如果有,应该提示您询问如何处理配置文件。
答案 2 :(得分:0)
URL唯一标识Web上的资源。测试网址的好方法是使用浏览器。我不知道新的URL(URL)应该做什么。 URL采用字符串参数但其他构造函数可用请参阅https://docs.oracle.com/javase/7/docs/api/java/net/URL.html如果将字符串传递给构造函数,则可能需要为字符串赋值,或者只使用文字字符串示例新URL(" google。 com")至少你会得到除空指针之外的东西。我无法知道您的图像是什么网址,因为网络是一个很大的地方。