2014-03-24 56 views
2

我正在嘗試爲我的GCM推送通知加載動態大圖標。但它總是返回null。這是我的代碼。 BitmapFactory.decode()總是返回null。我檢查了這個代碼來獲取圖像視圖的一些圖標,並在這種情況下工作正常,但在我的情況下。android - GCM通知動態大圖標無法正常工作

NotificationCompat.Builder mBuilder = 
        new NotificationCompat.Builder(this) 
        .setSmallIcon(R.drawable.ic_launcher) 
        .setLargeIcon(downloadBitmap(url)) 
        .setContentTitle(context.getString(R.string.app_name)) 
        .setContentText(message); 


static public Bitmap downloadBitmap(String url) { 
     final AndroidHttpClient client = AndroidHttpClient.newInstance("Android"); 
     final HttpGet getRequest = new HttpGet("http://files.softicons.com/download/system-icons/crystal-project-icons-by-everaldo-coelho/png/22x22/apps/skype.png"); 
     Bitmap bitmap = null; 
     try { 
      HttpResponse response = client.execute(getRequest); 
      final int statusCode = response.getStatusLine().getStatusCode(); 
      if (statusCode != HttpStatus.SC_OK) { 
       Log.w("ImageDownloader", "Error " + statusCode + " while retrieving bitmap from " + url); 
       return null; 
      } 

      final HttpEntity entity = response.getEntity(); 
      if (entity != null) { 
       InputStream inputStream = null; 
       try { 
        inputStream = entity.getContent(); 
        bitmap = BitmapFactory.decodeStream(new FlushedInputStream(inputStream)); 
        return bitmap; 
       } finally { 
        if (inputStream != null) { 
         inputStream.close(); 
        } 
        entity.consumeContent(); 
       } 
      } 
     } catch (Exception e) { 
      // Could provide a more explicit error message for IOException or IllegalStateException 
      getRequest.abort(); 
      Log.w("ImageDownloader", "Error while retrieving bitmap from " + url + e.toString()); 
     } finally { 
      if (client != null) { 
       client.close(); 
      } 
     } 
     return bitmap; 
    } 

    static class FlushedInputStream extends FilterInputStream { 
     public FlushedInputStream(InputStream inputStream) { 
      super(inputStream); 
     } 

     @Override 
     public long skip(long n) throws IOException { 
      long totalBytesSkipped = 0L; 
      while (totalBytesSkipped < n) { 
       long bytesSkipped = in.skip(n - totalBytesSkipped); 
       if (bytesSkipped == 0L) { 
         int num_byte = read(); 
         if (num_byte < 0) { 
          break; // we reached EOF 
         } else { 
          bytesSkipped = 1; // we read one byte 
         } 
       } 
       totalBytesSkipped += bytesSkipped; 
      } 
      return totalBytesSkipped; 
     } 
    } 

回答

0

我得到它的工作...這是一個與Android模擬器的問題。我正在使用模擬器2.2。正如我將它切換到更新版本時,它開始工作