XE5的Andr​​oid TBitmap.LoadFromStream失败,一个线程内线程、Andr、oid、LoadFromStream

2023-09-06 23:03:21 作者:那么不在意哈

我创建使用Delphi XE5一个简单的游戏为Android。我有一些资源,PNG和JPEG,我想表现出一个载入画面,而我的程序加载了所有的资源。

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.

但我发现把TBitmap.LoadFromFile,或TBitmap.LoadFromStream code一款Android线程内,导致应用程序立即退出并返回到启动,在调试模式下德尔福甚至不捕捉异常。 (在code工作完全在Windows上,并没有对Android的线程)

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)

我不得不打开logcat中看到了什么,我看到了类似错误创建绘图环境。

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

我的问题是有没有办法让使用Delphi XE5加载屏幕为Android?这样一个进度屏幕显示图像的同时在内存中被加载。

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.

我创建的测试项目只是为了找出问题,这里有结果。 LoadFromFile是线程1日志表明线程居然跑,但例外提出了后来???

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的截图: 来源$ C ​​$ C: http://www.pockhero.com /wp-content/uploads/2013/10/threadtest1.7z

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.

我知道这是一个有点古怪。我试图把FreeOnTerminate关闭,但它只能控制线程对象,而不是实际的线程。它看起来好像syncrhonized呼叫未syncrhonized。我没有时间和地点的位图实际上是被使用或复制的想法。 Possiblly还有另外一个GUI线程的地方,因为德尔福编译Android的LIB code没有在主线程中运行反正。

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.

下面正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;