我知道,首先你要去这是......为什么世界上的挫折感,你再使用AsyncTask的。
因此,这里是我的问题,我工作的一些Android应用程序(API 7为Android 2.1或更高版本),我测试的仿真器和一切都很好,所以后来我测试了HTC感觉和它说NetworkOnMainThreadExeption!
我下载了一些照片,然后在地图上绘制。
因此,要解决这个问题,每一个(网络连接),在这种情况下下载照片,我必须对AsyncTask的工作。
所以我需要一个方法如何知道什么时候所有照片都做了,所以我可以开始绘制。
我是想了这么多,也没有结果,我不知道。我得到了一个解决方案,处理程序,但如果运行在较慢的网络,我得到空指针(因为图片不下载)。
所以,请帮助我。
编辑:
这里的理念是:
位图bubbleIcon;
的onCreate(){
...
//我正在呼吁异步
新imgDown预先()执行(URL);
//然后我调用函数和类与图片bubbleIcon抽奖!
DrawOnMap(bubbleIcon);
}
//这是异步和EX。假设我需要下载PIC第一
类imgDown预先扩展的AsyncTask<字符串,太虚,位图> {
私人字符串URL;
公共imgDown预先(){
}
@覆盖
受保护的位图doInBackground(字符串... PARAMS){
网址= PARAMS [0];
尝试 {
返回getBitmapFromURL(URL);
}赶上(例外错误){
}
返回null;
}
@覆盖
保护无效onPostExecute(位图的结果){
bubbleIcon =结果;
bubbleIcon =位图
.createScaledBitmap(bubbleIcon,70%,70,真);
}
公共位图getBitmapFromURL(字符串SRC){
尝试 {
Log.e(src用户,SRC);
网址URL =新的URL(SRC);
HttpURLConnection的连接=(HttpURLConnection类)网址
.openConnection();
connection.setDoInput(真正的);
connection.connect();
输入的InputStream = connection.getInputStream();
// /涂卡德code呐SLIKA VO pomalecuk kvalitet!
BitmapFactory.Options选项=新BitmapFactory.Options();
options.inSampleSize = 3;
位图MYBITMAP = BitmapFactory
.DE codeStream(新FlushedInputStream(输入));
Log.e(位图,返回);
返回MYBITMAP;
}赶上(IOException异常E){
e.printStackTrace();
Log.e(getBitmapFromURL,e.getMessage());
返回null;
}
}
类FlushedInputStream扩展FilterInputStream中的{
公共FlushedInputStream(InputStream中的InputStream){
超(InputStream的);
}
众长跳跃(N久)抛出IOException异常{
长totalBytesSkipped = 0L;
而(totalBytesSkipped n种){
长bytesSkipped = in.skip(N - totalBytesSkipped);
如果(bytesSkipped == 0L){
INT =的byteValue阅读();
如果(的byteValue℃,){
打破; //我们到达EOF
} 其他 {
bytesSkipped = 1; //我们读到一个字节
}
}
totalBytesSkipped + = bytesSkipped;
}
返回totalBytesSkipped;
}
}
}
我希望现在是比较明确的。
解决方案 类OpenWorkTask扩展AsyncTask的{
@覆盖
保护布尔doInBackground(字符串... PARAMS){
// 做一点事
返回true;
}
@覆盖
保护无效onPostExecute(布尔结果){
//上述方法的结果
//处理结果在这里
myHandler.sendEmptyMessage(0);
}
}
处理程序将myHandler =新的处理程序(){
@覆盖
公共无效的handleMessage(信息MSG){
开关(msg.what){
情况下0:
//调用从其他pleaces这个功能
//做事的通知调用方法
打破;
默认:
打破;
}
}
};
I know that the first you gonna this is... why the heck on the world you then use AsyncTask.
So here is my problem i am working on some Android app (API 7 for android 2.1 or higher) , and i am testing on emulator and everything was fine, so then i tested on HTC Sensation and it says NetworkOnMainThreadExeption!
I was downloading some pictures and then draw on the map.
So to solve this problem every (internet connection) in this case downloading the pictures i must put on AsyncTask to work.
So i need a method how to know when all pictures are done so i can start drawing..
I was trying so much and no result i have no idea. I got one solution with handler but if run on slower net i get nullpointer(because the pictures are not downloaded).
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;
}
}
}
i hope now is more clear.
解决方案class OpenWorkTask extends AsyncTask {
@Override
protected Boolean doInBackground(String... params) {
// do something
return true;
}
@Override
protected void onPostExecute(Boolean result) {
// The results of the above method
// Processing the results here
myHandler.sendEmptyMessage(0);
}
}
Handler myHandler = new Handler() {
@Override
public void handleMessage(Message msg) {
switch (msg.what) {
case 0:
// calling to this function from other pleaces
// The notice call method of doing things
break;
default:
break;
}
}
};