Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Desarrollo en Delphi para Android (https://www.clubdelphi.com/foros/forumdisplay.php?f=57)
-   -   obtener ruta de archivo del explorador android (https://www.clubdelphi.com/foros/showthread.php?t=96622)

imaginate301 29-02-2024 19:22:51

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??

jhonalone 13-03-2024 19:04:38

Hola.
A lo mejor digo una tontería, pero ¿has cambiado en el Manifiesto...?

Código:

<uses-sdk android:minSdkVersion="%minSdkVersion%" android:targetSdkVersion="34" />
y también

Código:

<activity
            android:name="com.embarcadero.firemonkey.FMXNativeActivity"
            android:label="%activityLabel%"
            android:configChanges="orientation|keyboard|keyboardHidden|screenSize"
            android:launchMode="singleTask"
            android:exported="true">

Sé que es una oviedad, pero...

Un saludo.


La franja horaria es GMT +2. Ahora son las 12:00:47.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi