4

I have an android app with a webview.

Whenever a user clicks on a button, JavaScript creates a blob, puts text in it and downloads it.

Here's the function that does this:

function saveTextAsFile(A)
{
    FillTextToWrite(A);
    var textFileAsBlob = new Blob([textToWrite], {
        type: 'text/plain'
    });
    var downloadLink = document.createElement("a");
    downloadLink.download = "Analysis.txt";
    downloadLink.innerHTML = "Download File";
    if (window.webkitURL != null)
    {
        downloadLink.href = window.webkitURL.createObjectURL(textFileAsBlob);
    }
    else
    {
        downloadLink.href = window.URL.createObjectURL(textFileAsBlob);
        downloadLink.onclick = destroyClickedElement;
        downloadLink.style.display = "none";
        document.body.appendChild(downloadLink);
    }

    downloadLink.click();
}

It works fine in any browser, but when I try to download it in the app, nothing happens.

Is there a way to download the blob in the app or is it easier to change the JavaScript?

I need the JavaScript to work on browser as well as on the android app, so sending the blob to the android app in JavaScript will not work on browser.

2
  • Maybe this question/answers will help. stackoverflow.com/questions/33434532/… Commented Jan 11, 2017 at 14:40
  • 1
    @bobjoe Thank you for your answer, but it is solved. I forgot about this question but I will answer my solution. Commented Jan 11, 2017 at 14:43

1 Answer 1

1

The solution I found was to detect wheter the javascript is running in Android app or in browser (by adding a string to the user agent).

When in Android app I am sending variable textToWrite (which would normally go into the blob) to the java.

In javascript:

if(App){
    Android.sendData(textToWrite);
} else {//make blob}

In java:

myWebView.addJavascriptInterface(new myJavascriptInterface(this), "Android");

public class myJavascriptInterface {
    Context mContext;

    myJavascriptInterface(Context c) {
        mContext = c;
    }

    @JavascriptInterface
    public void sendData(String data) {
        //save data as file
    }
}
Sign up to request clarification or add additional context in comments.

2 Comments

What about IOS??
@Jacksonmj the app was not written for iOS

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.