java - BitmapFactory 在 .png 图像上完美工作,但当我从 url 加载 jpeg 图像时返回 null

标签 java android

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/

相关文章:

android - Android BroadcastReceiver、ContentProvider 和 Activity 之间的数据流?

java - Spring 值注入(inject) - 如果没有环境变量,则默认为文件中的系统属性

java - eclipse调试器不起作用的一个问题

java - 使我的持久模型中的属性独一无二

java - 执行 jps 命令时的额外内容

android - 根据用户偏好加载资源

java - 如何在 Spring Data JDBC 中的 CrudRepository 中的查询中引用实体?

android - 为什么 `android:screenOrientation="后面"` 在 android 4.1.2 中没有效果?

android - 在 Android 中使用 Google Maps API v2 添加图像

Android:到本地主机网站的 HTTP 连接