如何从另一个线程(Android 处理程序)在图像视图中显示位图
How display bitmap in imageview from another thread (Android handler)
我正在另一个线程中通过 TCP 从服务器下载图像,我想在 activity 中的 ImageView 中显示它。我创建了处理程序,但无法正常工作(不显示下载的图像)。怎么了?
编辑代码:
public class TcpClient extends Activity {
ImageView imageView;
public static String aHost;
public String aSocketIn;
public static int aSocketInInt;
private Handler uiHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_result);
imageView = (ImageView) findViewById(R.id.imageView);
Intent intent = getIntent();
aHost = intent.getStringExtra("addressIp");
aSocketIn = intent.getStringExtra("socketIn");
aSocketInInt = Integer.parseInt(aSocketIn);
uiHandler = new Handler();
ClientInThread clientInThread = new ClientInThread(aHost,aSocketInInt,uiHandler,imageView);
(new Thread(clientInThread)).start();
} }
class ClientInThread extends Thread implements Runnable {
public Bitmap bitmap = null;
public Handler handler;
String Host;
int SocketIn;
ImageView imageView;
ClientIn clientIn;
ClientInThread(String Host, int SocketIn, Handler handler, ImageView imageView) {
this.Host = Host;
this.SocketIn = SocketIn;
this.handler = handler;
this.imageView = imageView;
}
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
try {
InetAddress serwerAddress = InetAddress.getByName(Host);
Socket socket = new Socket(serwerAddress, SocketIn);
clientIn = new ClientIn(socket);
bitmap = clientIn.Receive();
}
catch (Exception e) {
e.printStackTrace();
}
} }
您误用了处理程序。
您应该像这样获取 UI 线程的处理程序,而不是使用处理程序的 handleMessage
方法:
...
private Handler uiHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_result);
...
uiHandler = new Handler();
...
}
...
public void run() {
try {
InetAddress serwerAddress = InetAddress.getByName(Host);
Socket socket = new Socket(serwerAddress, SocketIn);
clientIn = new ClientIn(socket);
bitmap = clientIn.Receive();
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap == null) {
// Downloading error
return;
}
// When a bitmap is downloaded you do:
uiHandler.post(new Runnable() {
imageView.setImageBitmap(bitmap);
});
...
}
如果您不限于使用第三方库
您可以使用非常酷的库 UniversalImageLoader 来完成此类任务。看这里 https://github.com/nostra13/Android-Universal-Image-Loader.
使用 AsyncTask
进行网络操作。
class ImageDownloader extends AsyncTask<Void, Void, Bitmap> {
private String host;
private int socketIn;
private ImageView imageView;
public ImageDownloader(String host, int socketIn, ImageView imageView) {
this.host = host;
this.socketIn = socketIn;
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
InetAddress serwerAddress = InetAddress.getByName(host);
Socket socket = new Socket(serwerAddress, socketIn);
clientIn = new ClientIn(socket);
bitmap = clientIn.Receive();
return bitmap;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
}
在 onCreate
开始
new ImageDownloader(aHost, aSocketInInt, imageView).execute();
我正在另一个线程中通过 TCP 从服务器下载图像,我想在 activity 中的 ImageView 中显示它。我创建了处理程序,但无法正常工作(不显示下载的图像)。怎么了?
编辑代码:
public class TcpClient extends Activity {
ImageView imageView;
public static String aHost;
public String aSocketIn;
public static int aSocketInInt;
private Handler uiHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_result);
imageView = (ImageView) findViewById(R.id.imageView);
Intent intent = getIntent();
aHost = intent.getStringExtra("addressIp");
aSocketIn = intent.getStringExtra("socketIn");
aSocketInInt = Integer.parseInt(aSocketIn);
uiHandler = new Handler();
ClientInThread clientInThread = new ClientInThread(aHost,aSocketInInt,uiHandler,imageView);
(new Thread(clientInThread)).start();
} }
class ClientInThread extends Thread implements Runnable {
public Bitmap bitmap = null;
public Handler handler;
String Host;
int SocketIn;
ImageView imageView;
ClientIn clientIn;
ClientInThread(String Host, int SocketIn, Handler handler, ImageView imageView) {
this.Host = Host;
this.SocketIn = SocketIn;
this.handler = handler;
this.imageView = imageView;
}
public void run() {
handler.post(new Runnable() {
@Override
public void run() {
imageView.setImageBitmap(bitmap);
}
});
try {
InetAddress serwerAddress = InetAddress.getByName(Host);
Socket socket = new Socket(serwerAddress, SocketIn);
clientIn = new ClientIn(socket);
bitmap = clientIn.Receive();
}
catch (Exception e) {
e.printStackTrace();
}
} }
您误用了处理程序。
您应该像这样获取 UI 线程的处理程序,而不是使用处理程序的 handleMessage
方法:
...
private Handler uiHandler;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.bundle_result);
...
uiHandler = new Handler();
...
}
...
public void run() {
try {
InetAddress serwerAddress = InetAddress.getByName(Host);
Socket socket = new Socket(serwerAddress, SocketIn);
clientIn = new ClientIn(socket);
bitmap = clientIn.Receive();
} catch (Exception e) {
e.printStackTrace();
}
if (bitmap == null) {
// Downloading error
return;
}
// When a bitmap is downloaded you do:
uiHandler.post(new Runnable() {
imageView.setImageBitmap(bitmap);
});
...
}
如果您不限于使用第三方库 您可以使用非常酷的库 UniversalImageLoader 来完成此类任务。看这里 https://github.com/nostra13/Android-Universal-Image-Loader.
使用 AsyncTask
进行网络操作。
class ImageDownloader extends AsyncTask<Void, Void, Bitmap> {
private String host;
private int socketIn;
private ImageView imageView;
public ImageDownloader(String host, int socketIn, ImageView imageView) {
this.host = host;
this.socketIn = socketIn;
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(Void... params) {
try {
InetAddress serwerAddress = InetAddress.getByName(host);
Socket socket = new Socket(serwerAddress, socketIn);
clientIn = new ClientIn(socket);
bitmap = clientIn.Receive();
return bitmap;
}
catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
if (bitmap != null) {
imageView.setImageBitmap(bitmap);
}
}
}
在 onCreate
开始
new ImageDownloader(aHost, aSocketInInt, imageView).execute();