【问题标题】:How do I load an image to an apk in delphi XE8如何在delphi XE8中将图像加载到apk
【发布时间】:2016-07-30 14:52:24
【问题描述】:

我正在使用 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
  ...

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

【问题讨论】:

    标签: android delphi bitmap


    【解决方案1】:

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

    您需要使用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'));
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-13
      • 2015-10-03
      • 2015-08-19
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多