Reading a Private Folder file in html file

886 views
Skip to first unread message

Mauritz Zondagh

unread,
May 3, 2017, 6:17:35 AM5/3/17
to DroidScript
Hi,

Can anyone point me in a direction how to read a text file created in a Private Folder in my Javascript app, within a html file used for webview. I only need to point the html to the Private folder.
e.g. readTextFile("file:///sdcard/Droidscript/TestShare/Image_Data1.txt"); This is what i have now i nthe htm file, need to get the path of the file in private folder created in the javascript app.
Javascript Loadtext and Savetext would be nice, how to access this from html?

Thanks

Mauritz Zondagh

unread,
May 3, 2017, 6:43:35 AM5/3/17
to DroidScript
Just to make it clear, i can read a absolute file reference text file within my html successfully, but i want to change the html code to read from a Private folder text file (created in the javascript)
The HTML does not see the variable in the javascript file.

Mauritz Zondagh

unread,
May 4, 2017, 9:05:58 AM5/4/17
to DroidScript
I found some information on DroidScript Group on sharing data between js and html.

I get a error "ReceiveAppData" is not defined. Not sure if the error relates to the html of js file, and how to solve this. If anyone can help?

In the JS file is do the following

// String prototype to facilitate data exchange to webview
String.prototype.escString=function() {
  return this.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
}

function OnStart()

    .....
   
    web1 = app.CreateWebView( ImgW, ImgH );
    var rec = {};
    rec.item1 = PFPathVCF;
    rec.item2 = "Hallo";    
    web1.Execute("ReceiveAppData(\"" + JSON.stringify(rec).escString() + "\");");  // ^^^^^^^^^ this global function must be defined in webview script


In HTML file is do this

<html>
    
<head>
    <script src="file:///android_asset/Easy_Launcher.js"></script>
</head>

<script>

    // String prototype to facilitate data exchange
    String.prototype.escString=function()
    {
        return this.replace(/\\/g, "\\\\").replace(/"/g, '\\"');
    }    

    // Called when droidscript application is started
    function OnStart()
    {
        // Insert any additional initialisation code into here...
    }

    // Receive data from the droidscript app and insert into text imputs...
    function ReceiveAppData( rec )
    {
        rec = JSON.parse(rec);
        document.getElementById("txtItem1").value = rec.item1;
        document.getElementById("txtItem2").value = rec.item2;
    }
</script>

     <body>
      To use the file

      var FilePath = document.getElementById("txtItem1").value+"/Image_Data1.txt";

Thanks

Steve Garman

unread,
May 4, 2017, 9:21:28 AM5/4/17
to DroidScript
It doesn't look like you are loading your html file into web1 before you do web1.Execute

Mauritz Zondagh

unread,
May 4, 2017, 9:55:11 AM5/4/17
to DroidScript
Ok, so i changed the order to 

  web1 = app.CreateWebView( ImgW, ImgH );
    var rec = {};
    rec.item1 = PFPathVCF;
    rec.item2 = "Hallo";    
    web1.LoadUrl( "file:sdcard/Droidscript/Easy_Launcher/Contact_Image1.html","allowzoom");
    web1.Execute("ReceiveAppData(\"" + JSON.stringify(rec).escString() + "\");");  // ^^^^^^^^^ this global function must be defined in webview script


Same error
Thanks

Mauritz Zondagh

unread,
May 4, 2017, 3:29:48 PM5/4/17
to DroidScript
Eureka, I found a way!

The key is that u should load your html as a variable inside my javascript file, and then to view the htm in a webview use LoadHtml. 
If u create a separate html file with your html code, and call it via LoadURL you cannot pass variable, cannot use alert ext.
(Maybe there is a way, but i could not solve it)

So there was a post from Steve Garman on Droidscript "Passing parameters to html file" showing how to load a html variable into a webview - all done from javascript file.

Here is my implementation where i use a  phone contact file (*.vcf), extract the BASE64 image data and display it on a webview.

web1 = app.CreateWebView( ImgW, ImgH );
var html1;
var Image_Data1 = Create_Image_Contacts(PFPathVCF+"/Contact1.vcf");
//var Image_Data1 = "<img src=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAHFJREFUWIXt1jsKgDAQRdF7xY25cpcWC60kioI6Fm/ahHBCMh+BRmGMnAgEWnvPpzK8dvrFCCCAcoD8og4c5Lr6WB3Q3l1TBwLYPuF3YS1gn1HphgEEEABcKERrGy0E3B0HFJg7C1N/f/kTBBBA+Vi+AMkgFEvBPD17AAAAAElFTkSuQmCC>";
var html1 = "<html><head>";
html1 += "<meta name=viewport content=width=device-width>";
//html1 += "<img src=Img/Droid2.png>";
//html1 += "<img src=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAHFJREFUWIXt1jsKgDAQRdF7xY25cpcWC60kioI6Fm/ahHBCMh+BRmGMnAgEWnvPpzK8dvrFCCCAcoD8og4c5Lr6WB3Q3l1TBwLYPuF3YS1gn1HphgEEEABcKERrGy0E3B0HFJg7C1N/f/kTBBBA+Vi+AMkgFEvBPD17AAAAAElFTkSuQmCC>";
html1 += Image_Data1;
html1 += "</body></html>";
web1.LoadHtml(html1,"file:///Sys/");    


function Create_Image_Contacts(VCFFilename)
{
//  var vcfdata = app.ReadFile(VCFFilename);
    var vcfdata = app.ReadFile("/sdcard/Droidscript/Easy_Launcher/AA Nooddiens.vcf");
    var pos =vcfdata.search("PHOTO;ENCODING=BASE64;"); // Return -1 if no match is found
    var TempString = vcfdata.substring(pos+21);
    pos = TempString.search(":"); // Return -1 if no match is found
    TempString = TempString.substring(pos+1);
    pos =TempString.search("END:VCARD"); // Return -1 if no match is found    
    TempString = TempString.substring(0,pos-1);
    TempString = TempString.replace(/[\r|\n|\r\n|\n\r]/g,"").replace(/ /g,""); // Replace space with ""; // Remove the \n\r between lines
    TempString = "<img src=data:image/png;base64,"+TempString+">";
    //TempString = "<img src=data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAACAAAAAgCAYAAABzenr0AAAABHNCSVQICAgIfAhkiAAAAHFJREFUWIXt1jsKgDAQRdF7xY25cpcWC60kioI6Fm/ahHBCMh+BRmGMnAgEWnvPpzK8dvrFCCCAcoD8og4c5Lr6WB3Q3l1TBwLYPuF3YS1gn1HphgEEEABcKERrGy0E3B0HFJg7C1N/f/kTBBBA+Vi+AMkgFEvBPD17AAAAAElFTkSuQmCC>";
    return TempString;


So this solve passing parameters. My main intention is to show a contact image on my Droidscript app. This still fails in the way i implement it. I want to share the vcf file with my app, but is still seems that sharing a vcf file  (receive) fails in Droidscript.

Mauritz Zondagh

unread,
May 6, 2017, 2:14:53 PM5/6/17
to DroidScript
I can just make one correction on the above code (For the benefit of other users)

The line detecting the end of the PHOTO image data in a vcf file 

pos =TempString.search("END:VCARD"); // Return -1 if no match is found

should rather be
pos =TempString.search("\n\n"); // Return -1 if no match is found    

It seems that the PHOTO base64 data end with a empty line
Reply all
Reply to author
Forward
0 new messages