Android Delphi Firemonkey last image filename

  

Does anyone have a simpler method of getting the filename of the last image?

(I modified this from another post. It may not even work if there are multiple albums on the device.

This sure seems like a lot of code for the simple function of finding the last image filename after you take a picture.

Thanks
robert

unit Pictures_Android;
//currently android only

interface

uses
System.Generics.Collections;

type
TPicture = class(TObject)
protected
FID: Integer;
FPath: string;
public
property ID: Integer read FID;
property Path: string read FPath;
end;

TPictureList = class(TList<TPicture>)
private
FTitle: string;
procedure AddPicture(const AID: Integer; const APath: string);
protected
FID: string;
public
procedure Load;

constructor Create(const ATitle: string);
property ID: string read FID;
property Title: string read FTitle;
end;

TPictureLibrary = class(TList<TPictureList>)
public
procedure LoadLists;
end;

implementation

uses

Androidapi.JNI.JavaTypes, Androidapi.JNI.Provider, Androidapi.JNI.Net, Androidapi.JNI.GraphicsContentViewText,
Androidapi.JNIBridge, Androidapi.Helpers;

{ TPictureLibrary }

procedure TPictureLibrary.LoadLists;
var
LBaseURI: Jnet_Uri;
LProjection: TJavaObjectArray<JString>;
LOrderBy: JString;
LCursor: JCursor;
LTitle, LID: string;
LList: TPictureList;
begin
LBaseURI := TJImages_Media.JavaClass.EXTERNAL_CONTENT_URI;
LProjection := TJavaObjectArray<JString>.Create(2);
LProjection[0] := StringToJString(‘DISTINCT ‘ ).concat(TJImages_ImageColumns.JavaClass.BUCKET_ID);
LProjection[1] := TJImages_ImageColumns.JavaClass.BUCKET_DISPLAY_NAME;
LOrderBy := TJImages_ImageColumns.JavaClass.BUCKET_DISPLAY_NAME.concat(StringToJString(‘ ASC’));
LCursor := TAndroidHelper.ContentResolver.query(LBaseURI, LProjection, nil, nil, LOrderBy);
if (LCursor <> nil) and LCursor.moveToFirst then
begin
repeat
LID := JStringToString(LCursor.getString(LCursor.getColumnIndex(TJImages_ImageColumns.JavaClass.BUCKET_ID)));
LTitle :=
JStringToString(LCursor.getString(LCursor.getColumnIndex(TJImages_ImageColumns.JavaClass.BUCKET_DISPLAY_NAME)));
LList := TPictureList.Create(LTitle);
LList.FID :=
JStringToString(LCursor.getString(LCursor.getColumnIndex(TJImages_ImageColumns.JavaClass.BUCKET_ID)));
Add(LList);
until not LCursor.moveToNext;
end;
end;

{ TPictureList }

constructor TPictureList.Create(const ATitle: string);
begin
inherited Create;
FTitle := ATitle;
end;

procedure TPictureList.AddPicture(const AID: Integer; const APath: string);
var
LPicture: TPicture;
begin
LPicture := TPicture.Create;
LPicture.FID := AID;
LPicture.FPath := APath;
Add(LPicture);
end;

procedure TPictureList.Load;
var
LBaseURI: Jnet_Uri;
LProjection, LSelectionArgs: TJavaObjectArray<JString>;
LSelection: JString;
LCursor: JCursor;
LPath: string;
LID: Integer;
begin
LBaseURI := TJImages_Media.JavaClass.EXTERNAL_CONTENT_URI;
LProjection := TJavaObjectArray<JString>.Create(2);
LProjection[0] := TJBaseColumns.JavaClass._ID; // TJImages_ImageColumns.JavaClass._ID;
LProjection[1] := TJMediaStore_MediaColumns.JavaClass.DATA;
LSelection := TJImages_ImageColumns.JavaClass.BUCKET_ID.concat(StringToJString(‘ = ?’));
LSelectionArgs := TJavaObjectArray<JString>.Create(1);
LSelectionArgs[0] := StringToJString(FID);
LCursor := TAndroidHelper.ContentResolver.query(LBaseURI, LProjection, LSelection, LSelectionArgs, nil);
if (LCursor <> nil) and LCursor.moveToLast then // get just last picture
begin
repeat
LID := LCursor.getInt(LCursor.getColumnIndex(TJBaseColumns.JavaClass._ID));
LPath := JStringToString(LCursor.getString(LCursor.getColumnIndex(TJMediaStore_MediaColumns.JavaClass.DATA)));
AddPicture(LID, LPath);
until not LCursor.moveToNext;
end;
end;

end.

Comments are closed.