Jump to content
Sign in to follow this  
Renate Schaaf

Browsing for certain files on Android

Recommended Posts

All I want is a substitute for an OpenFileDialog on Android, I mean, that can't be so hard, I thought.

In particular I want the user to pick a video file and read its file name. I found this

https://delphi-developers-archive.blogspot.com/2017/05/delphi-android-get-file-path-of-image.html

and modified it a bit, so it either returns a valid file name or else fails:

 

procedure TForm1.BrowseForVideoFile;
var
  Data: Jnet_Uri;
  Intent: JIntent;
begin
  // fMessageSubscriptionID: integer is a field of TForm1
  FMessageSubscriptionID := TMessageManager.DefaultManager.SubscribeToMessage
    (TMessageResultNotification, HandleActivityMessage);
  Intent := TJIntent.Create;
  Intent.setType(StringToJString('video/*'));
  Intent.setAction(TJIntent.JavaClass.ACTION_GET_CONTENT);
  MainActivity.startActivityForResult(Intent, 0);
end;

// when message is received
procedure TForm1.HandleActivityMessage(const Sender: TObject;
  const M: TMessage);
begin
  if M is TMessageResultNotification then
    if HandleIntentAction(TMessageReceivedNotification(M).Value) then
    begin
      // A valid filename has been returned
      // Do The Videofile processing
    end;
    Edit2.text := fVideoFilename;
end;

// This retrieves the file name the user picked, but reliably only if done per
// opening MyDocuments
function TForm1.HandleIntentAction(const Data: JIntent): Boolean;
var
  P: TJavaObjectArray<Jstring>;
  // in case you want only specific fields... not used here. I have passed nil to get all the columns.
  C: JCursor;
  I: integer;
begin
  P := nil;
  // The following makes no difference
  // P:=TJavaObjectArray<Jstring>.create(1);
  // P.Items[0]:=StringToJstring('_data');

  // this is suppose to give the information back to C ( : JCursor)
  C := MainActivity.getContentResolver.query(Data.getData, nil,
    // when projection is nil... it returns all columns. Ideally, you should only ask for the columns you need
    StringToJString(''),
    // java accepts nil... but you cannot give nil here, you need to give an empty JString
    nil, StringToJString(''));
  // java accepts nil... but you cannot give nil here, you need to give an empty JString

  C.moveToFirst;
  Result := false;
  for I := 0 to C.getColumnCount - 1 do
  begin
    if JStringToString(C.getColumnName(I)) = '_data' then
    // '_data' column contains the path... you can use this to create the filestream to upload or do whatever....
    begin
      // fVideoFilename is a field of the form
      fVideoFilename := JStringToString(C.getString(I));
      Result := true;
      Break;
    end;
  end;
  if not Result then
    fVideoFilename := '';
  // P.Free;
end;

My problem is, that this offers way too many browsers for the user to choose from, when all that really works is browsing MyDocuments.

What is the right approach to limit the browsers to MyDocuments?

Or is there an easier way to begin with?

 

Thanks for reading,

Renate

Share this post


Link to post
1 hour ago, Renate Schaaf said:

My problem is, that this offers way too many browsers for the user to choose from, when all that really works is browsing MyDocuments.

By "browsers" I assume you mean "folders"? What does "MyDocuments" actually represent? Does it mean the files contained in the folder returned by TPath.GetDocumentsPath? If so, is there some reason why you cannot iterate the files in that folder, and just present them in a list box?

Share this post


Link to post

Please excuse my lack of knowledge here, I'm just starting to work with this Android stuff.

Quote

Does it mean the files contained in the folder returned by TPath.GetDocumentsPath

I guess so.

Quote

If so, is there some reason why you cannot iterate the files in that folder, and just present them in a list box?

Well, I think I know how to do this, but that wouldn't help the user to pick to right video file, I need the thumbnails.

By browers I mean that every app that processes the file type gets displayed at the top of the window that opens. For example Google fotos, Samsung gallery, ...

I just want to open the documents folder ("Eigene Dateien" in my German tablet).

Share this post


Link to post

I'm not sure whether you can access this TPath.GetDocumentsPath  folder in Android 10/11 anymore without issues.
I tried to access TPath.GetSharedDownloadsPath which had worked up to now but throws an permission error of some sort on Android 10
even after requesting READ_EXTERNAL_STORAGE permission from user...

Share this post


Link to post

Yet another hoop to jump through.. Why are they making it so hard? Thanks for the link, I'll check it out.

 

Renate

Share this post


Link to post

I have a hard time with this as well, as my app up to now just lists all files with my specific suffix stored in downloads folder in a list view and the user can pick one.

In addition if he selects one he gets 3 buttons on that entry shown. One for actually opening the file, one for displaying file meta data in a popup and one for sharing
it. I'm not sure yet how I can replace all this in an Android 10/11 compatible way.

 

For Android 10 I could use a compatibility setting in the manifest, but for Android 11 this is no longer allowed/doesn't do anything anymore and Android 11
compatibility support is as far as I know required some time in 2021 for Google Play distribution...

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
Sign in to follow this  

×