2013-10-11 120 views
0

我在服務器上有多個圖像。我想要做的是我想從服務器檢索這些圖像,並將其設置在imageView上。從服務器獲取位圖圖像以在ImageView上設置

我已經從服務器獲取blob類型的圖像,將其解碼爲字節數組,然後將其轉換爲位圖圖像。

我很困惑如何從服務器獲取位圖圖像。

我經歷過很多問題上如此喜歡

Retriving image from server to android app

Load Large Image from server on Android

請任何一個可以幫助的鏈接或代碼。

謝謝你寶貴的時間。

我嘗試的東西,這裏是代碼:

public class MainActivity extends Activity { 

@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.activity_main); 

Bitmap bitmap = DownloadImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"); 


    ImageView img = (ImageView) findViewById(R.id.img); 
    img.setImageBitmap(bitmap); 
} 

private InputStream OpenHttpConnection(String urlString) 
throws IOException 
{ 
    InputStream in = null; 
    int response = -1; 

    URL url = new URL(urlString); 
    URLConnection conn = url.openConnection(); 

    if (!(conn instanceof HttpURLConnection))      
     throw new IOException("Not an HTTP connection"); 

    try{ 
     HttpURLConnection httpConn = (HttpURLConnection) conn; 
     httpConn.setAllowUserInteraction(false); 
     httpConn.setInstanceFollowRedirects(true); 
     httpConn.setRequestMethod("GET"); 
     httpConn.connect(); 
     response = httpConn.getResponseCode();     
     if (response == HttpURLConnection.HTTP_OK) { 
      in = httpConn.getInputStream();         
     }      
    } 
    catch (Exception ex) 
    { 
     throw new IOException("Error connecting");    
    } 
    return in;  
} 
private Bitmap DownloadImage(String URL) 
{   
    Bitmap bitmap = null; 
    InputStream in = null; 



    try { 
     in = OpenHttpConnection(URL); 
     BufferedInputStream bis = new BufferedInputStream(in, 8190); 

     ByteArrayBuffer baf = new ByteArrayBuffer(50); 
     System.out.println("BAF= "+baf); 
     int current = 0; 
     while ((current = bis.read()) != -1) 
     { 
      baf.append((byte)current); 
     } 
     byte[] imageData = baf.toByteArray(); 
     System.out.println("imageData= "+imageData); 
     bitmap =BitmapFactory.decodeByteArray(imageData, 0, imageData.length); 
     in.close(); 
    } 
    catch (IOException e1) 
    { 

     e1.printStackTrace(); 
    } 
    return bitmap;     
} 
} 

這將返回形象,但他的網址,我已經使用由映像名稱。如果我有更多的圖像,並且想要在應用程序加載時檢索所有圖像,該怎麼辦?

+0

檢查本教程中有關如何從服務器異步使用的AsyncTask下載圖片:http://www.android-ios-tutorials.com/182/show-progressbar-while-download-image-using-asynctask-in-android/ – Houcine

+2

凌空,畢加索,素...... – njzk2

+0

謝謝大家的回答。但每個人都提出了不同的技巧,而我對所有這些都是陌生的,這讓我更加困惑。 – Droid

回答

0

我建議使用Prime庫,它具有異步下載圖像的功能,然後以具有url和位圖圖像作爲參數的偵聽器進行回調。

4

我向你推薦一種不同的方式,就像一個魅力:Android查詢。

你可以從這裏下載jar文件:http://code.google.com/p/android-query/downloads/list

AQuery androidAQuery=new AQuery(this); 

舉個例子:

androidAQuery.id(YOUR IMAGEVIEW).image(YOUR IMAGE TO LOAD, true, true, getDeviceWidth(), ANY DEFAULT IMAGE YOU WANT TO SHOW); 

使用上面的代碼,你可以直接顯示通過URL你的形象。現在,下面的代碼是從網址直接獲取位圖:

androidAQuery.ajax(YOUR IMAGE URL,Bitmap.class,0,new AjaxCallback<Bitmap>(){ 
         @Override 
         public void callback(String url, Bitmap object, AjaxStatus status) { 
          super.callback(url, object, status); 

          //You will get Bitmap from object. 
         } 


    }); 

該庫由Android本身提供的,所以用它看任何你想要的結果。

這是非常快速和準確的,使用這個你可以找到更多的功能,如動畫時加載;獲取位圖,如果需要的話;等

+0

確定試試吧,如果這篇文章對你有幫助,那麼請接受並注意讓其他人可以使用它。 –

+0

您提供的鏈接由各種jar文件組成。我應該下載所有這些還是有特定的一個。 – Droid

+0

沒有做只有一個,只有第一個或第二個。 –

3

我會建議你使用Volley庫(這次報告會是here),在其應用谷歌使用由谷歌相同的庫播放。在Google I/O 13上,Ficus Kilkpatrick提出了Volley。 Volley是一個HTTP庫,旨在消除大量樣板代碼,簡化列表中圖像的處理,更快速地抽象出一個或另一個HTTP客戶端的複雜性。

初始化

排球依賴於一個請求隊列,該處理的請求隊列中,和上ImageLoader的,也就是負責加載圖像。我們需要每一個

public static ImageLoader mImageLoader; 
public static RequestQueue mRequestQueue; 

我們還需要初始化它們。隊列需要一個Context,ImageLoader需要一個ImageCache。在這裏,我使用了一個基本的包裝LruCache。

mRequestQueue = Volley.newRequestQueue(this); 
mImageLoader = new ImageLoader(mRequestQueue, new BitmapLru(64000)); 

初始化在第一個活動的onCreate()中完成。

獲取的圖像

假設你必須填寫一個gridview與從服務器下載的圖片。 ImageUrlAdapter使用NetworkImageView填充GridView(或任何AdapterView)。這是Volley提供的將網絡中的圖像放入ImageView中的類。它實際上擴展了ImageView,這意味着您可以在任何使用ImageView的地方使用它。使用起來非常簡單。適配器中唯一的代碼是getView。

@Override 
public View getView(int position, View convertView, ViewGroup parent) { 
    NetworkImageView imageView; 
    if (convertView == null) { 
     imageView = new NetworkImageView(getContext()); 
    } else { 
     imageView = (NetworkImageView) convertView; 
    } 
    String url = getItem(position); 
    imageView.setImageUrl(url, MainActivity.mImageLoader); 
    return imageView; 
} 

只有新來的事情就是imageView.setImageUrl(URL,MainActivity.mImageLoader);

就是這樣。沒有什麼是必需的。

+0

嘿,我正在瀏覽視頻。我也可以使用這個從服務器檢索.zip文件嗎? – Droid

+0

你需要做的是克隆項目庫(git clone https://android.googlesource。com/platform/frameworks/volley),然後作爲Android庫導入到IDE中,最後從項目中引用導入的項目以便使用它。 –

0

我強烈建議使用Square的Picasso庫。處理它非常容易,並且消除了檢索緩存的所有痛苦。

1

使用Universal Image Loader

由於您的困惑,我將解釋

點擊here下載jar文件並將其粘貼到您的libs文件夾

在你的清單中添加權限

的這兩條線
<uses-permission android:name="android.permission.INTERNET" /> 
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> 

使用此代碼

ImageLoader imageLoader = ImageLoader.getInstance(); 
ImageLoaderConfiguration config = new ImageLoaderConfiguration.createDefault(getApplicationContext()) 
imageLoader.init(config); 
DisplayImageOptions options = new DisplayImageOptions.Builder() 
.showStubImage(R.drawable.ic_launcher) 
.cacheInMemory() 
.cacheOnDisc() 
.build(); 
imageLoader.displayImage("http://www.allindiaflorist.com/imgs/arrangemen4.jpg",your_IMAGEVIEW,options,null); 

或者,如果你只是想該位圖,然後用這個

Bitmap bmp = imageLoader.loadImageSync("http://www.allindiaflorist.com/imgs/arrangemen4.jpg"); 
相關問題