0

Getting following exception while downloading images

> 10-19 13:18:49.421: E/AndroidRuntime(2259):   FATAL EXCEPTION: AsyncTask #4
10-19 13:18:49.421: E/AndroidRuntime(2259):     java.lang.RuntimeException: An error occured while executing doInBackground()
10-19 13:18:49.421: E/AndroidRuntime(2259):     at android.os.AsyncTask$3.done(AsyncTask.java:299)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at java.util.concurrent.FutureTask.finishCompletion(FutureTask.java:352)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at java.util.concurrent.FutureTask.setException(FutureTask.java:219)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at java.util.concurrent.FutureTask.run(FutureTask.java:239)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:230)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1080)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:573)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at java.lang.Thread.run(Thread.java:856)
10-19 13:18:49.421: E/AndroidRuntime(2259):     Caused by: java.lang.OutOfMemoryError
10-19 13:18:49.421: E/AndroidRuntime(2259):     at android.graphics.BitmapFactory.nativeDecodeStream(Native Method)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:529)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at android.graphics.BitmapFactory.decodeStream(BitmapFactory.java:601)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at com.yemektarifleri.monu.CategoryList.downloadBitmap(CategoryList.java:356)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at com.yemektarifleri.monu.CategoryList$BitmapDownloaderTask.doInBackground(CategoryList.java:299)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at com.yemektarifleri.monu.CategoryList$BitmapDownloaderTask.doInBackground(CategoryList.java:1)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at android.os.AsyncTask$2.call(AsyncTask.java:287)
10-19 13:18:49.421: E/AndroidRuntime(2259):     at java.util.concurrent.FutureTask.run(FutureTask.java:234)
10-19 13:18:49.421: E/AndroidRuntime(2259):     ... 4 more


@Override
    protected ArrayList<Product> doInBackground(String... params) {

        ArrayList<Product> productList = new ArrayList<Product>();
        try {
            String ret = getUrlString(params[0]);
            ret = ret.trim();           
            JSONObject jsonObj = new JSONObject(ret);


            JSONArray products = jsonObj.getJSONArray("item");
            JSONObject tmp;
            for(int i=0; i<products.length(); i++){
                tmp = products.getJSONObject(i);
                Product _product = new Product(
                        tmp.getString("foodId"), 
                        tmp.getString("yemekadi"), 
                        tmp.getString("topsure"),
                        tmp.getString("kackisi"),
                        tmp.getString("foodimg")

                        );
                productList.add(_product);

                foodIds.add(tmp.getString("foodId"));
            }
        } catch (SocketTimeoutException e) {
            e.printStackTrace();
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return productList;
    }



public Bitmap downloadBitmap(String url) throws MalformedURLException, IOException {
    final AndroidHttpClient client = AndroidHttpClient.newInstance("Android");
    final HttpGet getRequest = new HttpGet(url);

    try {
        HttpResponse response = client.execute(getRequest);
        final int statusCode = response.getStatusLine().getStatusCode();
        if (statusCode != HttpStatus.SC_OK) { 
            return null;
        }

        final HttpEntity entity = response.getEntity();
        if (entity != null) {
            InputStream inputStream = null;
            try {
                inputStream = entity.getContent(); 
                final Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
                return bitmap;
            } finally {
                if (inputStream != null) {
                    inputStream.close();  
                }
                entity.consumeContent();
            }
        }
    } catch (MalformedURLException e) {
        getRequest.abort();
        e.printStackTrace();
    } catch (IOException e) {
        getRequest.abort();
        e.printStackTrace();
    } finally {
        if (client != null) {
            client.close();
        }
    }
    return null;
}
4

1 回答 1

0

你得到一个带有位图的 OutOfMemory 异常,所以你应该看看这个

于 2013-10-19T13:51:48.527 回答