0

I m working on a project which POST a request to the web server and web server responds in XML format. here is the actual story starts. Actually i m sending the request to web server in the same activity and getting response in the same activity, the response contains the images of 500X800 in resolution and some plain text.

public class Home extends Activity implements AdapterView.OnItemClickListener {
    /** Called when the activity is first created. */

    Context context = Home.this;
    ArrayList<String> BookTitle = new ArrayList<String>();
    ArrayList<String> BookCoverPhotos = new ArrayList<String>();
    ArrayList<String> BookAuther = new ArrayList<String>();
    ArrayList<String> BookPublishDate = new ArrayList<String>();

    URL bookImageURL = null;
    Bitmap bitMapImage = null;

    View homeTabLayout;
    View reviewLayout;
    ImageButton btnBack;

    // All static variables
    static final String URL = "http://www.shiaislamiclibrary.com/requesthandler.ashx";
    // XML node keys
    static final String KEY_ITEM = "Book"; // parent node
    static final String KEY_BOOKAUTHOR = "BookAuthor";
    static final String KEY_BOOKDATEPUBLISHED = "DatePublished";
    static final String KEY_BOOKTITLE = "BookTitle";
    static final String KEY_BOOKCODE = "BookCode";
    static final String KEY_BOOKIMAGE = "BookImage";

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

        // Get XML Data in a Array List

        XMLParser parser = new XMLParser();
        String xml = parser.getXmlFromUrl(URL, "Imam Ali"); // getting
                                                            // XML
        Log.i("XML FORMAT", xml);
        Document doc = parser.getDomElement(xml); // getting DOM element

        NodeList nl = doc.getElementsByTagName(KEY_ITEM);

        // looping through all item nodes <item>
        for (int i = 0; i < nl.getLength(); i++) {
            Element e = (Element) nl.item(i);

            BookTitle.add(parser.getValue(e, KEY_BOOKTITLE));
            BookCoverPhotos.add("http://shiaislamicbooks.com/books_Snaps/"
                    + parser.getValue(e, KEY_BOOKCODE) + "/1.jpg");
            BookAuther.add(parser.getValue(e, KEY_BOOKAUTHOR));
            BookPublishDate.add(parser.getValue(e, KEY_BOOKDATEPUBLISHED));
            Log.i("URLs", BookPublishDate + "::" + BookAuther + "::"
                    + BookTitle + BookCoverPhotos.toString());

        }

        // OverView Page Views...

        final ImageView imgOverVuBookCoverPhoto = (ImageView) findViewById(R.id.img_OverVuBookCoverPhoto);
        final TextView txtOverVuBookTitle = (TextView) findViewById(R.id.txt_revwBookTitle);
        final TextView txtOverVuBookAuthor = (TextView) findViewById(R.id.txt_revwAutherName);
        final TextView txtBookPublishDate = (TextView) findViewById(R.id.txt_PublishDate);

        homeTabLayout = findViewById(R.id.rel_HomeLayout);
        reviewLayout = findViewById(R.id.scroll_ReviewLayout);
        reviewLayout.setVisibility(View.GONE);

