Jump to content
grantful

Kastri File Selector Help with Android

Recommended Posts

I am trying to to take a backup sqlite database and move it from the default Downloads folder to the default documets folder.

procedure CopyFileFromUri(const AUri: string; const ADestPath: string);
var
  LInputStream: JInputStream;
  LOutputStream: JFileOutputStream;
  LBuffer: TJavaArray<Byte>;
  LBytesRead: Integer;
begin
  LInputStream := TAndroidHelper.Context.getContentResolver.openInputStream(StrToJURI(AUri));
  try
    LOutputStream := TJFileOutputStream.JavaClass.init(StringToJString(ADestPath));
    try
      LBuffer := TJavaArray<Byte>.Create(1024);
      try
        repeat
          LBytesRead := LInputStream.read(LBuffer);
          if LBytesRead > 0 then
            LOutputStream.write(LBuffer, 0, LBytesRead);
        until LBytesRead = -1;
      finally
        LBuffer.Free;
      end;
    finally
      LOutputStream.close;
    end;
  finally
    LInputStream.close;
  end;
end;
 procedure TfrmVehicle.ReplaceDBFile(const SourcePath: string);
var
  DestPath: string;
begin
  // Assuming the destination database is in the documents folder
  DestPath := TPath.Combine(TPath.GetDocumentsPath, 'AutoRepair.db');

  try
    // If a database file already exists at the destination, delete it first
    if TFile.Exists(DestPath) then
      TFile.Delete(DestPath);

    // Copy the selected database file to the destination
    TFile.Copy(SourcePath, DestPath);
    //CopyFileFromUri(SourcePath, DestPath);
    ShowMessage('Database replaced successfully.');

    // Refresh the FireDAC connection and datasets
    //RefreshFireDACConnection;

  except
    on E: Exception do
    begin
      ShowMessage('Error replacing database: ' + E.Message);
      // Handle the exception as needed, e.g., log it or take appropriate action.
    end;
  end;
end;

function GetFilePathFromUri(const AUri: string): string;
var
  LUri: Jnet_Uri;
  LCursor: JCursor;
  LColumnIndex: Integer;
begin
  Result := '';
  LUri := StrToJURI(AUri); // Convert the URI string to a Jnet_Uri object
  if LUri.getScheme.equals(StringToJString('content')) then
  begin
    // Query the content resolver to get the file path
    LCursor := TAndroidHelper.Context.getContentResolver.query(LUri, nil, nil, nil, nil);
    if LCursor <> nil then
    begin
      try
        if LCursor.moveToFirst then
        begin
          LColumnIndex := LCursor.getColumnIndex(StringToJString('_data')); // Column for file path
          if LColumnIndex <> -1 then
            Result := JStringToString(LCursor.getString(LColumnIndex));
        end;
      finally
        LCursor.close;
      end;
    end;
  end
  else if LUri.getScheme.equals(StringToJString('file')) then
  begin
    // If it's a file URI, just return the path
    Result := JStringToString(LUri.getPath);
  end;
end;

procedure TfrmVehicle.FileSelectorComplete(Sender: TObject; const AOK: Boolean);
var
  I: Integer;
  FileName, FilePath, RawUri: string;
begin
  try
    if AOK then
    begin
      for I := Low(FFilesSelector.SelectedFiles) to High(FFilesSelector.SelectedFiles) do
      begin
        try
          // Get the raw URI and log it
          RawUri := FFilesSelector.SelectedFiles[I].RawPath;
          ShowMessage('Raw URI: ' + RawUri); // Log the raw URI

          // Get the decoded file path and log it
          FilePath := GetFilePathFromUri(RawUri);
          ShowMessage('Decoded File Path: ' + FilePath); // Log the decoded file path

          // Extract the file name from the decoded path
          FileName := ExtractFileName(FilePath);
          ShowMessage('Selected File Name: ' + FileName); // Log the selected file name

          if SameText(FileName, 'AutoRepair.db') then
          begin
            ReplaceDBFile(FilePath);
            Exit;
          end;
        except
          on E: Exception do
          begin
            ShowMessage('An error occurred while processing file: ' + E.Message);
            // Additional exception handling code here, if needed
          end;
        end;
      end;
      ShowMessage('No file named AutoRepair.db was selected.');
    end
    else
    begin
      ShowMessage('No files were selected.');
    end;
  except
    on E: Exception do
    begin
      ShowMessage('An error occurred: ' + E.Message);
      // Additional exception handling code here, if needed
    end;
  end;
end;

It compiles but

I am having a problem implementing this into my app. On android i get Selected filename : =  nothing. ANy help is much appreciated. 

Share this post


Link to post
5 hours ago, grantful said:

i get Selected filename : =  nothing

What does GetFilePathFromUri return? I expect it's nothing as well. 

Check the Android section of the readme for information on how to access the file

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

×