2015-05-16 23 views
0

我已經完成從JSON檢索100個圖像的URL。我想在我的Android應用程序的圖像庫中顯示。我曾嘗試轉換成位圖,但它返回null:在Android中的位圖轉換多個圖像?

(--- SkImageDecoder ::廠返回null)

有人可以幫助我瞭解如何多張圖片的URL轉換成位圖。

這裏是我的代碼

private class DownloadJSON extends AsyncTask<Void, Void, Void> { 
    @Override 
    protected void onPreExecute() { 
     super.onPreExecute(); 

     mProgressDialog = new ProgressDialog(TrustMe.this); 
     mProgressDialog.setTitle("Android JSON Parse Tutorial"); 
     mProgressDialog.setMessage("Loading..."); 
     mProgressDialog.setIndeterminate(false); 
     mProgressDialog.show(); 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     // TODO Auto-generated method stub 
     getImagesfromServer(); 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void args) 
    { 
     mProgressDialog.dismiss(); 

     for (int j = 0; j < arraylist.size(); j++) 
     { 
      Iterator<String> myVeryOwnIterator = map.keySet().iterator(); 
      while (myVeryOwnIterator.hasNext()) { 
       String key = (String) myVeryOwnIterator.next(); 
       String value = (String) map.get(key); 
       byte[] decodedString = Base64.decode(value, 
         Base64.DEFAULT); 
       decodedByte = BitmapFactory.decodeByteArray(
         decodedString, 0, decodedString.length); 

       bitimg.add(decodedByte); 
      } 
     } 

     gallery = (Gallery)findViewById(R.id.galleryImg); 
     _imageAdapter = new ImageAdapter(TrustMe.this); 
     if (_imageAdapter == null) { 
       _imageAdapter = new ImageAdapter(getApplicationContext()); 
       gallery.setAdapter(_imageAdapter); 
     } 
     _imageAdapter.setimage(bitimg); 
     _imageAdapter.notifyDataSetChanged(); 
    } 
} 

下面是JSON數據

05-16 13:04:08.449: I/System.out(686): strID = 259429316 
05-16 13:04:08.459: I/System.out(686): jarr = {"large_thumb":{"url":"http:\/\/thumb9.shutterstock.com\/thumb_large\/612877\/259429316\/stock-photo-cat-with-a-bouquet-at-the-feet-of-mistress-259429316.jpg","width":150,"height":100},"preview":{"url":"http:\/\/image.shutterstock.com\/display_pic_with_logo\/612877\/259429316\/stock-photo-cat-with-a-bouquet-at-the-feet-of-mistress-259429316.jpg","width":450,"height":299},"small_thumb":{"url":"http:\/\/thumb9.shutterstock.com\/thumb_small\/612877\/259429316\/stock-photo-cat-with-a-bouquet-at-the-feet-of-mistress-259429316.jpg","width":100,"height":67}} 
05-16 13:04:08.468: I/System.out(686): strID = 256797013 
05-16 13:04:08.468: I/System.out(686): jarr = {"large_thumb":{"url":"http:\/\/thumb10.shutterstock.com\/thumb_large\/1477187\/256797013\/stock-photo-beautiful-woman-with-cat-portrait-brunette-with-bengal-cat-close-up-256797013.jpg","width":150,"height":100},"preview":{"url":"http:\/\/image.shutterstock.com\/display_pic_with_logo\/1477187\/256797013\/stock-photo-beautiful-woman-with-cat-portrait-brunette-with-bengal-cat-close-up-256797013.jpg","width":450,"height":300},"small_thumb":{"url":"http:\/\/thumb10.shutterstock.com\/thumb_small\/1477187\/256797013\/stock-photo-beautiful-woman-with-cat-portrait-brunette-with-bengal-cat-close-up-256797013.jpg","width":100,"height":67}} 
+0

看到這個答案http://stackoverflow.com/questions/30256060/converting-image-url-to-bitmap-quickly/30256138#30256138 – theapache64

+0

我不能理解 – JAndroid

+0

'我做了從JSON檢索100圖像的URL。 '。 ?????對不起,我不明白這一點。請解釋你想要做什麼或完成了什麼。 – greenapps

回答

0

使用此,

URL newurl = new URL(imageLink); 
Bitmap image = BitmapFactory.decodeStream(newurl.openConnection().getInputStream()); 
0

一個建議,在doInBackground()而不是onPostExecute(),位圖解碼所有的位圖 - 解碼也是一件令人費解的工作,而且你似乎保存了你所有的我在Base64轉換的形式在內存中的法師?爲什麼不把它們保存爲磁盤上的緩存?