0

As the title says, loading bitmaps into TListViewItems causes ANDROID apps to crash. Works for iOS, but not for Android. Picture Paths are CORRECT. For simplicity sakes, you can just drop a TImage onto the form, and manually set one image to it, then try instead of going through a loop as I did, just set LItem.bitmap := TImage1.Bitmap. Is it something I am doing wrong? or can someone confirm this and possibly help me out with a work around for now.

Delphi XE7 Android

if MenuList.Items.Count=0 then  // load menu options + icons
  begin
    for i := 0 to Length(PicturePaths)-1 do
    begin
      LItem:=MenuList.Items.Add;
      LItem.Text := PictureNames[i];
      LItem.Bitmap.LoadFromFile(TPath.GetHomePath+PathDelim+PicturePaths[i]);
    end;
  end;
ThisGuy
  • 1,405
  • 1
  • 24
  • 51
  • Did you check the resulting filename? BTW you should use `TPath.Combine( TPath.GetHomePath, PicturePaths [i] )` because it is safe on all platforms. – Sir Rufo Oct 15 '14 at 16:25
  • Yes I did. And as I noted, even setting image from another image that is already displayed on the form doesn't work. I have this code snipped in the 'onShow' method of this form and it causes app to hang, but when I try to set iamges of listitem from an event after form is already shown, such as an click event, then it just doesn't do anything - no bitmap set. I will try using TPath.Combine now – ThisGuy Oct 15 '14 at 16:31
  • Ok, i got the "bug" and updated my answer :o) – Sir Rufo Oct 15 '14 at 19:42

1 Answers1

1

Update

This happens if the TListView.ItemAppearance.Item.Appearance is not set to one of the options starting with ImageListItem. In this situation the Bitmap property is not assigned.

Since XE7 it is possible to have different settings for the controls for different devices. So it is possible to have set ImageListItem and for any other device ListItem and you will get the exception.


Code used in sample project:

procedure TForm1.SpeedButton1Click( Sender: TObject );
var
  LItem: TListViewItem;
  LImageFilename: string;
begin
  LItem := ListView1.Items.Add;

  LImageFilename := TPath.Combine( TPath.GetHomePath, 'sample.png' );
  LItem.Text := LImageFilename;
  // just be sure that the file exists and the bitmap is assigned
  if TFile.Exists( LImageFilename ) and Assigned( LItem.Bitmap )
  then
    LItem.Bitmap.LoadFromFile( LImageFilename );
end;

Link to complete project source and compiled apk

Sir Rufo
  • 18,395
  • 2
  • 39
  • 73
  • 1
    Just to be clear for anyone else; your code is functionally equivalent, but you just added a couple lines of sanity-checking code, right? – Andrew Barber Oct 15 '14 at 17:51
  • To confirm - this worked on an actual Android Device? It works for me in iOS – ThisGuy Oct 15 '14 at 18:37
  • @AndrewBarber Yes, this just contains some checking if the file is found and the bitmap is assigned. But in the end I see the text and the image as expected – Sir Rufo Oct 15 '14 at 18:47
  • Tested on Galaxy Note 10.1 2014 (SM-P605) with Android 4.4.2 – Sir Rufo Oct 15 '14 at 18:49