[android] Open local (pdf) file?

Hello,

I’d like to let the user read a PDF when he clicked on a button (open the file in the associated application for PDF files) in an Android app. I understood that the assets are not readable like this so I tried to copy the file in an user directory to open it with a :
Lib.getURL (new URLRequest (filename));

At the moment, the copying of the file works but the getURL fails silently.

So :

  • is it a bug or just not possible to do that ?
  • does exists plugins to do that ?

Thanks,
David.

Lib.getURL is going to try and open a browser. On the desktop, you could use Sys.command with “open”, “xdg-open” or just the file name, to open on Mac, Linux or Windows, respectively.

On Android, this falls into the category of intents, I’ve never tried launching a file like this from an Android application, so the trick would be knowing what you need to do to trigger an intent on Android, probably attaching something that tells it you’re looking for a PDF reader

Thank you, I’ll try to investigate this way !

hello, I am quite interested by this topic.
I am in need of reading a pdf file from my openfl application. I am first starting it on desktop and afterwards, it’ll go on android.
I made some research and I have just found this:
http://lib.haxe.org/legacy/p/hxpdf

it is a legacy library for accessing pdf files. And, I assume there is a need to have this library installed:

but as hxpdf is a legacy library, I don’t really know if it will work with nicely with the new version of haxe(3.1.3) and openfl(2.1.8).
I haven’t tried hxpdf yet as I have just discovered it.
Hope it will help, and open new roads.

FIY:
openfl 2.2.4 doesn’t work on my machine.
it says:

Called from openfl/_v2/media/SoundChannel.hx line 312
Called from openfl/_v2/Lib.hx line 193
Called from C:\HaxeToolkit\haxe\std/neko/Lib.hx line 30
Uncaught exception - load.c(357) : Primitive not found : ./lime@lime_sound_channel_set_pitch(2)

on an empty project. so I am using 2.1.8

I think hxpdf has become a part of the “format” library that you can find here : https://github.com/HaxeFoundation/format/tree/master/format/pdf

Didn’t try it because I only needed to open the PDF with the associated application. So I made a new extension with this code in the Java (my Class is name FileOpen… quick and dirty) :

public static void openFile(String path, String type){
	
	Intent intent = new Intent();
	intent.setAction(android.content.Intent.ACTION_VIEW);
	File file = new File(path);
	intent.setDataAndType(Uri.fromFile(file), type);
	mainActivity.startActivity(intent); 
	
}

And this code for the haxe part (on an MouseEvent.CLICK) :

var str:String = "pdf/cgu.pdf"; // make sure this is in your assets
    var splittedFile:Array<String> = str.split('/');
    // Config.directory contains a string with the anme of my custom directory where I put every user generated file
    var filename:String = SystemPath.documentsDirectory + Config.directory + "/" + splittedFile[splittedFile.length - 1];
    if (!FileSystem.exists(filename))
    {
    	var bytes:ByteArray = Assets.getBytes(str);
    	File.saveContent(filename, bytes.asString());
    }
    FileOpen.openFile(filename, 'application/pdf');

And tada… it works !

Hi @davellx,

I am trying to have the same open file option but this time its for image files or gallery folder should open. How can I use the above example kindly lead me, as so far I have tried:

FileReference.browse()

Lime.ui.Dialogo

also I will love to hear about how can I create android extension for openfl ?

The Lime API for this would be lime.system.System.openFile, which should work on the desktop or HTML5, but I’m not sure this is implemented properly for Android.

If we have an idea that would work within the Lime API, I’d be open to accepting changes to Lime to have this functionality

1 Like

Finally I implemented it today: https://github.com/justnajm/openfl-open-image-file-android

But your mentioned approach will be best as it was very tricky to resolve all extension related issues and also android code…

2 Likes