2012-07-23 26 views
-1

我有代碼,我從另一個項目,將允許您傳遞一個網址,它會返回一個圖片...然後你將它設置爲圖像視圖和它將顯示圖片......但在某處......不再顯示檢索到的圖片......你們可以看看這段代碼,並告訴我是否有什麼問題?Android - 從網絡載入圖片不再工作

Click事件

void NextPic(View v) 
{ 
    picNum++; //int holding what pic number we are on 
    Drawable bPic = LoadImageFromWebOperations(picData[]); //picData = String[] with url data in it 
    imgView.setImageDrawable(bPic); //Cast earlier in the code 
} 

獲取產品圖功能

private Drawable LoadImageFromWebOperations(String url) 
{ 
    try 
    { 
     InputStream is = (InputStream) new URL(url).getContent(); 
     Drawable d = Drawable.createFromStream(is, "src name"); 
     return d; 
    } 
    catch(Exception e) 
    { 
     //Custom error handler 
     uu.SendError(e.getMessage(), "Main.GetPickTask.LoadImageFromWeboperations(String url)"); 
     return null; 
    } 
} 

logcat中顯示什麼都沒有......沒有錯誤抓到......和PIC保持上來空白......我甚至使用ic_launcher圖形作爲佔位符,以查看它是否是圖像視圖本身...但顯示了幾秒鐘,然後變黑

這是我使用的示例URL http://app.guycothal.com/Funnys/2012722133515231.jpg

+1

你是從ui線程調用它嗎?也許你的新項目是新的API級別,它不允許你在主線程上進行網絡操作。另外你的方法期待一個'String',你似乎試圖發送一個'String []'如果你在這裏粘貼的東西甚至被編譯了,我感到很驚訝。 – FoamyGuy 2012-07-23 18:27:31

+0

@Tim:它在另一個項目上工作......實際上這個項目的90%的代碼是從工作項目中複製的......唯一的變化是菜單和主題......我從一個項目中調用它'AsyncTask' ...但我發佈的代碼在'AsyncTask'之外,所以我可以完全排除 – 2012-07-23 18:36:24

+0

不知道我做了什麼......但現在它正在工作......那對你有幫助@Tim – 2012-07-23 19:05:17

回答

0
private Drawable ImageOperations(Context ctx, String url, String saveFilename) { 
     try { 
      InputStream is = (InputStream) fetch(url); 
      Drawable d = Drawable.createFromStream(is, "src"); 
      return d; 
     } catch (MalformedURLException e) { 
      return null; 
     } catch (IOException e) { 
      return null; 
     } 
    } 
    public Object fetch(String address) throws MalformedURLException,IOException { 
     URL url = new URL(address); 
     Object content = url.getContent(); 
     return content; 
    }