Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Copying assets to internalStorage on android #67

Open
hagna opened this issue Nov 15, 2019 · 0 comments
Open

Copying assets to internalStorage on android #67

hagna opened this issue Nov 15, 2019 · 0 comments

Comments

@hagna
Copy link

hagna commented Nov 15, 2019

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant