android客户端从服务器下载图片显示到本地出现错误!求帮助
/* MainActivity类*/
package com.example.downloadimage;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.Socket;
import java.net.UnknownHostException;
import android.os.Bundle;
import android.app.Activity;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.util.Log;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
private Button mbt1, mbt2;
private ImageView miV1;
private EditText meT1;
private myClickListener mcl;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setTitle("下载图片器");
setContentView(R.layout.activity_main);
mbt1 = (Button) this.findViewById(R.id.main_bt1);
mbt2 = (Button) this.findViewById(R.id.main_bt2);
miV1 = (ImageView) this.findViewById(R.id.main_iv1);
meT1 = (EditText) this.findViewById(R.id.main_et1);
mcl = new myClickListener();
mbt1.setOnClickListener(mcl);
mbt2.setOnClickListener(mcl);
}
/*继承了OnClickListener的内部类*/
private class myClickListener implements OnClickListener {
@Override
public void onClick(View v) {
// TODO Auto-generated method stub
if (v == mbt2) {
meT1.setText("");
// 取消
} else {
new Net(miV1).start();
}
}
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
/*Net类*/
package com.example.downloadimage;
import java.io.IOException;
import java.io.InputStream;
import java.net.InetAddress;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.net.SocketAddress;
import java.net.UnknownHostException;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.Drawable;
import android.location.Address;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;
/*下载图片的线程*/
public class Net extends Thread {
public static int PROT = 6666;
public static String IP = new String("127.0.0.1");
private ImageView miV1;
private MainActivity mma;
private byte[] b;
public Net(ImageView iV1) {
// TODO Auto-generated constructor stub
miV1 = iV1;
}
public void run() {
// TODO Auto-generated method stub
Socket socket;
try {
/* 有错 */
socket = new Socket(InetAddress.getByName(IP), PROT);
Log.e("xxxxxxxx", "xxxx");
InputStream is = socket.getInputStream();
b = Tool.useTool(is);
socket.close();
} catch (UnknownHostException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
miV1.post(new Runnable() {
public void run() {
// TODO Auto-generated method stub
Log.e("xxxxxxxx", "xxxx"+b.length);
miV1.setImageBitmap(BitmapFactory.decodeByteArray(b, 0,
b.length));
}
});
}
}
/*Tool类*/
package com.example.downloadimage;
import java.io.BufferedInputStream;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
public class Tool {
/*将一个输入流转换为字节数组*/
public static final byte[] useTool(InputStream is) {
byte[] b = new byte[256];
BufferedInputStream bis = new BufferedInputStream(is);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
int len = -1;
try {
while ((len = bis.read(b)) != -1) {
bao.write(b);
}
bis.close();
bao.close();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
return bao.toByteArray();
}
}
错误 求帮助! --------------------编程问答-------------------- 我没用socket下载,我用的是地址直接下载,然后转换成流 直接显示,当然也可以做本地缓存 --------------------编程问答-------------------- 除
补充:移动开发 , Android