我知道第一個你會這麼做的......爲什麼你在這個世界上使用AsyncTask。Android:如何等待AsyncTask在MainThread中完成?
所以這裏是我的問題,我正在一些Android應用程序(Android 2.1或更高版本的API 7)上工作,我正在測試仿真器,一切都很好,所以然後我在HTC Sensation上測試,它說NetworkOnMainThreadExeption!
我正在下載一些圖片,然後在地圖上繪製。
所以爲了解決這個問題每個(互聯網連接)在這種情況下下載圖片我必須把AsyncTask的工作。
所以我需要一個方法是如何知道什麼時候所有照片都做了,所以我可以開始繪製..
我嘗試了這麼多,沒有結果,我不知道。我有一個解決方案與處理程序,但如果運行在較慢的網絡我得到空指針(因爲圖片不下載)。
所以請幫助我。
編輯:
這裏的理念是:
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;
}
}
}
我現在希望更清晰。
,請複製粘貼相關的源代碼。如果僅提供非正式描述,則無法提供幫助。 –
我編輯了帖子。請參閱 – user1730007
使用進度對話框 – WillingLearner