我知道第一个你要这就是......为什么你会在世界上使用AsyncTask.
所以这是我的问题我正在研究一些Android应用程序(API为Android 2.1或更高版本),我正在测试模拟器,一切都很好,所以然后我测试HTC Sensation,它说NetworkOnMainThreadExeption!
我正在下载一些图片,然后在地图上绘制.
所以为了解决这个问题,每个(互联网连接)在这种情况下下载图片我必须把AsyncTask放到工作.
所以我需要一种方法如何知道所有图片何时完成所以我可以开始绘图..
我试了这么多,没有结果我不知道.我有一个处理程序的解决方案,但如果在较慢的网络上运行我得到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 { …Run Code Online (Sandbox Code Playgroud) 始终在服务器上上传带有0KB的图像test.jpg!
这是图片:
http://s11.postimage.org/xz6gbi1rl/Capture_Problem_Android.jpg
所以你可以看到我在服务器上得到了什么.
我将不胜感激任何帮助或建议,我希望我能解决问题..
Android代码:
public class Main extends Activity {
InputStream inputStream;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
setContentView(R.layout.main);
Bitmap bitmap = BitmapFactory.decodeResource(getResources(),R.drawable.ic_launcher);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 90, stream); //compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeBytes(byte_arr);
ArrayList<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
nameValuePairs.add(new BasicNameValuePair("image",image_str));
try{
HttpClient httpclient = new DefaultHttpClient();
HttpPost httppost = new HttpPost("http://server/uploader2.php");
httppost.setEntity(new UrlEncodedFormEntity(nameValuePairs));
HttpResponse response = httpclient.execute(httppost);
String the_string_response = convertResponseToString(response);
Toast.makeText(this, "Response " …Run Code Online (Sandbox Code Playgroud)