        btnBack = (ImageButton) findViewById(R.id.btnBack);
        btnBack.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                reviewLayout.setVisibility(View.GONE);
                homeTabLayout.setVisibility(View.VISIBLE);

            }
        });

        final GridView gridView = (GridView) findViewById(R.id.gridview);
        gridView.setAdapter(new ImageAdapter(this));
        gridView.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> arg0, View arg1, int pos,
                    long arg3) {

                reviewLayout.setVisibility(View.VISIBLE);
                homeTabLayout.setVisibility(View.GONE);

                Toast.makeText(context, BookTitle.get(pos), Toast.LENGTH_SHORT)
                        .show();

                txtOverVuBookTitle.setText(BookTitle.get(pos));
                txtOverVuBookAuthor.setText(BookAuther.get(pos));
                txtBookPublishDate.setText(BookPublishDate.get(pos));

                try {
                    bookImageURL = new URL(BookCoverPhotos.get(pos));
                } catch (MalformedURLException e) {
                    e.printStackTrace();
                    Toast.makeText(context, "Network Error", Toast.LENGTH_LONG)
                            .show();
                    Log.i("URL", "ERROR");
                }

                try {
                    bitMapImage = BitmapFactory.decodeStream(bookImageURL
                            .openConnection().getInputStream());
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                    Toast.makeText(context, "Bitmap Error", Toast.LENGTH_LONG)
                            .show();
                    Log.i("BITMAP", "ERROR");
                }

                imgOverVuBookCoverPhoto.setImageBitmap(bitMapImage);
            }
        });

    }

    public class ImageAdapter extends BaseAdapter {
        private Context context;

        public ImageAdapter(Context c) {
            context = c;
        }

        // ---returns the number of images---
        public int getCount() {
            // return imageIDs.length;
            return BookCoverPhotos.size();
            // return 6;
        }

        // ---returns the ID of an item---
        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        // ---returns an ImageView view---
        public View getView(int position, View convertView, ViewGroup parent) {

            // ImageView bmImage;

            ImageView img_BookCoverPhoto;
            img_BookCoverPhoto = new ImageView(context);

            LayoutInflater inflater = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            View row = inflater.inflate(R.layout.grid_style, parent, false);

            TextView txt_BooksTitle = (TextView) row
                    .findViewById(R.id.txt_BookTitle);
            txt_BooksTitle.setText(BookTitle.get(position) + "");

            img_BookCoverPhoto = (ImageView) row
                    .findViewById(R.id.imgBookCover);

            try {
                bookImageURL = new URL(BookCoverPhotos.get(position));
            } catch (MalformedURLException e) {
                e.printStackTrace();
                Toast.makeText(context, "Network Error", Toast.LENGTH_LONG)
                        .show();
                Log.i("URL", "ERROR");
            }

            try {
                bitMapImage = BitmapFactory.decodeStream(bookImageURL
                        .openConnection().getInputStream());
            } catch (IOException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
                Toast.makeText(context, "Bitmap Error", Toast.LENGTH_LONG)
                        .show();
                Log.i("BITMAP", "ERROR");
            }
            // img_BookCoverPhoto.setImageResource(imageIDs[position]);
            img_BookCoverPhoto.setImageBitmap(bitMapImage);

            return row;
        }

    }

    public class AsyncTaskForUI extends AsyncTask<String, Integer, String> {
        ProgressDialog progressDialog;

        @Override
        protected void onPreExecute() {

            super.onPreExecute();
            progressDialog = ProgressDialog
                    .show(context,
                            "Loading...",
                            "This may take a few minuts\nDepending Upon your Internet Connection Speed",
                            true);

        }

        @Override
        protected String doInBackground(String... params) {

            try {
                Thread.sleep(10000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

            return null;
        }

        @Override
        protected void onPostExecute(String result) {

            super.onPostExecute(result);
            progressDialog.dismiss();
            Toast.makeText(context, "Completed", Toast.LENGTH_SHORT).show();
        }

    }

    @Override
    public void onItemClick(AdapterView<?> arg0, View arg1, int arg2, long arg3) {

    }
}

i m loading the images and text in the custom GridView which contains a ImageView and a textView. here is the XML of layout

    <LinearLayout 
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/widget44"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:gravity="center_horizontal"
    android:orientation="vertical" >

    <ImageView
        android:id="@+id/imgBookCover"
        android:layout_width="150dp"
        android:layout_height="180dp"
        android:scaleType="center"
        android:adjustViewBounds="true"
        android:src="@drawable/book1">
    </ImageView>

    <TextView
        android:id="@+id/txt_BookTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:gravity="center_horizontal"
        android:text="TextView"
        android:layout_margin="5dp"
        android:textColorHighlight="#656565" 
        android:textColor="@color/White">
    </TextView>

</LinearLayout>

also here is my XMLParser.java class code

    public class XMLParser {

    // constructor
    public XMLParser() {

    }

    /**
     * Getting XML from URL making HTTP request
     * 
     * @param url
     *            string
     * */
    public String getXmlFromUrl(String url, String SearchKeywords) {
        String xml = null;

        try {
            // defaultHttpClient
            DefaultHttpClient httpClient = new DefaultHttpClient();

            HttpPost httpPost = new HttpPost(url);

            /*ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>(
                    3);
            nameValuePairs.add(new BasicNameValuePair("type", "Free Books"));
            nameValuePairs.add(new BasicNameValuePair("lang", "English"));
            nameValuePairs.add(new BasicNameValuePair("book_title", "hasan"));

            try {
                httpPost.setEntity(new UrlEncodedFormEntity(
                        nameValuePairs));

            } catch (UnsupportedEncodingException e1) {
                // TODO Auto-generated catch block
                e1.printStackTrace();
            }*/

            String postParameter = "type=Free Books&lang=English&book_title="+SearchKeywords;

            try {
                httpPost.setEntity(new StringEntity(postParameter));
            }catch (Exception e) {
                // TODO: handle exception
            }


            HttpResponse httpResponse = httpClient.execute(httpPost);
            HttpEntity httpEntity = httpResponse.getEntity();
            xml = EntityUtils.toString(httpEntity);

        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (ClientProtocolException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        // return XML
        return xml;
    }

    /**
     * Getting XML DOM element
     * 
     * @param XML
     *            string
     * */
    public Document getDomElement(String xml) {
        Document doc = null;
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        try {

            DocumentBuilder db = dbf.newDocumentBuilder();

            InputSource is = new InputSource();
            is.setCharacterStream(new StringReader(xml));
            doc = db.parse(is);

        } catch (ParserConfigurationException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (SAXException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        } catch (IOException e) {
            Log.e("Error: ", e.getMessage());
            return null;
        }

        return doc;
    }

    /**
     * Getting node value
     * 
     * @param elem
     *            element
     */
    public final String getElementValue(Node elem) {
        Node child;
        if (elem != null) {
            if (elem.hasChildNodes()) {
                for (child = elem.getFirstChild(); child != null; child = child
                        .getNextSibling()) {
                    if (child.getNodeType() == Node.TEXT_NODE) {
                        return child.getNodeValue();
                    }
                }
            }
        }
        return "";
    }

    /**
     * Getting node value
     * 
     * @param Element
     *            node
     * @param key
     *            string
     * */
    public String getValue(Element item, String str) {
        NodeList n = item.getElementsByTagName(str);
        return this.getElementValue(n.item(0));
    }
}

the code is working just fine but, its hangs app until the server sends response and my app shows black screen until it loads all the images and text in the desired views. and when i scroll the the gridView sometimes it gives exception

java.lang.OutOfMemoryError: bitmap size exceeds VM budget
  • but the problem is that now i want to do XML parsing and bitmap decoding in the AsyncTask. how can i achieve the both tasks in a single doInbackground() method of the AsynTask class.
  • also give a code snippet of resizing the high resolution images to low resolution using the AsyncTask class.
Qadir Hussain
  • 8,721
  • 13
  • 89
  • 124

1 Answers1

0

You should check the size of the image to be downloaded before decoding the image into the memory which can be done by enabling inJustDecodeBounds from BitmapFactory Options. After that you can scale your Bitmap according to the required size to be loaded. I had answered here in detail how we can get the size of the Bitmap before loading it in memory.

I had created a method that takes the url, requiredHeight, requiredWidth of the Image and returns a Bitmap that is suitable after scaling.

private static Bitmap decodeBitmapFromStream(String urlString, 
                                              int reqWidth, int reqHeight) {

        URL url = null;
            InputStream is = null;
            try {
                url = new URL(urlString);
                is = (InputStream) url.getContent();
            } catch (Exception e) {
                e.printStackTrace();
            }

            // First decode with inJustDecodeBounds=true to check dimensions
            final BitmapFactory.Options options = new BitmapFactory.Options();
            options.inJustDecodeBounds = true;
            BitmapFactory.decodeStream(is, null, options);

            // Height and Width of downloaded Image
            int height = options.outHeight;
            int width = options.outWidth;

            // Calculate inSampleSize
            options.inSampleSize = Math.min(width/reqWidth, height/reqHeight);

       // As InputStream can be used only once we have to regenerate it again.
            try {
                is = (InputStream) url.getContent();
            } catch (IOException e) {
                e.printStackTrace();
            }
            // Decode bitmap with inSampleSize set
            options.inJustDecodeBounds = false;
            return  BitmapFactory.decodeStream(is, null, options);
        }
Community
  • 1
  • 1
Lalit Poptani
  • 67,150
  • 23
  • 161
  • 242
  • Lalit thanks for your answer but where should i place this BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; as my images are not stored in any drawable folder rather i m getting the images from the web services – Qadir Hussain Nov 19 '12 at 07:32
  • options.inSampleSize = Math.min(width/reqWidth, height/reqHeight); can you tell me what are width and height .. where should i get these? – Qadir Hussain Nov 19 '12 at 08:17
  • That is the size of the image before loading in the memory I had edited my answer. – Lalit Poptani Nov 19 '12 at 09:09
  • Thanks Lalit Poptani. its working some how. but now i want to do this using the AsyncTask class. can u help me in this regard plz. – Qadir Hussain Nov 19 '12 at 10:31