简体   繁体   中英

How do I load an image to an apk in delphi XE8

I am working on an app in Delphi XE8.

When I run the program on my phone, it gives me an error:

Loading bitmap failed(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:

Standard RTL Path Functions across the Supported Target Platforms

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:

Deploying and accessing local files on iOS and 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.

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

...

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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