Android: wie warten AsyncTask fertig im MainThread?

Ich weiß, dass Sie gonna das ist... warum zum Teufel auf der Welt, die Sie dann verwenden AsyncTask.

So, hier ist mein problem ich arbeite auf einigen Android-app (API 7 für android 2.1 oder höher) , und ich die Prüfung auf emulator und alles war in Ordnung, so dann habe ich getestet auf HTC Sensation, und es sagt NetworkOnMainThreadExeption!

War ich herunterladen einige Bilder und dann malen Sie auf die Karte.

So, um dieses problem zu lösen jede (internet-Verbindung) in diesem Fall das herunterladen der Bilder, die ich setzen muss, auf AsyncTask zu arbeiten.

Also brauche ich eine Methode, wie man weiß, wenn alle Bilder fertig sind, damit ich anfangen kann zu zeichnen..

Ich versuche so viel und keinem Ergebnis habe ich keine Ahnung. Ich habe eine Lösung mit hf aber wenn laufen auf langsameren net ich bekomme nullpointer(denn die Bilder werden nicht heruntergeladen).

Also bitte helft mir.

EDIT:

hier ist die Idee:

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;
            }
        }
    }

ich hoffe, jetzt ist klarer.

Bitte fügen Sie den entsprechenden Quellcode. Es ist nicht möglich zu helfen, wenn Sie bieten informelle Beschreibung nur.
ich bearbeitet die post. bitte sehen
Verwenden Sie ein Dialogfeld mit dem Fortschritt
und was wäre wenn ich mit Fortschrittsdialog, Lesen Sie bitte noch einmal meine Frage... :/
während der Wartezeit gibt es immer noch user-Interaktion oder nicht? wenn nicht, dann mit einem Fortschritt-dialog, um einen dialog, der dem Benutzer sagt, warten

InformationsquelleAutor user1730007 | 2012-10-26

Schreibe einen Kommentar