Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>Here is how i ended up doing it. I used the Content:// protocol and set up a contentprovider to handle returning a file descriptor to the system</p> <p>Here is my fileContentProvider:</p> <pre><code>import java.io.File; import java.io.FileNotFoundException; import java.io.FileOutputStream; import java.io.IOException; import java.io.InputStream; import org.apache.commons.io.IOUtils; import android.content.ContentProvider; import android.content.ContentValues; import android.database.Cursor; import android.net.Uri; import android.os.ParcelFileDescriptor; import android.util.Log; public class FileContentProvider extends ContentProvider { @Override public ParcelFileDescriptor openFile(Uri uri, String mode) { Log.d("FileContentProvider","fetching: " + uri); ParcelFileDescriptor parcel = null; String fileNameRequested = uri.getLastPathSegment(); String[] name=fileNameRequested.split("\\."); String prefix=name[0]; String suffix=name[1]; // String path = getContext().getFilesDir().getAbsolutePath() + "/" + uri.getPath(); //String path=file:///android_asset/"+Consts.FILE_JAVASCRIPT+" /*check if this is a javascript file*/ if(suffix.equalsIgnoreCase("js")){ InputStream is = null; try { is = getContext().getAssets().open("www/"+Consts.FILE_JAVASCRIPT); } catch (IOException e1) { // TODO Auto-generated catch block e1.printStackTrace(); } File file = stream2file(is,prefix,suffix); try { parcel = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY); } catch (FileNotFoundException e) { Log.e("FileContentProvider", "uri " + uri.toString(), e); } } return parcel; } /*converts an inputstream to a temp file*/ public File stream2file (InputStream in,String prefix,String suffix) { File tempFile = null; try { tempFile = File.createTempFile(prefix, suffix); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } tempFile.deleteOnExit(); FileOutputStream out = null; try { out = new FileOutputStream(tempFile); } catch (FileNotFoundException e) { // TODO Auto-generated catch block e.printStackTrace(); } try { IOUtils.copy(in, out); } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } return tempFile; } @Override public boolean onCreate() { return true; } @Override public int delete(Uri uri, String s, String[] as) { throw new UnsupportedOperationException("Not supported by this provider"); } @Override public String getType(Uri uri) { throw new UnsupportedOperationException("Not supported by this provider"); } @Override public Uri insert(Uri uri, ContentValues contentvalues) { throw new UnsupportedOperationException("Not supported by this provider"); } @Override public Cursor query(Uri uri, String[] as, String s, String[] as1, String s1) { throw new UnsupportedOperationException("Not supported by this provider"); } @Override public int update(Uri uri, ContentValues contentvalues, String s, String[] as) { throw new UnsupportedOperationException("Not supported by this provider"); } } </code></pre> <p>in the manifest i defined the provider:</p> <pre><code>&lt;provider android:name="com.example.mypackage.FileContentProvider" android:authorities="com.example.fileprovider" /&gt; </code></pre> <p>Here is the javascript o inject into the webview:</p> <pre><code>webView.loadUrl("javascript:(function() { " + "var script=document.createElement('script'); " + " script.setAttribute('type','text/javascript'); " + " script.setAttribute('src', 'content://com.example.fileprovider/myjavascriptfile.js'); " /* + " script.onload = function(){ " + " test(); " + " }; " */ + "document.body.appendChild(script); " + "})();"); </code></pre> <p>and here is the myjavascriptfile.js (as an example):</p> <pre><code> function changeBackground(color) { document.body.style.backgroundColor = color; </code></pre> <p>}</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload