私はあなたが最初にこれを行うことを知っています...なぜあなたは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;
}
}
}
私は今がより明確であることを願っています。