How to tell Android to use an application to open a mime type?

nachoap10

Member
Licensed User
Longtime User
Hi again:

My new (but, I'm sure, not last) problem with my rooted T1 android ebook is: How could I associate a mime type to an existing android application?

If I'm right, in android it's not possible to call an application directly by code. Instead, we have to do an "intent" with a mime type.

Well, it usually works, but in my rooted T1 when I make this:

B4X:
Try
    inAct.Initialize(inAct.ACTION_VIEW, sOrg)
    inAct.SetType("text/*")
    StartActivity(inAct)
Catch
    ToastMessageShow("I CAN'T OPEN THE BOOK: " & sOrg, True)
End Try

I don't get CoolReader (the book viewer for the sOrg fb2 file) in the app list ready to open "text/*" files. I've tried to change mime type to "*/*", and I get a lot of apps ready to open my book (none of them can do it, of course), but CoolReader isn't in that list.

So, I've read that the app (CoolReader) has to write his own manifest file, in order to be called by android, but CoolReader does it. If I try to execute a fb2 file with File Expert, installed in my T1, CoolReader is in the list of apps ready to open the book, but if I try to open with Root Explorer or with my B4A app, it is not. sOrg is right (for example, Jota Text Editor is in the list, and it opens the fb2 file).

I suposse it's a problem with permissions (I know, it's a rooted device, and may be some things doesn't work as we expect). :sign0148:

:sign0163: Do you know any other way in order to execute an installed App by code from B4A, or any way to make android executes just the app I want? Is it possible to create the "intent" as root? How? :BangHead:

I'm going to try to install Market and uninstall CoolReader and install it again from Market. I've installed from other way, and maybe it doesn't work well if it's not installed from official Market.

Thanks in advance. Any ideas will be welcomed!!!
 

nachoap10

Member
Licensed User
Longtime User
You can start any application that you want from your code by adding:
B4X:
inAct.SetComponent("package/.activity")
'for example:
inAct.SetComponent("com.android.browser/.BrowserActivity")

You can use PackageManager from the phone library to find the "main" intent that starts a specific app.

Thank you very much, Erel. I'll try it this afternoon :icon_clap:

Edit: Waaawww! It seems to be an excellent way to get apps installed. With a little of chance it'll work in the T1.
 
Last edited:
Upvote 0

nachoap10

Member
Licensed User
Longtime User
:sign0098::sign0098::sign0098::sign0098::sign0098::sign0098::sign0098:

IT WORKS!

:sign0098::sign0098::sign0098::sign0098::sign0098::sign0098::sign0098:


The final code is:

B4X:
Try
   inAct.Initialize(inAct.ACTION_VIEW, sOrg)
   inAct.SetType("text/*")
   inAct.SetComponent("org.coolreader/.CoolReader")
   StartActivity(inAct)
Catch
   ToastMessageShow("No puedo abrir el libro: " & sOrg, True)
End Try

and it opens CoolReader directly, without app list.

In order to get the activity, I've add these lines in the first execution:

First:

B4X:
   packages = pm.GetInstalledPackages
   For i = 0 To packages.Size - 1
      sPaquete = packages.Get(i)
               Log(packages.Get(i))
   Next

Here I've placed a breakpoint over sPaquete = packages.Get(i) and I've been reading package names in the debugger. Coolreader was the first package: "org.coolreader"

Second

I've commented previous lines and I've written this:

B4X:
in = pm.GetApplicationIntent("org.coolreader")

with a breakpoint over it. In the list of variables I saw the activity name: "Coolreader".

Finally, I've commented all these lines and I've written:

inAct.SetComponent("org.coolreader/.CoolReader")

Man, you're great! Thenk you very much.

Final code is:

B4X:
'Activity module
Sub Process_Globals
   'These global variables will be declared once when the application starts.
   'These variables can be accessed from all modules.

End Sub

Sub Globals
   'These global variables will be redeclared each time the activity is created.
   'These variables can only be accessed from this module.

   Dim btOpen As Button
   Dim lvLista As ListView
End Sub

Sub Activity_Create(FirstTime As Boolean)
   Activity.LoadLayout("Entrada")
End Sub

Sub Activity_Resume

End Sub

Sub Activity_Pause (UserClosed As Boolean)

End Sub

Sub PonerURL(sCadena As String) As String
Dim i As Long
Dim sAux As String
Dim StrOrg() As String
Dim StrDst() As String
      
   StrOrg = Array As String("%", "!", "#", "$", "&", "'", "(", ")", "*", "+", ",", ":", ";", "=", "?", "@", "[", "]", " ", "<", ">", "\", "^", "`", "{", "|", "}" )
   StrDst = Array As String("%25", "%21", "%23", "%24", "%26", "%27", "%28", "%29", "%2A", "%2B", "%2C", "%3A", "%3B", "%3D", "%3F", "%40", "%5B", "%5D", "%20", "%3C", "%3E", "/", "%5E", "%60", "%7B", "%7C", "%7D")
   
'   StrOrg = Array As String("%", "!", "#", "$", "&", "'", "(", ")", "*", "+", ",", ":", ";", "=", "?", "@", "[", "]", " ", "<", ">", "\", "^", "`", "{", "|", "}", "€", "‚", "ƒ", "„", "…", "†", "‡", "ˆ", "‰", "Š", "‹", "Œ", "Ž", "‘", "’", "“", "”", "•", "–", "—", "˜ ", "™", "š", "›", "œ", "ž", "Ÿ", "¡", "¢", "£", "¥", "|", "§", "¨", "©", "ª", "«", "¬", "¯", "®", "¯", "°", "±", "²", "³", "´", "µ", "¶", "·", "¸", "¹", "º", "»", "¼", "½", "¾", "¿", "À", "Á", "Â", "Ã", "Ä", "Å", "Æ", "Ç", "È", "É", "Ê", "Ë", "Ì", "Í", "Î", "Ï", "Ð", "Ñ", "Ò", "Ó", "Ô", "Õ", "Ö", "Ø", "Ù", "Ú", "Û", "Ü", "Ý", "Þ", "ß", "à", "á", "â", "ã", "ä", "å", "æ", "ç", "è", "é", "ê", "ë", "ì", "í", "î", "ï", "ð", "ñ", "ò", "ó", "ô", "õ", "ö", "÷", "ø", "ù", "ú", "û", "ü", "ý", "þ", "ÿ")
'   StrDst = Array As String("%25", "%21", "%23", "%24", "%26", "%27", "%28", "%29", "%2A", "%2B", "%2C", "%3A", "%3B", "%3D", "%3F", "%40", "%5B", "%5D", "%20", "%3C", "%3E", "/", "%5E", "%60", "%7B", "%7C", "%7D", "%80", "%82", "%83", "%84", "%85", "%86", "%87", "%88", "%89", "%8A", "%8B", "%8C", "%8E", "%91", "%92", "%93", "%94", "%95", "%96", "%97", "%98", "%99", "%9A", "%9B", "%9C", "%9E", "%9F", "%A1", "%A2", "%A3", "%A5", "%A6", "%A7", "%A8", "%A9", "%AA", "%AB", "%AC", "%AD", "%AE", "%AF", "%B0", "%B1", "%B2", "%B3", "%B4", "%B5", "%B6", "%B7", "%B8", "%B9", "%BA", "%BB", "%BC", "%BD", "%BE", "%BF", "%C0", "%C1", "%C2", "%C3", "%C4", "%C5", "%C6", "%C7", "%C8", "%C9", "%CA", "%CB", "%CC", "%CD", "%CE", "%CF", "%D0", "%D1", "%D2", "%D3", "%D4", "%D5", "%D6", "%D8", "%D9", "%DA", "%DB", "%DC", "%DD", "%DE", "%DF", "%E0", "%E1", "%E2", "%E3", "%E4", "%E5", "%E6", "%E7", "%E8", "%E9", "%EA", "%EB", "%EC", "%ED", "%EE", "%EF", "%F0", "%F1", "%F2", "%F3", "%F4", "%F5", "%F6", "%F7", "%F8", "%F9", "%FA", "%FB", "%FC", "%FD", "%FE", "%FF")
   
   
   sAux = sCadena
   For i = 0 To StrOrg.Length-1
      sAux = sAux.Replace(StrOrg(i), StrDst(i))
   Next
   
   Return sAux
End Sub

Sub EjecutarRoot (comando As String)
Dim Command, Runner As String
Dim StdOut, StdErr As StringBuilder
Dim Result As Int
Dim Ph As Phone
   

   Runner = File.Combine(File.DirInternalCache, "runner")
   Command = File.Combine(File.DirInternalCache, "command")
   File.WriteString(File.DirInternalCache, "runner", "su < " & Command)
   File.WriteString(File.DirInternalCache, "command", comando & CRLF & "exit") 'Any commands via crlf, and exit at end 
   StdOut.Initialize
   StdErr.Initialize
   Result = Ph.Shell("sh", Array As String(Runner), StdOut, StdErr)
   Msgbox(StdOut.tostring, "")
End Sub

Sub AbrirLibro (RutaLibro As String)
Dim inAct As Intent
Dim RutaL, RutaE, Prefijo, Origen, Destino, sFichero, sOrg, sOrg2, sFicheroDst As String
Dim NumLetras, i, iPos As Long
Dim TR As TextReader
      ' Prefijo
      Prefijo = "file://" & "mnt/extsd/MISLIBROS/"
      Origen  = "/mnt/extsd/.LIBROS/"
      Destino = "/mnt/extsd/MISLIBROS/"
      
      ' Número de letras a descartar (prefijo de lipapa)
      NumLetras = 10
      
      If NumLetras < RutaLibro.Length Then
      
         ' Descartamos las primeras
         sFichero = RutaLibro.SubString(NumLetras)
         iPos = RutaLibro.LastIndexOf("\")
         sFicheroDst = RutaLibro.SubString2(iPos+1, RutaLibro.Length)
      
         sOrg = Prefijo & PonerURL(sFicheroDst)
         sOrg2 = sFichero.Replace("\", "/")
         
         'EjecutarRoot("mkdir /mnt/extsd/MISLIBROS")
         If Not(File.Exists(Destino, sFicheroDst)) Then
            EjecutarRoot("cat '" & Origen & sOrg2 & "'>'" & Destino & sFicheroDst & "'")
         End If
         
         Try
            inAct.Initialize(inAct.ACTION_VIEW, sOrg)
            inAct.SetType("text/*")
            inAct.SetComponent("org.coolreader/.CoolReader")
            StartActivity(inAct)
         Catch
            ToastMessageShow("No puedo abrir el libro: " & sOrg, True)
         End Try
      Else
         ToastMessageShow("El libro seleccionado no tiene la ruta asignada. Haz Copiar en ebook desde Lipapa para PC", True)
      End If
End Sub


Sub btOpen_Click
Dim pm As PackageManager
Dim packages As List
Dim sPaquete As String
Dim in As Intent

   'Ver paquetes instalados
   'in = pm.GetApplicationIntent("org.coolreader")
   'packages = pm.GetInstalledPackages
   'For i = 0 To packages.Size - 1
   '   sPaquete = packages.Get(i)
        '   Log(packages.Get(i))
   'Next
   
   'El tipo de rutas que se encontrará en RUTAPAPYRE, en la bd de lipapa
   AbrirLibro("C:\LIBROS\A\AS\ASI\Asimov, Isaac & Janet\Fronteras II [10349].FB2")
End Sub
 
Upvote 0

nachoap10

Member
Licensed User
Longtime User
Oh my God! It's still better. This way of working allows to put books zipped, and CoolReader opens them :sign0060::sign0060::sign0060:

So, I can zip all my books and they occupy only about 1/3 of their original size

:wav::wav::wav:​
 
Upvote 0
Top