线程内的XE5 Android TBitmap.LoadFromStream失败 [英] XE5 Android TBitmap.LoadFromStream fail inside a thread

查看:252
本文介绍了线程内的XE5 Android TBitmap.LoadFromStream失败的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Delphi XE5为Android创建一个简单的游戏。我有一些资源,PNG和Jpegs,我想显示加载屏幕,而我的程序加载所有的资源。



但我发现放置TBitmap.LoadFromFile,或TBitmap.LoadFromStream代码里面一个android线程,导致App立即退出并返回到Launcher,在调试模式下,Delphi甚至不会捕获异常。 (该代码在Windows上完美运行,而在Android上没有线程)



我不得不打开logcat来看看发生了什么,我看到像创建绘图上下文 。



我的问题是,有没有办法使用Delphi XE5加载Android屏幕?所以当图像被加载到内存中时,进度屏幕显示。






我创建的测试项目只是为了隔离问题,在这里是结果。 LoadFromFile是线程1.日志建议线程实际运行,但异常被引发之后???



Logcat截图:
源代码: http://www.pockhero.com/wp-content/uploads/2013/10/threadtest1.7z

解决方案

经过很多测试和同事的帮助,我的同事和我解决了这个问题。解决方案不是终止线程并保持线程运行。



我知道这有点奇怪。我试图关闭FreeOnTerminate,但它只控制线程对象,而不是实际的线程。看起来似乎是syncrhonized呼叫没有被syncrhonized。我不知道位图实际被使用或复制的位置和位置。可能有另一个GUI线程在某个地方,因为Delphi编译的Android lib代码不会在主线程中运行。



这里是工作代码。

 程序TBitmapThread.Execute; 
开始
继承;
BeforeExecute;
try
fBitmap:= TBitmap.CreateFromFile(TPath.Combine(TPath.GetDocumentsPath,'koala.jpg'));
//睡眠(2000);
同步(UpdateImage);
//保持线程运行
而不终止do
begin
睡眠(100);
结束
fBitmap.Free;
除了
在E:Exception do
begin
Log.d('TestThread Exception:'+ E.message);
结束
结束
AfterExecute;
结束


I am creating a simple game for Android using Delphi XE5. I have a few resources, PNGs and Jpegs, I wanted to show a loading screen while my program loads up all the resources.

But I found putting TBitmap.LoadFromFile, or TBitmap.LoadFromStream code inside a android thread, caused the App to quit immediately and return to Launcher, in debug mode Delphi don't even catch an exception. (The code works perfectly on Windows, and without thread on Android)

I had to open logcat to see what went on, I saw something like "Error creating drawing context".

My question is there a way to make a loading screen for Android using Delphi XE5? So that a progress screen shows while images gets loaded in memory.


I created test project just to isolate the problem, here are the result. LoadFromFile is Thread 1. The log suggests thread actually ran, but Exceptions were raised afterwards???

Logcat screenshot: Source code: http://www.pockhero.com/wp-content/uploads/2013/10/threadtest1.7z

解决方案

After a lots of testing and colleague's help, my colleague and I solved the problem. The solution is not to terminate the thread and keep the thread running.

I know this is a bit odd. I tried to turn FreeOnTerminate off, but it only control the thread object, not the actually thread. It appears as if syncrhonized call is not syncrhonized. I have no idea when and where the bitmap is actually being used or copied. Possiblly there is another GUI thread somewhere, since Delphi compiled Android lib code don't run in the main thread anyway.

Here is working code.

procedure TBitmapThread.Execute;
begin
  inherited;
  BeforeExecute;
  try
    fBitmap := TBitmap.CreateFromFile(TPath.Combine(TPath.GetDocumentsPath, 'koala.jpg'));
    // Sleep(2000);
    Synchronize(UpdateImage);
    // Keep the thread running
    while not Terminated do
    begin
      Sleep(100);
    end;
    fBitmap.Free;
  except
    on E:Exception do
    begin
      Log.d('TestThread Exception: ' + E.message);
    end;
  end;
  AfterExecute;
end;

这篇关于线程内的XE5 Android TBitmap.LoadFromStream失败的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