Android Question SOLVED - Phone Library / Content Chooser / Multiple MIMEs

Robert Valentino

Well-Known Member
Licensed User
Longtime User
From what I have read: Multiple Mimes are allowed so I wanted to add the following routine to Phone Library / Content Chooser

Code: Copy
        public void ShowMultipleMIME(final BA ba, String[] MimeTypes, String Title) {
            if (eventName == null)
                throw new RuntimeException("ContentChooser not initialized.");
            Intent in = new Intent(Intent.ACTION_GET_CONTENT);
            in.setType("*/*");
            in.putExtra(Intent.EXTRA_MIME_TYPES, MimeTypes);
            in.addCategory(Intent.CATEGORY_OPENABLE);
            in = Intent.createChooser(in, Title);
            ion = new IOnActivityResult() {
                @Override
                public void ResultArrived(int resultCode, Intent intent) {
                    String Dir = null, File = null;
                    if (resultCode == Activity.RESULT_OK && intent != null && intent.getData() != null) {
                        try {
                            Uri uri = intent.getData();
                            String scheme = uri.getScheme();
                            if (ContentResolver.SCHEME_FILE.equals(scheme)) {
                                Dir = "";
                                File = uri.getPath();
                            }
                            else if (ContentResolver.SCHEME_CONTENT.equals(scheme)) {
                                Dir = anywheresoftware.b4a.objects.streams.File.ContentDir;
                                File = uri.toString();
                            }

                        } catch (Exception e) {
                            throw new RuntimeException(e);
                        }
                    }
                    ion = null;
                    if (Dir != null && File != null) {
                        ba.raiseEvent(ContentChooser.this, eventName + "_result", true, Dir, File);
                    }
                    else {
                        ba.raiseEvent(ContentChooser.this, eventName + "_result", false, "", "");
                    }
                }
            };
            ba.startActivityForResult(ion, in);
        }


I just do not know how to go about compiling the Phone Library.

Added this routine into the Content Chooser section and then tried to compile with simple library compiler but got a ton of errors so I'm thinking that I'm doing something wrong??

1746983296719.png
 

Attachments

Last edited:
Update:

Just to verify that multiple mime types works I did this little example
Code: Copy
            Dim ViewIntent         As Intent
                   
               ViewIntent.Initialize("android.intent.action.GET_CONTENT" , "downloads")  
            ViewIntent.Flags     = 1
            ViewIntent.setType("*/*")
            ViewIntent.putExtra("android.intent.extra.MIME_TYPES", Array As String("application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"))
            ViewIntent.addCategory("android.intent.category.DEFAULT")

            StartActivity(ViewIntent)

The above code did just what I thought it would showed me the xls and xlsx files only

So, it seems we could add the routine to Content Chooser
 
Upvote 0
Got the Phone library to compile and my ShowMultipleMIME works just fine.

Example Code:
B4X: Copy
Private  mPickFile        As ContentChooser

mPickFile.Initialize("PickFile")
mPickFile.ShowMultipleMIME(Array As String("application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), "Pick a Excel (xls or xlsx) file to import")               
                
Wait For PickFile_Result(Success As Boolean, Dir As String, FileName As String)
                
Log($"Success:${Success} Dir:${Dir}  FileName:${FileName}"$)
                            
If  Success Then
End If
 

Attachments

Upvote 0
I tried the previously posted routine using Array As String("image/jpg", "image/jpeg", "image/png", "image/gif", "image/tiff")

As my MIME array string just to verify that I could see all these different types and works perfectly.
 
Upvote 0
Got the Phone library to compile and my ShowMultipleMIME works just fine.

Example Code:
B4X: Copy
Private  mPickFile        As ContentChooser

mPickFile.Initialize("PickFile")
mPickFile.ShowMultipleMIME(Array As String("application/vnd.ms-excel", "application/vnd.openxmlformats-officedocument.spreadsheetml.sheet"), "Pick a Excel (xls or xlsx) file to import")              
               
Wait For PickFile_Result(Success As Boolean, Dir As String, FileName As String)
               
Log($"Success:${Success} Dir:${Dir}  FileName:${FileName}"$)
                           
If  Success Then
End If
Sorry, I know this is an old thread. So far this lib is the latest that has this functionality?

Thanks in advance.
 
Upvote 0
Back
Top