Watch, Follow, &
Connect with Us

For forums, blogs and more please visit our
Developer Tools Community.


Welcome, Guest
Guest Settings
Help

Thread: get file names with file sizes from Photos directory



Permlink Replies: 5 - Last Post: Sep 21, 2017 10:16 AM Last Post By: Dave Nottage Threads: [ Previous | Next ]
Mustafa Ersagun...

Posts: 10
Registered: 10/13/11
get file names with file sizes from Photos directory
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 27, 2017 8:56 AM
How can I get all file names with file sizes from Photos directory of ios device.

I Think we need permission from the operating system. Or we have to use any service...

For other platforms I can get all directory and files like below:

stList := TStringList.Create;
try
Lb.Clear;

try
TDirectory.GetFileSystemEntries( EPath.Text,
function(const Path: string; const SearchRec: TSearchRec): Boolean
begin
result := true;
if SearchRec.Attr = faDirectory then stList.Add( '[ ' + SearchRec.Name + ' ]')
else
if SearchRec.Attr < 72
then stList.Add( SearchRec.Name + ' ' + inttostr( round( SearchRec.Size / 1024 ) ) + ' KB' )
else
result := false;
end
);
Lb.items.Assign( stList );
except on e:Exception do
begin
Showmessage( e.message );
Exit;
end;
end;
finally
stList.disposeof;
end;

I tried with folowing directories on ios: '/var/mobile/Media/DCIM' or '/private/var/mobile/Media/DCIM'

