2013-10-29 81 views
0

我已經使用DecodeUrl()解碼了url的圖像,函數返回E_SUCCESS,但日後顯示「HttpTransaction [0]已經關閉」。如果DecodeUrl()成功,那麼它也應該調用OnImageDecodeUrlReceived(),這也沒有發生。我已繼承IImageDecodeUrlEventListener,給予HTTP權限的應用程序,並驗證了鏈接,但不明白爲什麼日誌顯示「HttpTransaction已關閉」,並且函數OnImageDecodeUrlReceived()沒有被調用。如何顯示圖片url的位圖?

回答

1
String path = L"http://www.test.gr/images/23101212121.png"; 
Image* pImage = new Image(); 
pImage->Construct(); 
// Set a URL 
Uri uri; 
RequestId reqId; 
uri.SetUri(path); 
// Choose the bitmap pixel format 
BitmapPixelFormat format; 
if(path.EndsWith(L"jpg") or path.EndsWith(L"bmp") or path.EndsWith(L"gif")) 
{ 
    format = BITMAP_PIXEL_FORMAT_RGB565; 
} 
else if(path.EndsWith(L"png")) 
{ 
    format = BITMAP_PIXEL_FORMAT_ARGB8888; 
} 
// Request image 
pImage->DecodeUrl(uri, format, 224, 127, reqId, *this, 5000); 

請點擊此鏈接,使請求成功 link

您可以在Tizen與下面的工具的幫助下運行的bada項目

See here

0

非常快速的方法:

private Bitmap getBitmap(String url) 
    { 
     File f=fileCache.getFile(url); 

     //from SD cache 
     Bitmap b = decodeFile(f); 
     if(b!=null) 
      return b; 

     //from web 
     try { 
      Bitmap bitmap=null; 
      URL imageUrl = new URL(url); 
      HttpURLConnection conn = (HttpURLConnection)imageUrl.openConnection(); 
      conn.setConnectTimeout(30000); 
      conn.setReadTimeout(30000); 
      conn.setInstanceFollowRedirects(true); 
      InputStream is=conn.getInputStream(); 
      OutputStream os = new FileOutputStream(f); 
      Utils.CopyStream(is, os); 
      os.close(); 
      bitmap = decodeFile(f); 
      return bitmap; 
     } catch (Exception ex){ 
      ex.printStackTrace(); 
      return null; 
     } 
    } 

    //decodes image and scales it to reduce memory consumption 
    private Bitmap decodeFile(File f){ 
     try { 
      //decode image size 
      BitmapFactory.Options o = new BitmapFactory.Options(); 
      o.inJustDecodeBounds = true; 
      BitmapFactory.decodeStream(new FileInputStream(f),null,o); 

      //Find the correct scale value. It should be the power of 2. 
      final int REQUIRED_SIZE=70; 
      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.inSampleSize=scale; 
      return BitmapFactory.decodeStream(new FileInputStream(f), null, o2); 
     } catch (FileNotFoundException e) {} 
     return null; 
    }