[英]android update imageview from outside UI thread
I'm trying to listen for a connection that sends a picture to my android then set this image to an image view. 我正在尝试侦听将图片发送到android的连接,然后将此图像设置为图像视图。
the process should be done automatically to simulate a push content behavior. 该过程应自动完成以模拟推送内容行为。
this code is in TCPConHandler class 这段代码在TCPConHandler类中
public void listenForIncomingImageFlag() {
new Thread(new Runnable() {
@Override
public void run() {
try {
FlagScan = new Scanner(mySocket.getInputStream());
while (true) {
Flag = FlagScan.nextInt();
Log.d("FLagImage", "Flag ="+Flag);
if (Flag == 1) {
listenForIncomingImage(true);
Flag = 0;
}
}
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}).start();
}
as soon as the thread running the tcp connection return a socket this function will be called . 一旦运行tcp连接的线程返回一个套接字,该函数就会被调用。 then it start another function the listenForIncomingImage.
然后它启动另一个函数listenForIncomingImage。
public void listenForIncomingImage(boolean b) {
check = b;
Thread InComImage = new Thread(new Runnable() {
@Override
public void run() {
try {
inComImageBitmap = BitmapFactory.decodeStream(mySocket
.getInputStream());
MainActivity.setImage(inComImageBitmap, true);
check = false;
Log.d("Bitmap", "Bitmap receviced and sent to method");
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
});
InComImage.start();
}
thing is now to pass the image to imageUpdate function in mainactivity if i am using runOnUiThread i get an error for calling non static method. 现在,如果我正在使用runOnUiThread,则将图像以mainactivity的形式传递给imageUpdate函数,但会因调用非静态方法而出错。
public void imageUpdate() {
new Thread(new Runnable() {
@Override
public void run() {
while (true) {
runOnUiThread(new Runnable() {
public void run() {
while(imageCheck == true){
myImage.setImageBitmap(finalImage);
imageCheck = false;}
}
});
}
}
}).start();
}
so i had to start this thread when the mainactivity and block it somehow using the imagecheck boolean that is sent from the tcpConHandler class when it receive the bitmap. 所以我必须在mainactivity时启动此线程,并使用tcpConHandler类在收到位图时发送的imagecheck布尔值以某种方式阻止它。
doing such cause the app to overload the memory and crash is there a better way to simulate push content behavior on android . 这样做会导致应用程序过载内存并崩溃,这是在Android上模拟推送内容行为的更好方法。
also is there anyway to have a function on the mainactivity that runs a thread with while(true) block inside it that can update the ui and can be static ? 还是在mainactivity上有一个函数运行一个带有while(true)块的线程,该线程可以更新ui并可以是静态的? .
。 or any similar implementation for such.
或任何类似的实现方式。
if i am using runOnUiThread i get an error for calling non static method
如果我使用runOnUiThread,我会因调用非静态方法而出错
because you will need to send MainActivity
context to tcpConHandler
for accessing runOnUiThread
from non-activity class instead of calling runOnUiThread
in static way. 因为您需要将
MainActivity
上下文发送到tcpConHandler
以便从非活动类访问runOnUiThread
,而不runOnUiThread
静态方式调用runOnUiThread
。
create an tcpConHandler
constructor for passing Activity context as: 创建一个
tcpConHandler
构造函数以将Activity上下文传递为:
public class TcpConHandler{
Activity activity;
public TcpConHandler(Activity activity){
this.activity=activity;
}
//...your code here..
public void imageUpdate() {
//...your code here...
activity.runOnUiThread(new Runnable() {
public void run() {
// update imageView here...
}
});
}
from MainActivity create TcpConHandler object as by sending activity instance; 从MainActivity通过发送活动实例创建TcpConHandler对象;
TcpConHandler tchConhandler=new TcpConHandler(this);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.