Wayback Machinekoobas.hobune.stream
May JUN Jul
Previous capture 13 Next capture
2021 2022 2023
1 capture
13 Jun 22 - 13 Jun 22
sparklines
Close Help
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
  • Asset Store
  • Get Unity

UNITY ACCOUNT

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account
  • Blog
  • Forums
  • Answers
  • Evangelists
  • User Groups
  • Beta Program
  • Advisory Panel

Navigation

  • Home
  • Products
  • Solutions
  • Made with Unity
  • Learning
  • Support & Services
  • Community
    • Blog
    • Forums
    • Answers
    • Evangelists
    • User Groups
    • Beta Program
    • Advisory Panel

Unity account

You need a Unity Account to shop in the Online and Asset Stores, participate in the Unity Community and manage your license portfolio. Login Create account

Language

  • Chinese
  • Spanish
  • Japanese
  • Korean
  • Portuguese
  • Ask a question
  • Spaces
    • Default
    • Help Room
    • META
    • Moderators
    • Topics
    • Questions
    • Users
    • Badges
  • Home /
avatar image
1
Question by jkramar · Jul 03, 2014 at 02:31 PM · androidstreamingassetspdfapplication.openurl

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

Comment
Add comment · Show 4
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image DazBailey · Jul 07, 2014 at 04:11 PM 0
Share

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!

avatar image DazBailey · Jul 07, 2014 at 10:16 PM 0
Share

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!

avatar image jkramar · Jul 10, 2014 at 01:35 PM 0
Share

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...

avatar image FunIsDangerous · Jul 04, 2017 at 02:58 PM 0
Share

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

alt text

4 Replies

· Add your reply
  • Sort: 
avatar image
5
Best Answer

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

  1. TextAsset pdfTem = Resources.Load("PDFs/"+namePDF, typeof(TextAsset)) as TextAsset;

copy the pdf file

  1. System.IO.File.WriteAllBytes(Application.persistentDataPath + "/"+namePDF+".pdf", pdfTem.bytes);

open the pdf file

  1. Application.OpenURL(Application.persistentDataPath+"/"+namePDF+".pdf");

Comment
Add comment · Show 8 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image jkramar · Oct 22, 2014 at 07:53 PM 0
Share

This does not work for me.

I get a "open failed: EACCES (Permission denied)" error in the console when running on a device.

avatar image zerocorp · Oct 22, 2014 at 08:31 PM 0
Share

try to do this:

player setting -> write access ->external

avatar image jkramar · Oct 23, 2014 at 12:09 AM 0
Share

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.

avatar image zerocorp · Oct 23, 2014 at 05:10 PM 0
Share

you can show me part of code? what version of Unity you use?

avatar image jkramar · Oct 23, 2014 at 05:53 PM 0
Share

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");

Show more comments
avatar image
1

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
 }
Comment
Add comment · Show 6 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image alexander_q · Dec 23, 2015 at 09:45 AM 1
Share

What does "CHANGE a paragraph Extension .bytes" mean?

avatar image Jamy4000 · Jul 04, 2017 at 02:52 PM 0
Share

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/ ) ;)

avatar image AurelienComParLImage Jamy4000 · Jan 12, 2018 at 01:38 PM 0
Share

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 ?

avatar image Jamy4000_Absolute AurelienComParLImage · Jan 12, 2018 at 02:01 PM 0
Share

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 !

Show more comments
avatar image
0

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.

Comment
Add comment · Show 1 · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users
avatar image zerocorp · Oct 23, 2014 at 08:14 PM 0
Share

O_o should work... if you want, you can spend your package and revised it

avatar image
0

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

Comment
Add comment · Share
10 |3000 characters needed characters left characters exceeded
▼
  • Viewable by all users
  • Viewable by moderators
  • Viewable by moderators and the original poster
  • Advanced visibility
Viewable by all users

Your answer

Hint: You can notify a user about this post by typing @username

Up to 2 attachments (including images) can be used with a maximum of 524.3 kB each and 1.0 MB total.

Follow this Question

Answers Answers and Comments

11 People are following this question.

avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image avatar image

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


Enterprise
Social Q&A

Social
Subscribe on YouTube social-youtube Follow on LinkedIn social-linkedin Follow on Twitter social-twitter Follow on Facebook social-facebook Follow on Instagram social-instagram

Footer

  • Purchase
    • Products
    • Subscription
    • Asset Store
    • Unity Gear
    • Resellers
  • Education
    • Students
    • Educators
    • Certification
    • Learn
    • Center of Excellence
  • Download
    • Unity
    • Beta Program
  • Unity Labs
    • Labs
    • Publications
  • Resources
    • Learn platform
    • Community
    • Documentation
    • Unity QA
    • FAQ
    • Services Status
    • Connect
  • About Unity
    • About Us
    • Blog
    • Events
    • Careers
    • Contact
    • Press
    • Partners
    • Affiliates
    • Security
Copyright © 2020 Unity Technologies
  • Legal
  • Privacy Policy
  • Cookies
  • Do Not Sell My Personal Information
  • Cookies Settings
"Unity", Unity logos, and other Unity trademarks are trademarks or registered trademarks of Unity Technologies or its affiliates in the U.S. and elsewhere (more info here). Other names or brands are trademarks of their respective owners.
  • Anonymous
  • Sign in
  • Create
  • Ask a question
  • Spaces
  • Default
  • Help Room
  • META
  • Moderators
  • Explore
  • Topics
  • Questions
  • Users
  • Badges