0

インターネットから画像を取得するバックグラウンド ワーカーがあります。

現在、キャッチ後に画像を取得しようとすると、Stack OverFlow が発生しました。

これが私のコードです

フェッチ機能

    private InputStream fetch(String urlString) throws MalformedURLException, IOException, IllegalStateException, SocketTimeoutException { 

    HttpClient httpClient = new DefaultHttpClient(); 

    HttpParams params = httpClient.getParams();
    HttpConnectionParams.setConnectionTimeout(params, TIME_OUT);
    HttpConnectionParams.setSoTimeout(params, TIME_OUT);

    HttpGet request = new HttpGet(urlString); 
    HttpResponse response = httpClient.execute(request); 

    return response.getEntity().getContent(); 
} 

バックグラウンド ワーカー

static int tryouts = 0;
/**
 * Getting the bitmap from the web asynchronously 
 * @author orel.z
 * @version 1.0
 */
class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>
{
    @Override
    protected Bitmap doInBackground(String... params) {

        Bitmap bitmap = null;

        try
        {
            InputStream is = fetch(params[0]);
            bitmap = BitmapFactory.decodeStream(is);
            is.close();
            is = null;
            tryouts = 0;
        }
        catch(IOException exception)
        {
            if(tryouts < 3)
            {
                doInBackground(params);
                tryouts++;
            }
            else
            {
                tryouts = 0;
                bitmap = null;
            }
        }
        catch (OutOfMemoryError e) 
        {
            if(tryouts < 3)
            {
                System.gc();
                doInBackground(params);
                tryouts++;
            }
            else
            {
                tryouts = 0;
                bitmap = null;
            }
        }

        return bitmap;
    }
}

これがログです

02-26 15:26:31.616: E/Crittercism(16936): java.lang.StackOverflowError
02-26 15:26:31.616: E/Crittercism(16936):              at     java.lang.String.substring(String.java:1436)
02-26 15:26:31.616: E/Crittercism(16936):              at java.util.regex.Splitter.finishSplit(Splitter.java:105)
02-26 15:26:31.616: E/Crittercism(16936):              at java.util.regex.Splitter.fastSplit(Splitter.java:75)
02-26 15:26:31.616: E/Crittercism(16936):              at java.lang.String.split(String.java:1831)
02-26 15:26:31.616: E/Crittercism(16936):              at java.lang.String.split(String.java:1813)
02-26 15:26:31.616: E/Crittercism(16936):              at java.net.URI.isValidDomainName(URI.java:577)
02-26 15:26:31.616: E/Crittercism(16936):              at java.net.URI.isValidHost(URI.java:545)
02-26 15:26:31.616: E/Crittercism(16936):              at java.net.URI.parseAuthority(URI.java:491)
02-26 15:26:31.616: E/Crittercism(16936):              at java.net.URI.parseURI(URI.java:409)
02-26 15:26:31.616: E/Crittercism(16936):              at java.net.URI.<init>(URI.java:204)
02-26 15:26:31.616: E/Crittercism(16936):              at org.apache.http.impl.conn.ProxySelectorRoutePlanner.determineProxy(ProxySelectorRoutePlanner.java:198)
02-26 15:26:31.616: E/Crittercism(16936):              at org.apache.http.impl.conn.ProxySelectorRoutePlanner.determineRoute(ProxySelectorRoutePlanner.java:150)
02-26 15:26:31.616: E/Crittercism(16936):              at org.apache.http.impl.client.DefaultRequestDirector.determineRoute(DefaultRequestDirector.java:596)
02-26 15:26:31.616: E/Crittercism(16936):              at org.apache.http.impl.client.DefaultRequestDirector.execute(DefaultRequestDirector.java:293)
02-26 15:26:31.616: E/Crittercism(16936):              at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:555)
02-26 15:26:31.616: E/Crittercism(16936):              at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:487)
02-26 15:26:31.616: E/Crittercism(16936):              at org.apache.http.impl.client.AbstractHttpClient.execute(AbstractHttpClient.java:465)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager.fetch(ImageManager.java:405)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager.access$3(ImageManager.java:396)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:319)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager.java:329)
02-26 15:26:31.616: E/Crittercism(16936):              at com.tvincisdk.utils.ImageManager$BitmapWorkerTask.doInBackground(ImageManager
4

2 に答える 2

1

あなたがあなたの電話のtryouts++;後だからですか?それは決して 3 に達しdoInBackground()ない原因になるのではないか?tryouts

試す:

 tryouts++;
 return doInBackground(params);

そして何が起こるか見てください。

また、次の結果を返す必要がありますdoInBackground(params);

于 2013-02-26T13:51:02.783 に答える
1

これはより単純なロジックです。

class BitmapWorkerTask extends AsyncTask<String, Void, Bitmap>
{
    @Override
    protected Bitmap doInBackground(String... params) {

        Bitmap bitmap = null;

        for (int tryouts=0; tryouts<3; tryouts++) {
            try
            {
                InputStream is = fetch(params[0]);
                bitmap = BitmapFactory.decodeStream(is);
                is.close();
                is = null;

                break;
            }
            catch(IOException exception)
            {
                bitmap = null;
            }
            catch (OutOfMemoryError e) 
            {
                System.gc();
                bitmap = null;
            }
        }

        return bitmap;
    }
}
于 2013-02-26T14:05:53.647 に答える