How to open a document with the default application

743 views
Skip to first unread message

sebastián lópez

unread,
Jan 4, 2017, 11:39:45 AM1/4/17
to Kivy users support
Hi

I would like to know how to open a document with the default application under Android by given the path.

Thanks!

ZenCODE

unread,
Jan 4, 2017, 3:23:54 PM1/4/17
to Kivy users support
import webbrowser

webbrowser
.open("path/to/file")



That should do it?

sebastián lópez

unread,
Jan 4, 2017, 8:29:36 PM1/4/17
to Kivy users support
Hi, It doesn't work

So I'm trying with this

PythonActivity = autoclass('org.renpy.android.PythonActivity')
Intent = autoclass('android.content.Intent')
String = autoclass('java.lang.String') 
Uri = autoclass('android.net.Uri')
filename : is the file name full path
fileNameJava = cast('java.lang.CharSequence', String(filename))
file = cast('java.io.File', fileNameJava)
target = Intent()
target.SetAction(Intent.ACTION_VIEW)
target.setDataAndType(Uri.fromFile(file),"application/pdf")
target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
intent = Intent.createChooser(target, 'Abrir el archivo')
currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
currentActivity.startActivity(intent)

But it still doesn't work. I have tried using the Kivy Launcher but it fails

sebastián lópez

unread,
Jan 4, 2017, 8:55:44 PM1/4/17
to Kivy users support
I have found two errors

target.SetAction --> target.setAction 

urifromfile = Uri.fromFile(file2view)  # unknown

ZenCODE

unread,
Jan 4, 2017, 9:02:21 PM1/4/17
to Kivy users support
Why does it "not work"? What is the error? This works for us on all platforms (Sorry, got the function name wrong in the first post).

webbrowser.open_new(ad_item["/path/to/file.pdf")


sebastián lópez

unread,
Jan 4, 2017, 9:25:54 PM1/4/17
to Kivy users support
I try again your approach and it doesn't work, but don´t crash.

In the other hand a fix the error and obtain this script

        PythonActivity = autoclass('org.renpy.android.PythonActivity')  #request the Kivy activity instance
        Intent = autoclass('android.content.Intent')
        String = autoclass('java.lang.String') 
        Uri = autoclass('android.net.Uri')
        fileNameJava = cast('java.lang.CharSequence', String(filename))
        File = autoclass('java.io.File')
        target = Intent()
        target.setAction(Intent.ACTION_VIEW)
        urifromfile = Uri.fromFile(File(filename))
        target.setData(urifromfile)#setDataAndType( urifromfile,"application/pdf")
        target.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY)
        #intent = Intent.createChooser(target, 'Abrir el archivo')
        currentActivity = cast('android.app.Activity', PythonActivity.mActivity)
        currentActivity.startActivity(target)

It' doesn't crash and shows me a dialog to select an app in order to open the file
but then shows me the file doesn't exists

This is the path since it was created:
filename = '/storage/emulated/0/kivy/tarjetas/reportes/201.pdf'

And It's also the file I'm trying to open

ZenCODE

unread,
Jan 5, 2017, 12:43:33 AM1/5/17
to Kivy users support
Try making that

 'file://storage/emulated/0/kivy/tarjetas/reportes/201.pdf'

The problem with using PythonActivity is that it's Android only, and more complex for a simple task. Or perhaps you have another reason for preferring it?

sebastián lópez

unread,
Jan 5, 2017, 6:50:07 AM1/5/17
to Kivy users support
Hi,

My target os are android and windows,

so on windows I've been using os.startfile, and in the other hand the previous code.

I fix the error by putting a popup message before. I think the error was related to the time the system takes to create the pdf file
Reply all
Reply to author
Forward
0 new messages