2013-04-05 112 views
0

我試圖從github使用universalimageloader在網格視圖中獲取圖像。但不是從常量加載靜態最終圖像,我想從服務器動態加載圖像路徑。所以我做了一個jsonparser來從服務器加載信息並解析它。但我正在努力將其分配給IMAGES字符串數組。請告訴我如何分配。我是新手到android。動態插入圖像路徑

ImageConstants.java

import java.util.ArrayList; 
import java.util.HashMap; 

import org.json.JSONArray; 
import org.json.JSONException; 
import org.json.JSONObject; 

import android.app.ProgressDialog; 
import android.os.AsyncTask; 
import android.util.Log; 

public class ImageConstant { 

    public static String[] IMAGE1; 
    private static String[] constants; 
    public static String url = "http://ensign119.com/assets/image11.php"; 

     // JSON Node names 
     protected static final String TAG_PRODUCTS = "image_path"; 
     protected static final String TAG_CID = "file_id"; 
     public static final String TAG_NAME = "file_name"; 

     // contacts JSONArray 
     JSONArray products = null; 

    public String[] ImageConstant() { 
     // TODO Auto-generated constructor stub 

     new Message().execute(url); 
     for(int i=0; i<IMAGE1.length;i++){ 
      constants[i] = IMAGE1[i]; 
     } 

     for(String name:constants){ 
      Log.d("new path", name); 
     } 

     return constants; 
    } 

    class Message extends AsyncTask<String, Integer, ArrayList<HashMap<String, String>> > { 
     ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>(); 
//  ProgressDialog progress = new ProgressDialog(Path.this); 
     // Creating JSON Parser instance 
     JSONParser jParser = new JSONParser(); 
       @Override 
      protected ArrayList<HashMap<String, String>> doInBackground(String... params) { 

        Log.d("doInBackgound","backgound is running"); 

        Log.d("json string ", jParser.getJSONFromUrl(url).toString()); 
        // getting JSON string from URL 
        JSONObject json = jParser.getJSONFromUrl(url); 

        Log.d("path_parsing", "before parsing"); 
        try { 
         // Getting Array of Contacts 
         products = json.getJSONArray(TAG_PRODUCTS); 

         // looping through All Contacts 
         for(int i = products.length()-1; i >=0; i--){ 
          JSONObject c = products.getJSONObject(i); 

          // Storing each json item in variable 
          String cid = c.getString(TAG_CID).toString(); 
          String name = c.getString(TAG_NAME); 
          // creating new HashMap 
          HashMap<String, String> map = new HashMap<String, String>(); 

          // adding each child node to HashMap key => value 
          map.put(TAG_CID, cid); 
          map.put(TAG_NAME, name); 

          // adding HashList to ArrayList 
          mylist.add(map); 
          Log.d("mylist_value", mylist.toString()); 
         } 
        } catch (JSONException e) { 
         e.printStackTrace(); 
        } 
        Log.d("path_parsing", "after parsing"); 
        return mylist; 

       } 

      @Override 
      protected void onPostExecute(ArrayList<HashMap<String, String>> result) { 
//    if(progress.isShowing()){ 
//     progress.dismiss(); 
//    } 
       IMAGE1 = new String[result.size()]; 
       for(int i=0; i<result.size(); i++){ 
        IMAGE1[i] = result.get(i).get("file_name"); 
       } 
       for(String path: IMAGE1){ 
        Log.d("path", path.toString()); 
       } 
       Log.d("postExecute","Postexecute is running"); 
      } 

      @Override 
      protected void onProgressUpdate(Integer... values) { 
       // TODO Auto-generated method stub 
       super.onProgressUpdate(values); 

      } 

      @Override 
      protected void onPreExecute() { 
       // TODO Auto-generated method stub 
      } 
    } 

} 

Constants.java

public final class Constants { 

    public static final String[] IMAGES = new String[] { 

      "http://wrong.site.com/corruptedLink", // Wrong link 
    }; 

    private Constants() { 
    } 

    public static class Config { 
     public static final boolean DEVELOPER_MODE = false; 
    } 

    public static class Extra { 
     public static final String IMAGES = "com.nostra13.example.universalimageloader.IMAGES"; 
     public static final String IMAGE_POSITION = "com.nostra13.example.universalimageloader.IMAGE_POSITION"; 
    } 
} 
+0

你得到的錯誤是什麼? – asloob 2013-04-05 07:26:21

+0

我沒有收到任何錯誤。我想要字符串數組IMAGES需要動態加載。你能告訴我該怎麼做。 – user2064667 2013-04-05 07:28:14

回答

0
Try this, 

https://github.com/thest1/LazyList

In GridView Adapter, add this code in your getView() method 

ImageLoader imageLoader = new Imageloader(MainActivity.this); 
imgLoader.DisplayImage(YOUR IMAGE PATH, YOUR IMAGEVIEW); 
0

通用圖像裝載機

https://github.com/nostra13/Android-Universal-Image-Loader。它基於Lazy List(基於相同原理)。但它有很多其他配置。我寧願使用通用圖像加載程序因爲它給你更多的配置選項。如果downlaod失敗,您可以顯示錯誤圖像。可以顯示圓角的圖像。可以緩存在光盤或內存上。可以壓縮圖像。

在您的自定義適配器構造

File cacheDir = StorageUtils.getOwnCacheDirectory(a, "your folder"); 

// Get singletone instance of ImageLoader 
imageLoader = ImageLoader.getInstance(); 
// Create configuration for ImageLoader (all options are optional) 
ImageLoaderConfiguration config = new ImageLoaderConfiguration.Builder(a) 
     // You can pass your own memory cache implementation 
    .discCache(new UnlimitedDiscCache(cacheDir)) // You can pass your own disc cache implementation 
    .discCacheFileNameGenerator(new HashCodeFileNameGenerator()) 
    .enableLogging() 
    .build(); 
// Initialize ImageLoader with created configuration. Do it once. 
imageLoader.init(config); 
options = new DisplayImageOptions.Builder() 
.showStubImage(R.drawable.stub_id)//display stub image 
.cacheInMemory() 
.cacheOnDisc() 
.displayer(new RoundedBitmapDisplayer(20)) 
.build(); 

在你getView()

ImageView image=(ImageView)vi.findViewById(R.id.imageview); 
    imageLoader.displayImage(imageurl, image,options); 
    //provide imageurl, imageview and options 
    //if constant is an array containing urls 
    //instead of imageurl use constants[position]. 

你可以用其他選項配置,以滿足您的需求。

隨着通用圖像加載器,您可以查看持有人的平滑滾動和性能。 http://developer.android.com/training/improving-layouts/smooth-scrolling.html