2014-03-04 91 views
2

我們希望將存儲在PHP服務器中的圖片加載到我們的android應用程序中。 我們已經將圖像網址存儲在mysql數據庫中,我們可以將它們全部提取到ArrayList中,但我們無法將它們設置到gridview中。如何將一堆圖片加載到服務器中存儲到Android應用程序中

我們還必須保持圖像的長寬比,因爲它的繪畫圖像我們無法顯示,我們無法在靜態方塊中顯示它。

這是我們的代碼中,我們從服務器獲取圖像的URL - >

public class fragmentShopall extends Fragment{ 


     public JSONObject jsonResponse; 
     public JSONArray jsonMainNode; 
     public ListView mListView; 
     public ArrayList<String> mylist,imgNames ; 
     public ArrayAdapter<String> dataAdapter; 
     public GridView grid; 
     public ListView list; 
     public fragmentShopall(){} 

    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragmentshopall, container, false); 
     grid=(GridView) rootView.findViewById(R.id.gridView1); 
     list = (ListView) rootView.findViewById(R.id.listView1); 

     testAsynch MyTask= new testAsynch(); 
     MyTask.execute(); 

     return rootView; 
    } 

    class testAsynch extends AsyncTask<Void,Integer,String> 
    { 
     String readFeed; 
     JSONObject json; 
     StringBuilder builder = new StringBuilder(); 

     protected void onPreExecute(){ 

       Log.d("PreExceute","On pre Exceute......"); 
     } 

     protected String doInBackground(Void...arg0) { 

     Log.d("DoINBackGround","On doInBackground..."); 
     StringBuilder builder = new StringBuilder(); 
     HttpClient client = new DefaultHttpClient(); 

     // domain intentionally obfuscated for security reasons 
     //HttpGet httpGet = new HttpGet("http://innobytes.in/webservices/category"); 
     HttpGet httpGet = new HttpGet("http://10.0.2.2/pratik/category"); 
     httpGet.setHeader("content-type", "application/json; charset=UTF-8"); 
     try 
     { 
     HttpResponse response = client.execute(httpGet); 
     StatusLine statusLine = response.getStatusLine(); 
     int statusCode = statusLine.getStatusCode(); 
     if (statusCode == 200) { 
      HttpEntity entity = response.getEntity(); 
      InputStream content = entity.getContent(); 
      BufferedReader reader = new BufferedReader(new InputStreamReader(content)); 
      String line; 
      while ((line = reader.readLine()) != null) { 
       builder.append(line); 
       } 
      } else { 
       Log.d("onProgressUpdate","Failed to download file.........."); 
      } 
     } catch (ClientProtocolException e) { 
      e.printStackTrace(); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
     return builder.toString(); 

    } 

     @SuppressWarnings("null") 
     protected void onPostExecute(String result) { 
       // NOTE: You can call UI Element here. 
      String autcElement = null; 
      String img; 

    String url="http://innobytes.in/artsgallery.in/productpic/"; 
    imgNames = new ArrayList<String>(); 
    mylist = new ArrayList<String>();      
     try{ 
      readFeed = result; 

      JSONArray jsonarray = new JSONArray(readFeed); 

      System.out.println("OKAY_8!!"); 
      //*********** Process each JSON Node ***********//* 

      int lengthJsonArr = jsonarray.length(); 

     for (int i = 0; i < lengthJsonArr; i++) 
      { 

      System.out.println("OKAY_9!!"); 
     //****** Get Object for each JSON node. ***********//* 
      JSONObject jsonChildNode = jsonarray.getJSONObject(i); 
      autcElement = jsonChildNode.optString("tbimageurl").toString(); 
      imgNames.add(autcElement); 
      img=url+autcElement; 
      mylist.add(img); 
      System.out.println(""+img); 

      System.out.print(" Hi.... "+mylist.get(i)); 

//Picasso.with(getActivity()).load("http://innobytes.in/artsgallery.in/productpic/"+autcElement).into((Target) list); 

      } 

     }     
     catch (Exception e) { 
      e.printStackTrace(); 
     } 

      // Close progress dialog 
     } 
     } 
    } 

什麼是最佳的解決方案使用我們擷取到的URL來獲取圖像,並顯示它們,而保持其縱橫比也許像Pinterest或Flickr?

+0

設置適配器從哪裏存儲圖像URL在數據庫..等待我會給你的例子 –

+0

當然,一個例子會很好。 –

回答

0

,而無需下載的每一個形象,你可以使用LazyList Loading機制

基本上懶列表加載使用URL從SD卡或從服務器的圖像延遲加載。這就像按需加載圖像。

圖像可以緩存到本地SD卡或手機內存。網址被認爲是關鍵。如果密鑰存在於SD卡中,則顯示來自SD卡的圖像,通過從服務器下載並顯示圖像並將其緩存到您選擇的位置。緩存限制可以設置。您也可以選擇自己的位置來緩存圖像。緩存也可以被清除。

而不是用戶等待下載大圖像,然後顯示懶惰列表,按需加載圖像。由於圖像緩存,您可以離線顯示圖像。

0

首先初始化你的價值的 像

public class Beanclassname { 

    private String mediaImageUrl; 


    public String getMediaImageUrl() { 
     return mediaImageUrl; 
    } 
    public void setMediaImageUrl(String mediaImageUrl) { 
     this.mediaImageUrl = mediaImageUrl; 
    } 
} 

現在把值豆和atlast添加它在ArrayList和做setadapter 像

bean.setMediaImageUrl(jsonChildNode.getString("key")); 
arraylist.add(bean); 
_adapter = new adapter(this, arraylist); 
         gridview.setAdapter(_adapter); 
         _adapter.notifyDataSetChanged(); 
現在

在適配器中使用創建豆懶惰的圖像加載器來顯示圖像

+0

在適配器類中使用此鏈接用於圖像加載器https://github.com/thest1/LazyList –

+0

初始化ImageLoader像那樣ImageLoader imageLoader = null; \t imageLoader = new ImageLoader(activity);然後調用圖像加載器imageLoader.DisplayImage(bean.getMediaImageUrl(),R.drawable)的顯示方法。ic_stub,view.imgView); –

+0

如果你有任何疑問,那麼你可以關注我.. –

相關問題