2013-03-13 106 views
1

我试图从URL下载图像。图像类型是PNG,分辨率是400x400像素。无法解码图像文件中的位图

这里是下载代码片段。

Bitmap bitmap=null; 
URL imageUrl = new URL(url); 
conn = (HttpURLConnection)imageUrl.openConnection(); 
conn.setConnectTimeout(30000); 
conn.setReadTimeout(30000); 
conn.setInstanceFollowRedirects(true); 
InputStream ins=conn.getInputStream(); 
os = new FileOutputStream(f); 
Utilities.getUtilities().copyStream(ins, os); 
os.flush(); 

Log.i(TAG_NAME, "file size : "+ f.length()); 
Log.i(TAG_NAME, "file exists in cache? " + f.exists()); 
bitmap = decodeFile(f); 
return bitmap; 

这是文件编写器。

public void copyStream(InputStream is, OutputStream os) { 
    final int buffer_size=1024; 
    try 
    { 
     byte[] bytes=new byte[buffer_size]; 
     for(;;) 
     { 
      int count=is.read(bytes, 0, buffer_size); 
      if(count==-1) 
       break; 
      os.write(bytes, 0, count); 
     } 
    } 
    catch(Exception ex){ 
     ex.printStackTrace(); 
    } 
} 

而且解码方法

private Bitmap decodeFile(File f){ 
    //decode image size 
    BitmapFactory.Options o = new BitmapFactory.Options(); 
    o.inJustDecodeBounds = true; 
    try { 
    BitmapFactory.decodeStream(new FileInputStream(f)); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

    final int REQUIRED_SIZE = 400; //for testing, it is set to b a constant 
    System.out.println("REQUIRED_SIZE >>> " + REQUIRED_SIZE); 
    int width_tmp=o.outWidth, height_tmp=o.outHeight; 
    int scale=1; 
    while(true){ 
     if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE) 
      break; 
     width_tmp/=2; 
     height_tmp/=2; 
     scale*=2; 
    } 

    //decode with inSampleSize 
    BitmapFactory.Options o2 = new BitmapFactory.Options(); 
    //o2.inJustDecodeBounds = true; 
    o2.inPreferredConfig = Bitmap.Config.ARGB_8888; 
    o2.inSampleSize=scale; //scale is set off since android:src automatically scales the image to fit the screen 

    try { 
     return BitmapFactory.decodeStream(new FileInputStream(f)); 
    } catch (FileNotFoundException e) { 
     e.printStackTrace(); 
    } 

} 

我可以看到,文件中的设备存在。但是,解码流失败。我花了数小时在互联网上搜索;尝试几乎所有的东西,没有成功,几乎滚滚而来。

解码流导致以下错误。

SkImageDecoder::Factory returned null 

你发现了什么在这里失去了?

编辑:

问题现在已经得到解决。服务器期待cookie的详细信息,我没有附上。花了将近一天,在灌木丛周围跳动:-)

感谢所有的宝贵意见!

+0

任何错误或只是回报空值?如果错误,发布logcat信息。 – 2013-03-13 14:42:57

+0

对不起!糟糕,编辑问题! – Renjith 2013-03-13 14:45:21

+0

希望你已经尝试过提到的解决方案[这里](http://stackoverflow.com/questions/12006785/android-skimagedecoder-factory-returned-null) – PCoder 2013-03-13 14:50:50

回答

0

IMO,你可能想重新评估httpurlconn vs本地httpclient实施的优点。 Android /谷歌去httpurlconn,但许多人选择更好地控制网络协议周围的低级细节。

这是sample async httpclient封装在位图处理程序中。您可以使用影响bmp尺寸的规则轻松扩展样本method=processBitmapEntity()

样品getbitmap网址:

public int getBitmap(String mediaurl, int ctr){ 

      Handler handler = new Handler() { 
       public void handleMessage(Message message) { 
       switch (message.what) { 
       case HttpConnection.DID_START: { 
        Log.d(TAG, "Starting connection..."); 
        break; 
       } 
       case HttpConnection.DID_SUCCEED: { 
        //message obj is type bitmap 
        Log.d(TAG, "OK bmpARRAY " +message.arg1); 
        Bitmap response = (Bitmap) message.obj; 
        break; 
       } 
       case HttpConnection.DID_ERROR: { 

        Exception e = (Exception) message.obj; 
        e.printStackTrace(); 
        Log.d(TAG, "Connection failed."); 
        break; 
       } 
       } 
      } 
      };   
      new HttpConnection(handler, PreferenceManager.getDefaultSharedPreferences(this), ctr).bitmap(mediaurl); 

     return -1; 

而且在HttpConnection的类的位图处理程序,它上面的链接样本的一部分:

private void processBitmapEntity(HttpEntity entity) throws IOException { 
    BufferedHttpEntity bufHttpEntity = new BufferedHttpEntity(entity); 
    Bitmap bm = BitmapFactory.decodeStream(bufHttpEntity.getContent()); 
    handler.sendMessage(Message.obtain(handler, DID_SUCCEED, bm)); 
} 

而一个git project