Jump to content
Martin Ortmayr

How to read file on plugged in Android device

Recommended Posts

I'm having trouble figuring out how to read / copy a file that is on the filesystem of an Android device plugged into a Win10 PC via USB.

I can see the file using Windows Explorer as shown in the attached image.

Usually I would just use a drive letter to open the file but in this case it has no drive letter.

I've tried something like this but it gives an error:

 

    assignfile(tf,'"\\This PC\TC20\Internal shared storage\WXLog.log"');
    reset(tf);   //Exception EInOutError, I/O error 123 occurs here

What's the proper way to do this?

Thanks

 

 

explorer.png

Share this post


Link to post

The device is not accessible via the host machine's file system. There is no local drive letter or UNC share assigned to the device, so you can't use filesystem-based functions to access the device's files.

 

So, you will have to use alternative means of accessing the device's files, such as:

 

 

Edited by Remy Lebeau
  • Like 2

Share this post


Link to post

The components from Vcl.Shell.ShellCtrls.pas and this code snippet will do the job:

 

procedure TMainForm.bbCopyClick(Sender: TObject);
var
  i,n,j : integer;
  fileOp : IFileOperation;
  siSrcList : IShellItemArray;
  idList : array of PItemIDList;
  siSrcFile,siDestFolder : IShellItem;
begin
  with ShellListView do if assigned(Selected) then begin
    n:=Selected.Index;
    if SelCount=1 then with SelectedFolder do begin
      OleCheck(SHCreateItemFromIDList(AbsoluteID,IShellItem,siSrcFile));
      OleCheck(SHCreateItemFromParsingName(PChar(edDestDir.Text),nil,IShellItem,siDestFolder));
      OleCheck(fileOp.CopyItem(siSrcFile,siDestFolder,pchar(DisplayName),nil));
      OleCheck(fileOp.PerformOperations);
      end
    else begin
      SetLength(idList,SelCount); j:=0;
      OleCheck(CoCreateInstance(CLSID_FileOperation,nil,CLSCTX_ALL,IFileOperation,fileOp));
      OleCheck(fileOp.SetOperationFlags(FOF_FILESONLY+FOF_NOCONFIRMMKDIR+FOF_NO_CONNECTED_ELEMENTS));
      for i:=n to Items.Count-1 do if Items[i].Selected then begin
        idList[j]:=Folders[i].AbsoluteID; inc(j);
        end;
      OleCheck(SHCreateShellItemArrayFromIDLists(SelCount,@idList[0],siSrcList));
      OleCheck(SHCreateItemFromParsingName(PChar(edDestDir.Text),nil,IShellItem,siDestFolder));
      OleCheck(fileOp.CopyItems(siSrcList,siDestFolder));
      OleCheck(fileOp.PerformOperations);
      idList:=nil;
      end;
    end;
  end;

 

Share this post


Link to post

Update:

procedure TMainForm.bbCopyClick(Sender: TObject);
var
  i,n,j : integer;
  fileOp : IFileOperation;
  siSrcList : IShellItemArray;
  idList : array of PItemIDList;
  siSrcFile,siDestFolder : IShellItem;
begin
  with ShellListView do if assigned(Selected) then begin
    n:=Selected.Index;
    OleCheck(CoCreateInstance(CLSID_FileOperation,nil,CLSCTX_ALL,IFileOperation,fileOp));
    if SelCount=1 then with SelectedFolder do begin
      OleCheck(SHCreateItemFromIDList(AbsoluteID,IShellItem,siSrcFile));
      OleCheck(SHCreateItemFromParsingName(PChar(edDestDir.Text),nil,IShellItem,siDestFolder));
      OleCheck(fileOp.CopyItem(siSrcFile,siDestFolder,pchar(DisplayName),nil));
      OleCheck(fileOp.PerformOperations);
      end
    else begin
      SetLength(idList,SelCount); j:=0;
      OleCheck(fileOp.SetOperationFlags(FOF_FILESONLY+FOF_NOCONFIRMMKDIR+FOF_NO_CONNECTED_ELEMENTS));
      for i:=n to Items.Count-1 do if Items[i].Selected then begin
        idList[j]:=Folders[i].AbsoluteID; inc(j);
        end;
      OleCheck(SHCreateShellItemArrayFromIDLists(SelCount,@idList[0],siSrcList));
      OleCheck(SHCreateItemFromParsingName(PChar(edDestDir.Text),nil,IShellItem,siDestFolder));
      OleCheck(fileOp.CopyItems(siSrcList,siDestFolder));
      OleCheck(fileOp.PerformOperations);
      idList:=nil;
      end;
    end;
  end;

 

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×