How can I open a file with his application?

nachoap10

Member
Licensed User
Longtime User
Hi:

I need to call some files in order they are opened with his applications. For example, I need to call some fb2 files (an ebook format) in order they are opened with CoolReader or FBReader, or in order Android tells user what application he wants to use, and show a list of avalaible applications, or something similar.

What I want to have is similar to what you have when you are working with File Expert and press over a fb2 file, a pdf file, a jpg file... They are opened or File Expert ask you what application do you want to work with.

Maybe a Shell routine would work, but it doesn't exists in B4A. I've tried to work with Phone.Shell, but it didn't work:

Dim pi As Phone
pi.Shell("\sdcard\Fronteras [10348].FB2", Null, Null, Null)

where "Fronteras [10348].FB2" is the book I want to open, and it's placed in /sdcard/. I've tried to change "\" for "/", but it didn't work...

Is it possible to do this?

Thank you in advance an best regards.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
Android applications cannot be started with Shell.
You should use an Intent for this.
For example:
B4X:
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "file///sdcard/Fronteras.FB2")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.")
    End Try

Start with a file without spaces which can cause some problems.
 
Upvote 0

nachoap10

Member
Licensed User
Longtime User
Android applications cannot be started with Shell.
You should use an Intent for this.
For example:
B4X:
    Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "file///sdcard/Fronteras.FB2")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.")
    End Try

Start with a file without spaces which can cause some problems.

Thank you very much. I'm going to try your code :sign0098:
 
Upvote 0

nachoap10

Member
Licensed User
Longtime User
Well, I've tried your code, and a lot of variations of it, and it can't execute fb2 nor png.

I've renamed my fb2 as Fronteras.FB2 (without spaces nor brackets nor numbers), and I've tried this:

B4X:
Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "file///sdcard/Fronteras.FB2")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.", True)
    End Try

and this:

B4X:
Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "file:///sdcard/Fronteras.FB2")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.", True)
    End Try

and this:

B4X:
Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "file://sdcard/Fronteras.FB2")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.", True)
    End Try

and this:

B4X:
Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "file:/sdcard/Fronteras.FB2")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.", True)
    End Try

and this:

B4X:
Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "file:////sdcard//Fronteras.FB2")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.", True)
    End Try

and this:

B4X:
Dim i As Intent
    i.Initialize(i.ACTION_MAIN, "///sdcard/Fronteras.FB2")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.", True)
    End Try

and a lot of combinations with '/' and without it, with 'file:', with 'file', without them... All of them have failed.

Afterwards, I've tried with a png file, named demo.png, with all previous combinations, but it has failed too.

What is wrong with this code? I'm desesperated :SHOCKED::SHOCKED::SHOCKED: because I'm trying to open a fb2 file in android for a lot. I've tried creating reports in pdf, but standard pdf files cannot open external documents (only html links). I've tried to create fb2 files whitch call other fb2 files (it works in my PC), but CoolReader for Android can't still open external fb2 links. I've tried to create html reports, but html can't open external fb2 files... In the few platforms it worked (opening html external files), it opens files as xml code, because there isn't plugins for browsers in order to open fb2 files (as pdf does).

If someone can help me I will be really happy. Thanks in advance and best regards.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
You are correct. You should also set the type. I'm not familiar with fb2 files so I don't know if there are third party application that know how to open them by an Intent and what they expect.
This code works for images:
B4X:
Dim i As Intent
i.Initialize(i.ACTION_VIEW, "file:///sdcard/1.jpg")
i.SetType("image/*")
StartActivity(i)
 
Upvote 0

nachoap10

Member
Licensed User
Longtime User
You are correct. You should also set the type. I'm not familiar with fb2 files so I don't know if there are third party application that know how to open them by an Intent and what they expect.
This code works for images:
B4X:
Dim i As Intent
i.Initialize(i.ACTION_VIEW, "file:///sdcard/1.jpg")
i.SetType("image/*")
StartActivity(i)

Thank you again. Now it works! :sign0060:

MIME type for fb2 is text/xml. It doesn't open CoolReader, but now it shows a list with some programs, and in this list is Moon+ Reader, a fb2/epub reader :D

I'm going to do more changes to my code, trying to get CoolReader to my list and trying to open spaced file names, but the most difficult question has been solved with your help.

Thank you very very much. :sign0098::sign0098::sign0098:

My final code is:

B4X:
Dim i As Intent
   i.Initialize(i.ACTION_VIEW, "file:///sdcard/Fronteras2.fb2")
   i.SetType("text/xml")
    Try
        StartActivity(i)
    Catch
        ToastMessageShow("Cannot start file.", True)
    End Try
 
Upvote 0

nachoap10

Member
Licensed User
Longtime User
:wav: Done! :wav:

I have only changed the MIME type for "text/*" and now I have all installed applications in the program list of installed tools to work with fb2, including CoolReader

:sign0060::sign0060::sign0060::sign0060::sign0060::sign0060::sign0060::sign0060:

:sign0008:​
 
Upvote 0

nachoap10

Member
Licensed User
Longtime User
What are these files?

These are Fiction Book files. Fiction Book (fb2) are a special xml files for ebooks. I have been working in a free application (LIPAPA) for catalog and work with these files for two years, but now my users (and myself :D) want an Android read-only version, in order to consult their database books in their tablets.

Lipapa can import new data and covers for other web, which have the fb2 books. At the moment it has got about 10.000 books, most of them revised and very well formatted.

As fb2 is "xml", you can correct or reformat your books, with NotePad++ or XML Copy Editor (both free and excellents applications).

If you want to know more about Lipapa, you can visit LIPAPA MARTE - Gestión de e-books en FB2 (I have not publicity in my web ;)) or the web with the books (most of them in Spanish): www.papyrefb2.net

Well, I have still a little work in order to see my application working in android (I have never programmed for this platform, and I left Visual Basic 15 years ago), but Basic4Android seems to be all I need. It's an excellent resource!

Best regards.
 
Upvote 0

sdesan

Member
Licensed User
Longtime User
just a question: if i want to read files included in app (build-in putting them in Files folder) what i must write istead of
i.Initialize(i.ACTION_VIEW, "file:///sdcard/Fronteras2.fb2")
some thing like
i.Initialize(i.ACTION_VIEW, "file:///" & File.DirInternal & "/Fronteras2.fb2")
Any help is appreciated
 
Upvote 0
Top