iffy / wiish

Why Is It So Hard (to make a cross-platform app)?
MIT License
116 stars 7 forks source link

Copying assets to internalStorage on android #67

Open hagna opened 5 years ago

hagna commented 5 years ago

I needed to copy assets to internalstorage on android, so I made these changes to wiish:

diff --git a/wiishpkg/building/data/android-webview/app/src/main/java/org/wiish/exampleapp/WiishActivity.java b/wiishpkg/building/data/android-webview/app/src/main/java/org/wiish/exampleapp/WiishActivity.java
index efdbcc0..f729d20 100644
--- a/wiishpkg/building/data/android-webview/app/src/main/java/org/wiish/exampleapp/WiishActivity.java
+++ b/wiishpkg/building/data/android-webview/app/src/main/java/org/wiish/exampleapp/WiishActivity.java
@@ -1,6 +1,11 @@
 package org.wiish.exampleapp;

 import java.lang.Thread;
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.OutputStream;
+import java.io.IOException;
+import java.io.InputStream;

 import android.app.Activity;
 import android.os.Bundle;
@@ -15,6 +20,7 @@ import android.widget.LinearLayout.LayoutParams;
 import android.graphics.Bitmap;
 import android.os.Message;
 import android.util.Log;
+import android.content.res.AssetManager;

 // Log.d(TAG, "message");

@@ -59,6 +65,40 @@ public class WiishActivity extends Activity {
    public native void wiish_sendMessageToNim(String message);
    public native void wiish_signalJSIsReady();

+   public void copyAssets() {
+       AssetManager assetManager = getAssets();
+       String[] files = null;
+       try {
+       files = assetManager.list("");
+       } catch (IOException e) {
+       Log.e("tag", "Failed to get asset file list.", e);
+       }
+       for(String filename : files) {
+       InputStream in = null;
+       OutputStream out = null;
+       try {
+         in = assetManager.open(filename);
+         File outFile = new File(getInternalStoragePath(), filename);
+         out = new FileOutputStream(outFile);
+         copyFile(in, out);
+         in.close();
+         in = null;
+         out.flush();
+         out.close();
+         out = null;
+       } catch(IOException e) {
+           Log.e("tag", "Failed to copy asset file: " + filename, e);
+       }       
+       }
+   }
+   public void copyFile(InputStream in, OutputStream out) throws IOException {
+       byte[] buffer = new byte[1024];
+       int read;
+       while((read = in.read(buffer)) != -1){
+         out.write(buffer, 0, read);
+       }
+   }
+
    public void evalJavaScript(final String js) {
        if (webView == null) {
            return;
diff --git a/wiishpkg/webview_mobile.nim b/wiishpkg/webview_mobile.nim
index aaf5f3b..6c0ec89 100644
--- a/wiishpkg/webview_mobile.nim
+++ b/wiishpkg/webview_mobile.nim
@@ -26,6 +26,7 @@ when defined(android):
   jclass org.wiish.wiishexample.WiishActivity of JVMObject:
     proc evalJavaScript*(js: string)
     proc getInternalStoragePath*(): string
+    proc copyAssets*()

 type
   WebviewApp* = ref object of BaseApplication

I'm not sure what wiish should do, but I'm sure I would rather make these changes in my own project repo.