简体   繁体   English

如何在Delphi XE8中将图像加载到APK

[英]How do I load an image to an apk in delphi XE8

I am working on an app in Delphi XE8. 我正在使用Delphi XE8开发一个应用程序。

When I run the program on my phone, it gives me an error: 当我在手机上运行程序时,它给我一个错误:

Loading bitmap failed(image.png) 加载位图失败(image.png)

My code works as follows: 我的代码如下:

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
  ...

Please note the image is saved in the same folder as my program. 请注意,图像与我的程序保存在同一文件夹中。

Don't use relative paths. 不要使用相对路径。 Always use absolute paths. 始终使用绝对路径。

You need to use the Deployment Manager to deploy the image file to an appropriate folder on the phone, and then use the System.IOUtils.TPath class to locate that folder at runtime: 您需要使用Deployment Manager将映像文件部署到电话上的适当文件夹,然后使用System.IOUtils.TPath类在运行时查找该文件夹:

Standard RTL Path Functions across the Supported Target Platforms 跨支持的目标平台的标准RTL路径功能

On Android, deploy the image file to the ./assets/internal folder, and then use the TPath.GetDocumentsPath() method at runtime, as documented on this blog: 在Android上,将映像文件部署到./assets/internal文件夹,然后在运行时使用TPath.GetDocumentsPath()方法,如本博客中所述:

Deploying and accessing local files on iOS and Android 在iOS和Android上部署和访问本地文件

What the EDN documentation and blog both fail to mention is that you also need to add the System.StartupCopy unit to your app's uses clause. EDN文档和博客都没有提到的是,您还需要将System.StartupCopy单元添加到应用程序的uses子句中。

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

...

img.bitmap.LoadFromFile(TPath.Combine(TPath.GetDocumentsPath, 'Image.png'));

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM