I know that the first thing you are going to do is ... why the hell in the world that you used in AsyncTask.
So here is my problem: I am working on some Android application (API 7 for Android version 2.1 or higher), and I am testing the emulator, and everything was fine, so I tested on HTC Sensation and it says NetworkOnMainThreadExeption!
I uploaded a few photos and then painted on a map.
Therefore, to solve this problem, everyone (Internet connection) in this case uploads the images that I must use for AsyncTask to work.
So, I need a method to find out when all the pictures are taken, so I can start drawing.
I tried so much, and I had no idea whatsoever. I have one solution with a handler, but if you are working on a slower network, I get nullpointer (because the pictures do not load).
So please help me.
EDIT:
here is the idea:
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; } } }
Hope now clearer.
android handler android-asynctask solution
user1730007 Oct 26 2018-12-12T00: 00Z
source share