13

私はあなたが最初にこれを行うことを知っています...なぜあなたはAsyncTaskを使用するのですか。

だからここに私の問題があります私はいくつかのAndroidアプリ(Android 2.1以上のAPI 7)に取り組んでおり、エミュレーターでテストしていますが、すべて問題なかったので、HTCセンセーションでテストしたところ、NetworkOnMainThreadExeptionと表示されます

いくつかの写真をダウンロードして、地図に描いていました。

したがって、この問題をすべて (インターネット接続) で解決するには、この場合、写真をダウンロードして、AsyncTask を機能させる必要があります。

だから、すべての絵がいつ完成したかを知る方法が必要なので、絵を描き始めることができます..

私はとても試してみましたが、結果はわかりません。ハンドラーを使用して1つのソリューションを取得しましたが、低速のネットで実行するとnullpointerが発生します(写真がダウンロードされないため)。

だから私を助けてください。

編集:

ここにアイデアがあります:

Bitmap bubbleIcon ;
    onCreate(){
     ...
// i am making call for Async
new ImgDown().execute(url);
//and then i calling functions and classes to draw with that picture bubbleIcon !
DrawOnMap(bubbleIcon);
}




//THIS IS ASYNC AND FOR EX. SUPPOSE I NEED TO DOWNLOAD THE PIC FIRST
     class ImgDown extends AsyncTask<String, Void, Bitmap> {

        private String url;

        public ImgDown() {
        }

        @Override
        protected Bitmap doInBackground(String... params) {
            url = params[0];
            try {
                return getBitmapFromURL(url);
            } catch (Exception err) {
            }

            return null;

        }

        @Override
        protected void onPostExecute(Bitmap result) {
            bubbleIcon = result;
            bubbleIcon = Bitmap
                    .createScaledBitmap(bubbleIcon, 70, 70, true);

        }

        public Bitmap getBitmapFromURL(String src) {
            try {
                Log.e("src", src);
                URL url = new URL(src);
                HttpURLConnection connection = (HttpURLConnection) url
                        .openConnection();
                connection.setDoInput(true);
                connection.connect();
                InputStream input = connection.getInputStream();
                // /tuka decode na slika vo pomalecuk kvalitet!
                BitmapFactory.Options options = new BitmapFactory.Options();
                options.inSampleSize = 3;
                Bitmap myBitmap = BitmapFactory
                        .decodeStream(new FlushedInputStream(input));
                Log.e("Bitmap", "returned");
                return myBitmap;
            } catch (IOException e) {
                e.printStackTrace();
                Log.e("getBitmapFromURL", e.getMessage());
                return null;
            }
        }

        class FlushedInputStream extends FilterInputStream {
            public FlushedInputStream(InputStream inputStream) {
                super(inputStream);
            }

            public long skip(long n) throws IOException {
                long totalBytesSkipped = 0L;
                while (totalBytesSkipped < n) {
                    long bytesSkipped = in.skip(n - totalBytesSkipped);
                    if (bytesSkipped == 0L) {
                        int byteValue = read();
                        if (byteValue < 0) {
                            break; // we reached EOF
                        } else {
                            bytesSkipped = 1; // we read one byte
                        }
                    }
                    totalBytesSkipped += bytesSkipped;
                }
                return totalBytesSkipped;
            }
        }
    }

私は今がより明確であることを願っています。

4

4 に答える 4

32
class OpenWorkTask extends AsyncTask {

    @Override
    protected Boolean doInBackground(String... params) {
        // do something
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // The results of the above method
        // Processing the results here
        myHandler.sendEmptyMessage(0);
    }

}

Handler myHandler = new Handler() {

    @Override
    public void handleMessage(Message msg) {
        switch (msg.what) {
        case 0:
            // calling to this function from other pleaces
            // The notice call method of doing things
            break;
        default:
            break;
        }
    }
};
于 2012-10-26T02:20:44.430 に答える
2
class openWorkTask extends AsyncTask<String, String, Boolean> {

    @Override
    protected Boolean doInBackground(String... params) {
        //do something
        return true;
    }

    @Override
    protected void onPostExecute(Boolean result) {
        // The results of the above method
        // Processing the results here
    }
}
于 2012-10-26T02:02:59.143 に答える
0

私があなたなら、進行状況ダイアログを使用します。このようにして、ユーザーはASyncTaskが画像をダウンロードしている間に何かが起こっていることを確認できます。PostExecuteで、画像がnullかどうかをチェックするメインコードからメソッドを呼び出します。doInBackgroundメソッドではUIを更新できないため、onPreExecuteまたはonPostExecuteのいずれかでUIが機能することを忘れないでください

private class DownloadPictures extends AsyncTask<String, Void, String> 
{

    ProgressDialog progressDialog;

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

        //Download your pictures

        return null;

    }

    @Override
    protected void onPostExecute(String result) 
    {

        progressDialog.cancel();

        //Call your method that checks if the pictures were downloaded

    }

    @Override
    protected void onPreExecute() {

        progressDialog = new ProgressDialog(
                YourActivity.this);
        progressDialog.setMessage("Downloading...");
        progressDialog.setCancelable(false);
        progressDialog.show();

    }

    @Override
    protected void onProgressUpdate(Void... values) {
        // Do nothing
    }

}
于 2012-10-26T02:11:11.797 に答える