如何在delphi XE8中将图像加载到apk

时间:2016-07-30 14:52:24

标签: android delphi bitmap

我正在使用Delphi XE8中的应用程序。

当我在手机上运行程序时,它会给我一个错误:

  

加载位图失败(image.png)

我的代码如下:

if ListBox1.ItemIndex = 0 then
begin
  img.bitmap.LoadFromFile('Image.png');
  iMin:= Round(iNumber * 1);
  iMax:= Round(iNumber *13.24);
  iAvg:= Round(iNumber * 2.59);
  label7.Text:= inttostr(iMin);
  label5.Text:= inttostr(iAvg);
  label6.Text:= inttostr(iMax);
  label2.Text:= 'Minimum';
  label3.Text:= 'Average';
  label4.Text:= 'Maximum';
end
else
  ...

请注意图像与我的程序保存在同一文件夹中。

1 个答案:

答案 0 :(得分:2)

不要使用相对路径。始终使用绝对路径。

您需要使用Deployment Manager将图像文件部署到手机上的相应文件夹中,然后使用System.IOUtils.TPath类在运行时找到该文件夹​​:

Standard RTL Path Functions across the Supported Target Platforms

在Android上,将映像文件部署到./assets/internal文件夹,然后在运行时使用TPath.GetDocumentsPath()方法,如本博客所述:

Deploying and accessing local files on iOS and Android

EDN文档和博客都未提及的是,您还需要将System.StartupCopy单元添加到应用的uses子句中。

uses
  ..., System.IOUtils, System.StartupCopy;

...

img.bitmap.LoadFromFile(TPath.Combine(TPath.GetDocumentsPath, 'Image.png'));
相关问题