Dynamic Loadfrom file image is giving Access vailation on Firemonkey Mobile – Firemonkey

Photo of author
Written By M Ibrahim
android delphi delphi-xe6 firemonkey

Quick Fix: Call img.MultiResBitmap.Add(); before accessing img.MultiResBitmap.Items[0]. If you don’t need multi-resolution, use img.Bitmap instead.

The Problem:

A programmer is creating a mobile application using Firemonkey, where they want to display images in a list box. They are loading the images dynamically at runtime using the LoadFromFile method of the TImage component. However, they are encountering a "segmentation 11" exception when running the application on a mobile device. They are unable to display the images dynamically on the phone using the code they have provided.

The Solutions:

Solution 1: Call `img.MultiResBitmap.Add()` method before loading the image from file

The img.MultiResBitmap.Items[0] property can only be accessed after creating a new bitmap item and adding it to the img.MultiResBitmap collection, which can be achieved by calling the img.MultiResBitmap.Add() method.

Code Example:

begin
  ...
  // Create a new bitmap item and add it to the collection
  img.MultiResBitmap.Add;
  
  // Now you can use img.MultiResBitmap.Items[0]
  img.MultiResBitmap.Items[0].Bitmap.LoadFromFile('path/to/image.png');
  ...
end;

Alternatively, you can also use the img.Bitmap property directly if you don’t need multi-resolution images:

begin
  ...
  img.Bitmap.LoadFromFile('path/to/image.png');
  ...
end;