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 详细信息。花了将近一天的时间,在灌木丛中跳来跳去:-)

感谢大家的宝贵意见!

4

1 回答 1

0

IMO,您可能需要重新评估 httpurlconn 与本机httpclient实现的优点。Android/google 选择 httpurlconn,但许多人选择更好地控制网络协议的底层细节。

这是包含在位图处理程序中的示例异步httpclient。method=processBitmapEntity()您可以使用影响 bmp 大小的规则轻松扩展示例。

示例 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 项目

于 2013-03-13T15:21:22.000 回答