在编写从网络下载图片的程序时,遇到了Failed binder transaction。在onPostExecute()
方法中有如下代码片段Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
//跟新ImageView
imageBox.setImageBitmap(bitmap);
。我猜想是不是这里出了问题。Google了一下,说是图片的大小不能超过40k,于是我换了一张小于40k的图片。但是不但没解决,反而又出了另一个错误:
这里贴出主要代码,以便大家帮忙给出建议和解决办法。
package com.example.asyncmessagehandler;
import java.io.IOException;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.HttpStatus;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.util.EntityUtils;
import android.app.Activity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends Activity {
private Button download = null;
private ImageView imageBox = null;
private ProgressDialog progressDialog;
public static final String IMAGE_PATH = "http://a0.att.hudong.com/15/37/20300415460903132947371591450.gif";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
download = (Button)findViewById(R.id.download);
imageBox = (ImageView)findViewById(R.id.img);
progressDialog = new ProgressDialog(MainActivity.this);
progressDialog.setTitle("提示信息");
progressDialog.setMessage("正在下载, 请稍后...");
//不允许手动取消这个弹出框
progressDialog.setCancelable(false);
//设置ProgressDialog的样式是圆圈
progressDialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
download.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
//在UI Thread当中实例化AsyncTask对象,并调用execute方法
new MyAsyncTask().execute(IMAGE_PATH);
}
});
}
public class MyAsyncTask extends AsyncTask<String, Integer, byte[]>{
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
progressDialog.show();
}
@Override
protected byte[] doInBackground(String... params) {
// TODO Auto-generated method stub
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(params[0]);
byte[] images = new byte[]{};
try {
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
if(httpEntity != null && response.getStatusLine().getStatusCode() == HttpStatus.SC_OK){
images = EntityUtils.toByteArray(httpEntity);
}
} catch (ClientProtocolException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}finally{
//关闭连接
httpClient.getConnectionManager().shutdown();
}
return images;
}
@Override
protected void onProgressUpdate(Integer... values) {
// TODO Auto-generated method stub
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(byte[] result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
//将doInBackground方法返回的byte[]解码成要给Bitmap
Bitmap bitmap = BitmapFactory.decodeByteArray(result, 0, result.length);
//跟新ImageView
imageBox.setImageBitmap(bitmap);
//progressDialog消失
progressDialog.dismiss();
}
}
}
建议你使用xutil