图像加载从网址越来越慢的系统android

时间:2014-02-24 08:54:05

标签: java json eclipse android-fragments android-imageview

每当我想在移动设备上运行我的代码时,移动设备速度很慢,图像加载速度也很慢我认为存在尺寸问题所以请告诉我如何获得小尺寸的图像。 这是我的代码。

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 个答案:

答案 0 :(得分:0)

使用异步任务执行此类操作,如下所示:http://www.vogella.com/tutorials/AndroidBackgroundProcessing/article.html