Edited by: Mustafa Ersagun Korkmaz on Aug 27, 2017 8:57 AM

Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: get file names with file sizes from Photos directory [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Aug 28, 2017 4:19 PM   in response to: Mustafa Ersagun... in response to: Mustafa Ersagun...
Mustafa Ersagun Korkmaz wrote:

How can I get all file names with file sizes from Photos directory of ios device.

I Think we need permission from the operating system. Or we have to use any service...

The "official" way to access photos on the device is to use the Photos framework. There's a QP report here:

https://quality.embarcadero.com/browse/RSP-17603

Where Horacio helped me work out a particular issue I was having - I mistakenly thought it was a feature that needed to
be added, and it turned out to be just that Wrap does not need to be used for the PHImageManager instance. The report
has an example project that may help you get started. I have a complete solution, however it's planned to be part of a
commercial library.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Mustafa Ersagun...

Posts: 10
Registered: 10/13/11
Re: get file names with file sizes from Photos directory [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 7, 2017 8:07 AM   in response to: Dave Nottage in response to: Dave Nottage
Thanks Dave. I have reviewed your code today.

And tried somethink like below But I could not get the file names and sizes.

iOSapi.Photos,
Macapi.Helpers,
iOSapi.Foundation,
iOSapi.UIKit;

procedure TForm1.bRequestAuthoClick(Sender: TObject);
begin
TPHPhotoLibrary.OCClass.requestAuthorization( OnRequestAuthorization );
end;

procedure TForm1.OnRequestAuthorization(status: PHAuthorizationStatus);
var i: integer;

LFetchResult: PHFetchResult;
FAsset : PHAsset;

LImageSize: CGSize;
LImageOptions: PHImageRequestOptions;
LImageManager: PHImageManager;

begin
if status = PHAuthorizationStatusAuthorized then
begin
bRequestAutho.Enabled := false;

LFetchResult := TPHAsset.OCClass.fetchAssetsWithMediaType(PHAssetMediaTypeImage, nil);
if LFetchResult.count > 0 then
begin
for i := 0 to LFetchResult.count - 1 do
begin
FAsset := TPHAsset.Wrap( LFetchResult.objectAtIndex( i ) );
try
FAsset.retain;

LImageSize.width := 1024;
LImageSize.height := 1024;

LImageOptions := TPHImageRequestOptions.Wrap(TPHImageRequestOptions.Alloc.init);
LImageOptions.setSynchronous(false);
LImageOptions.setDeliveryMode(PHImageRequestOptionsDeliveryModeHighQualityFormat);
LImageOptions.setResizeMode(PHImageRequestOptionsResizeModeFast);

LImageManager := TPHImageManager.OCClass.defaultManager;
LImageManager.requestImageForAsset(FAsset, LImageSize, PHImageContentModeAspectFit, LImageOptions, OnRequestImage);

LImageOptions.release;

{Lb.items.add( NSStrToStr( FAsset.burstIdentifier ) + ' - ' +
inttostr( FAsset.pixelWidth ) + 'X' + inttostr( FAsset.pixelHeight ) ); }
finally
FAsset.release;
end;
end;
end;
end;
end;

procedure TForm1.OnRequestImage(result: UIImage; info: NSDictionary);
begin
Lb.items.add( NSStrToStr( info.fileGroupOwnerAccountName ) + ' ' + NSStrToStr( info.fileType ) + ' ' +
inttostr( info.fileSize ) + ' ' +
inttostr( trunc( result.size.width ) ) + 'X' + inttostr( trunc( result.size.height ) ) );
end;

Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: get file names with file sizes from Photos directory [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 7, 2017 2:16 PM   in response to: Mustafa Ersagun... in response to: Mustafa Ersagun...
Mustafa Ersagun Korkmaz wrote:

And tried somethink like below But I could not get the file names and sizes.

I should have mentioned that API will not allow you to obtain the actual file names. You might be able to determine the
size from the sizes of the images.

On iOS, for file names of photos taken/stored outside of your own application, I expect you're going to be out of luck.

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Mustafa Ersagun...

Posts: 10
Registered: 10/13/11
Re: get file names with file sizes from Photos directory [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 21, 2017 2:58 AM   in response to: Dave Nottage in response to: Dave Nottage
Thank you Dave and Allen Drennan.

All done: file size, file name, file date

unit Unit1;

interface

uses
System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs,
FMX.Controls.Presentation, FMX.StdCtrls, FMX.Layouts, FMX.ListBox,
iOSapi.Photos,
Macapi.Helpers,
iOSapi.Foundation,
iOSapi.UIKit;

type
TForm1 = class(TForm)
Lb: TListBox;
bRequestAutho: TButton;
procedure bRequestAuthoClick(Sender: TObject);
private
Gstr : String;
procedure OnRequestAuthorization(status: PHAuthorizationStatus);
procedure OnresultHandler (param1: NSData; param2: NSString; param3: iOsApi.Photos.UIImageOrientation; param4: NSDictionary);
public
{ Public declarations }
end;

var
Form1: TForm1;

implementation

{$R *.fmx}


procedure TForm1.bRequestAuthoClick(Sender: TObject);
begin
TPHPhotoLibrary.OCClass.requestAuthorization( OnRequestAuthorization );
end;

procedure TForm1.OnRequestAuthorization(status: PHAuthorizationStatus);
var i: integer;
FAsset : PHAsset;
LImageOptions: PHImageRequestOptions;
LImageManager: PHImageManager;
LFetchResult: PHFetchResult;

NSA: NSArray;
AssetResource: PHAssetResource;
StList: TStringList;
begin
if status = PHAuthorizationStatusAuthorized then
begin
bRequestAutho.Enabled := false;
lb.items.Clear;
LFetchResult := TPHAsset.OCClass.fetchAssetsWithMediaType(PHAssetMediaTypeImage, nil);
if LFetchResult.count > 0 then
begin
LImageOptions := TPHImageRequestOptions.Wrap(TPHImageRequestOptions.Alloc.init);
LImageOptions.setSynchronous({false}true);
LImageOptions.setDeliveryMode(PHImageRequestOptionsDeliveryModeHighQualityFormat);//PHImageRequestOptionsDeliveryModeFastFormat
LImageOptions.setResizeMode(PHImageRequestOptionsResizeModeFast);

StList := TStringList.Create;
LImageManager := TPHImageManager.OCClass.defaultManager;
try
for i := 0 to LFetchResult.count - 1 do
begin
FAsset := TPHAsset.Wrap( LFetchResult.objectAtIndex( i ) );

LImageManager.requestImageDataForAsset( FAsset, LImageOptions, OnresultHandler );

NSA := TPHAssetResource.OCClass.assetResourcesForAsset( FAsset );
if NSA.count > 0 then
begin
AssetResource := TPHAssetResource.Wrap( NSA.objectAtIndex( 0 ) );
Gstr := Gstr + ' ' + NSStrToStr( AssetResource.originalFilename );
end;

Gstr := Gstr + ' ' + inttostr( FAsset.pixelWidth ) + 'X' + inttostr( FAsset.pixelHeight ) + ' ' +
formatdatetime( 'dd.mm.yyyy hh:nn', NSDateToDateTime( FAsset.ModificationDate ) );

StList.Add( Gstr );
end;
Lb.items.Assign( StList );
finally
LImageOptions.release;
StList.disposeof;
end;
end;

end;
end;

procedure TForm1.OnresultHandler (param1: NSData; param2: NSString; param3: iOsApi.Photos.UIImageOrientation; param4: NSDictionary);
begin
Gstr := inttostr( round( param1.length / 1024 ) ) + ' KB ';
end;

end.

Edited by: Mustafa Ersagun Korkmaz on Sep 21, 2017 4:36 AM

Dave Nottage

Posts: 1,850
Registered: 1/7/00
Re: get file names with file sizes from Photos directory [Edit] [Edit]
Click to report abuse...   Click to reply to this thread Reply
  Posted: Sep 21, 2017 10:16 AM   in response to: Mustafa Ersagun... in response to: Mustafa Ersagun...
Mustafa Ersagun Korkmaz wrote:

Thank you Dave and Allen Drennan.

All done: file size, file name, file date

I guess I missed the originalFilename property.. thanks! (and thanks Allen)

--
Dave Nottage [MVP, TeamB]
Find hints, tips and tricks at Delphi Worlds blog: http://www.delphiworlds.com
Legend
Helpful Answer (5 pts)
Correct Answer (10 pts)

Server Response from: ETNAJIVE02