- Home /
How to open a pdf on android device from streaming assets folder
Hi,
I've been banging my head against the wall trying to figure this out. I've scoured the forums and have implemented what I think is the correct solution, but it is still not working.
I'm trying open a pdf in the device's web browser. The PDF is located in my streaming assets folder. I'm calling the following from OnGUI when a button is pressed.
string path;
//set the correct path to the rules files
if (Application.platform == RuntimePlatform.Android) {
path = System.IO.Path.Combine(Application.streamingAssetsPath, Tag.rulesFilename);
WWW www = new WWW(path);
while (!www.isDone) { //I will replac e this with a Coroutine once its working
Debug.Log ("LOADING FILE: " + www.progress); //returns 0 for a few iterations
};
Debug.Log ("FILE LOADED: " + www.progress); //returns 1
Debug.Log ("FILE EXISTS: " + (System.IO.File.Exists(path)?"YES":"NO")); //returns NO
Debug.Log("ANDROID PATH " + path); //returns ANDROID PATH jar:file:///mnt/asec/com.me.app-1/pkg.apk!/assets/file.pdf
Application.OpenURL(www.url); //intent launches but nothing happens
}
else {
path = System.IO.Path.Combine(Application.streamingAssetsPath, Tag.rulesFilename);
Application.OpenURL(path);
}
The non-android portion works fine in the editor and a standalone windows exe. I've also check that the following will open a web browser on android.
Application.OpenURL("www.google.com");
Anyone have any ideas?
Thanks
I myself are looking to open / launch a .pdf from a onGui button on an android device, can't seem to get anything to work though!
Jumping on here to see if we can get some help!
+1!
I'm trying this for reference:
if (GUI.Button (new Rect (750, 750, buttonSize3, buttonSize3), btn3Img, btnGUIStyle)) {
Application.OpenURL("jar:https://" + Application.dataPath + "!/assets/" + "testpdf.pdf");
}
Turns out if the pdf is hosted online, it just launches the browser and starts a file download, kinda want it to open a pdf viewer!
Sorry for Hi-Jack just trying to get some attention!
DazBailey,
The pdf downloading when hosted online is likely a function of the OS/device rather than the way you are opening it with unity. Check if you can change the default behavior for pdf files in the OSs' settings or install a pdf viewer on the device.
I'm still looking for a solution to open a local file...
I tried that and it works, but I only tried on windows, can't try it on android
Application.OpenURL("E:\\Download_E\\pdf.pdf"); //sample pdf I just downloaded
Answer by zerocorp · Oct 21, 2014 at 06:06 PM
you can not open directly from streamingassets, you must copy the pdf to the device and then open it.
place the pdf in the resources folder and change its extention to bytes
TextAsset pdfTem = Resources.Load("PDFs/"+namePDF, typeof(TextAsset)) as TextAsset;
copy the pdf file
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/"+namePDF+".pdf", pdfTem.bytes);
open the pdf file
Application.OpenURL(Application.persistentDataPath+"/"+namePDF+".pdf");
This does not work for me.
I get a "open failed: EACCES (Permission denied)" error in the console when running on a device.
try to do this:
player setting -> write access ->external
tried it. No longer get an error and intent starts with message
START u0 {act=android.intent.action.VIEW dat=file:///<file name and path> from pid 1234
But nothing happens.
you can show me part of code? what version of Unity you use?
I'm using 4.5.4f1
Here's the code snippet. It's pretty much a direct copy of yours.
string filename = "pdffile";
TextAsset pdfTemp = Resources.Load ("pdf/" + filename, typeof(TextAsset)) as TextAsset;
System.IO.File.WriteAllBytes(Application.persistentDataPath + "/" + filename + ".pdf", pdfTemp.bytes);
Application.OpenURL(Application.persistentDataPath + "/" + filename + ".pdf");
Answer by Johny Maia · Dec 05, 2015 at 06:10 PM
1 - place the PDF file in the Resources folder 2 - CHANGE a paragraph Extension .bytes 3 - goes playerSettings > otherSettings and change the write acess to external ( sdcard ) 4- use the below method paragraph LOAD File
using UnityEngine;
using System.Collections;
using System.IO;
public class BtnOpenPDF : MonoBehaviour
{
#region Public Methods
public void OpenPDF(string filename)
{
string path = System.IO.Path.Combine(Application.persistentDataPath, filename + ".pdf");
TextAsset pdfTemp = Resources.Load(filename, typeof(TextAsset)) as TextAsset;
File.WriteAllBytes(path, pdfTemp.bytes);
Application.OpenURL(path);
}
#endregion
}
What does "CHANGE a paragraph Extension .bytes" mean?
After spending too much hours on that, this method work, thank you really much ! For the other person struggling with this, you can use this code but remember to put your pdf file in a .bytes extension (just rename the file after showing their extension in the name, http://kb.winzip.com/kb/entry/26/ ) ;)
Hi Jamy4000 Sorry to bother you after this time but .. Did you succed ? I try this but still nothing on my app. Can you share how you did if possible ?
Hey Aurelien,
In the end the only way I found to open a pdf is to use a server and put my pdf on it. I then call the function Application.OpenURL(path), here's a couple of line of code, if that can help :
string path = "https://yourServer/PDFsDirectory/"; //Path to your server containing the PDFs
public void OpenPDF()
{
try
{
string filename = this.gameObject.name;
path += filename + ".pdf";
Application.OpenURL(path);
} catch (IOException e)
{
Debug.Log("Error at ImagePicklerScript :\n" + e.ToString());
}
}
I just tested it on Android, not on iOS, but i don't see why it should not work :) Hope it will help you !
Answer by jkramar · Oct 23, 2014 at 07:14 PM
That is exactly what I have.
The problem is not finding the resource - when I debug, pdfTemp exists and has a size. It is the openURL call that starts the intent and then nothing happens.
O_o should work... if you want, you can spend your package and revised it
Answer by Sylafrs · Dec 30, 2014 at 04:42 PM
Do not use "file://" in your path.. for some reasons, on Android, it doesn't work..
In my code I have something like that :
string path = Application.persistentDataPath + "/SavedScreen.pdf";
yield return pdfCreater.ExportPDF(path);
#if UNITY_EDITOR && !FORCE_PHONE
Application.OpenURL("file://" + path);
#elif UNITY_IPHONE
EtceteraBinding.showWebPage("file://" + path, true);
#elif UNITY_ANDROID
Application.OpenURL(path);
#else
Application.OpenURL("file://" + path);
#endif
Your answer
Follow this Question
Related Questions
How to open PDF on particular page on Android? 0 Answers
Download video and put it into StreamingAssets folder 0 Answers
Open screenshot in file location Android 0 Answers
Streaming assets android 0 Answers
Sqlite4Unity3d difficulties 1 Answer