[英]BitmapFactory works perfectly on .png image but returns null when i load jpeg image from url
BitmapFactory works perfectly on .png image but returns null when I load jpeg image from url. BitmapFactory在.png图像上可以完美工作,但是当我从url加载jpeg图像时返回null。
BitmapFactory.decodeStream( (InputStream) new URL(URL).getContent(), null, options);
I use this code but when I provide a jpeg image url it returns null. 我使用此代码,但是当我提供jpeg图片网址时,它将返回null。
What should I do? 我该怎么办?
try this way may it works it works for me for .jpg image 尝试这种方式可能对我.jpg图片有效
Bitmap bitmaps = LoadImage(strMainImageLink, new BitmapFactory.Options()); where **LoadImage** method is as shown below
LoadImage method 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;
}
Check if your jpg has a color profile set in it. 检查您的jpg是否设置了颜色配置文件。 The Android bitmap decoder has failed me because of this.
因此,Android位图解码器使我失败了。 Sadly I did not have the time or resources to find a solution for this.
可悲的是,我没有时间或资源来为此找到解决方案。
To know if there is color profile set in the jpg, try to open it with photoshop, if there is it should prompt you to ask you what to do with the profile. 要知道jpg中是否设置了颜色配置文件,请尝试使用photoshop将其打开,如果有,它会提示您询问如何处理该配置文件。
A URL uniquely identifies a resource on the web. URL唯一标识网络上的资源。 A good way to test a url is with a browser.
浏览器是测试网址的一种好方法。 I have no idea what new URL(URL) is supposed to do.
我不知道新的URL(URL)应该做什么。 URL takes a string argument but other constructors are available see https://docs.oracle.com/javase/7/docs/api/java/net/URL.html If you pass a string to a constructor you probably want to assign a value to the string or just use a literal string example new URL("google.com") at least you will get something other than null pointer.
URL具有字符串参数,但其他构造函数也可用,请参见https://docs.oracle.com/javase/7/docs/api/java/net/URL.html如果将字符串传递给构造函数,则可能要分配一个值或仅使用文字字符串示例new URL(“ google.com”)至少会得到除空指针以外的内容。 I have no way of knowing what url your image is because the web is a big place.
我无法知道您的图片的网址是什么,因为网络很大。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.