Ver Mensaje Individual
  #1  
Antiguo 29-02-2024
imaginate301 imaginate301 is offline
Registrado
 
Registrado: dic 2023
Posts: 1
Reputación: 0
imaginate301 Va por buen camino
obtener ruta de archivo del explorador android

Buenas tardes, estoy intentando obtener la ruta de un archivo seleccionado desde el explorador de archivos por defecto de android.

Este procedimiento para abrirlo

Código Delphi [-]
procedure TForm15.OpenFileSelector;
var
  Intent: JIntent;
begin
  Intent := TJIntent.Create;
  Intent.setAction(TJIntent.JavaClass.ACTION_OPEN_DOCUMENT);
  Intent.setType(StringToJString('*/*')); // Selecciona todos los tipos de archivos
  Intent.addCategory(TJIntent.JavaClass.CATEGORY_OPENABLE);
  SharedActivity.startActivityForResult(Intent, 0);

end;

Esta funcion para obtener la ruta

Código Delphi [-]
function TForm15.GetSelectedFilePath(const Intent: JIntent): string;
var
  Uri: Jnet_Uri;
  Cursor: JCursor;
  ColumnIndex: Integer;
  FilePath: string;
begin
  Result := '';
  Uri := Intent.getData;
  if Assigned(Uri) then
  begin
    Cursor := SharedActivity.getContentResolver.query(Uri, nil, nil, nil, nil);
    if Assigned(Cursor) then
    begin
      ColumnIndex := Cursor.getColumnIndexOrThrow(TJMediaStore_MediaColumns.JavaClass.DATA);
      Cursor.moveToFirst;
      FilePath := JStringToString(Cursor.getString(ColumnIndex));
      Cursor.close;
      Result := FilePath;
    end;
  end;
end;

y este para recibir la notificacion :

Código Delphi [-]
procedure TForm15.HandleActivityMessage(const Sender: TObject; const M: TMessage);
var
  ResultNotification: TMessageResultNotification;
  SelectedFilePath: string;
begin
  if M is TMessageResultNotification then
  begin
    ResultNotification := TMessageResultNotification(M);
    if ResultNotification.RequestCode = 0 then
    begin
      if ResultNotification.ResultCode = TJActivity.JavaClass.RESULT_OK then
      begin
        SelectedFilePath := GetSelectedFilePath(ResultNotification.Value);
        ShowMessage('Ruta del archivo seleccionado: ' + SelectedFilePath);
        rutapdf.Text:= SelectedFilePath;
      end;
    end;
  end;
end;

Utilizo el servicio de mensajeria de delphi

Código Delphi [-]
TMessageManager.DefaultManager.SubscribeToMessage(TMessageResultNotification, HandleActivityMessage);

En teoria lo guarda en un edit llamado rutapdf

En versiones anteriores a android 14 si me funciona, pero lo instalo en otro movil con android 14 y se bloquea al llamar al servicio de mensajeria.

Alguien me puede ayudar porfavor??

Última edición por Casimiro Notevi fecha: 29-02-2024 a las 20:41:38. Razón: Poner etiquetas [delphi] [/delphi] al código.
Responder Con Cita