2014-02-24 58 views
0

每當我想在手機上運行我的代碼,移動速度慢,圖像加載速度也慢我認爲有尺寸問題,所以請告訴我如何獲得小尺寸的圖像。 這是我的代碼。圖片加載從網址變慢系統android

public class TopRatedFragment extends Fragment { 

    JSONParser jParser = new JSONParser(); 
    private static String url_all_products = "http://3ilogics.org/vid/get_all_products.php"; 

    // JSON Node names 
    private static final String TAG_SUCCESS = "success"; 
    private static final String TAG_PRODUCTS = "products"; 
    private static final String TAG_PID = "pid"; 
    private static final String TAG_NAME = "name"; 
    private static final String TAG_EMAIL = "price"; 
    private static final String TAG_IMG = "images_url"; 

    // products JSONArray 
    JSONArray products = null; 
    JSONArray products2 = null; 
    @Override 
    public View onCreateView(LayoutInflater inflater, ViewGroup container, 
      Bundle savedInstanceState) { 

     View rootView = inflater.inflate(R.layout.fragment_top_rated, container, 
       false); 
     List<NameValuePair> params = new ArrayList<NameValuePair>(); 
     JSONObject json = jParser.makeHttpRequest(url_all_products, "GET", params); 

     try { 
      products = json.getJSONArray(TAG_PRODUCTS); 



      //sd=json.getJSONObject(TAG_PRODUCTS).length(); 
      for (int i = 0; i < products.length(); i++) { 
       JSONObject c = products.getJSONObject(i); 

       // Storing each json item in variable 
       String id = c.getString(TAG_PID); 
       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_PID, id); 
       map.put(TAG_NAME, name); 


      } 


     } catch (JSONException e) { 
      // TODO Auto-generated catch block 
      e.printStackTrace(); 
     } 
     HorizontalListView listview = (HorizontalListView) rootView 
       .findViewById(R.id.listview); 
     listview.setAdapter(mAdapter); 


     return rootView; 


    } 

    private static String[] dataObjects = new String[] { "Text #1", "Text #2", 
      "Text #3", "Text #4", "Text #5", "Text #6", "Text #7", "Text #8", 
      "Text #9", "Text #10" }; 

    private BaseAdapter mAdapter = new BaseAdapter() { 

     @Override 
     public int getCount() { 
      return TopRatedFragment.this.products.length(); 
     } 

     @Override 
     public Object getItem(int position) { 
      return null; 
     } 

     @Override 
     public long getItemId(int position) { 
      return 0; 
     } 

     @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
      View retval = LayoutInflater.from(parent.getContext()).inflate(
        R.layout.viewitem, null); 
      TextView title = (TextView) retval.findViewById(R.id.title); 
      ImageView title2 = (ImageView) retval.findViewById(R.id.image); 
      String img=null; 

      try { 
       //Log.d("sda",products.getJSONObject(position).toString()); 
       title.setText(products.getJSONObject(position).getString(TAG_NAME)); 
       img=products.getJSONObject(position).getString(TAG_IMG); 
       title2.setImageBitmap(getBitmapFromURL(img)); 
       System.gc(); 
      } 

      catch (JSONException e) { 
       // TODO Auto-generated catch block 
       e.printStackTrace(); 
      } 



      //Log.d("All Products: ", json.toString()); 
      return retval; 
     } 

    }; 
    public Bitmap getBitmapFromURL(String img) { 
     try { 

      URL url = new URL(img); 
      HttpURLConnection connection = (HttpURLConnection) url 
        .openConnection(); 
      connection.setDoInput(true); 
      connection.connect(); 
      InputStream input = connection.getInputStream(); 

      Bitmap myBitmap = BitmapFactory.decodeStream(input); 

      return myBitmap; 

     } catch (Exception e) { 
      // TODO: handle exception 
      e.printStackTrace(); 
      return null; 
     } 
    } 


} 
+1

同時運行此代碼爲網絡電話不能在UI線程中執行,你應該得到的異常。 –

回答