[CORDOVA] assets 폴더 통째로 옮겨서 새로 로드 하기

9 views
Skip to first unread message

young chol seo

unread,
Apr 23, 2019, 12:50:14 AM4/23/19
to kamjaroid
 assets 폴더 통째로 옮겨서 새로 로드 하기 


/*
Licensed to the Apache Software Foundation (ASF) under one
or more contributor license agreements. See the NOTICE file
distributed with this work for additional information
regarding copyright ownership. The ASF licenses this file
to you under the Apache License, Version 2.0 (the
"License"); you may not use this file except in compliance
with the License. You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing,
software distributed under the License is distributed on an
"AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
KIND, either express or implied. See the License for the
specific language governing permissions and limitations
under the License.
*/

package com.test.hello;

import android.content.Context;
import android.content.res.AssetManager;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.widget.Toast;

import org.apache.cordova.*;

import java.io.BufferedOutputStream;
import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStream;
import java.io.OutputStreamWriter;
import java.util.logging.Logger;

public class MainActivity extends CordovaActivity
{
static String TAG = "LOG_H";//LOG_H HTML FILE FROM ASSETS FOLDER IN ANDROID WEBVIEW
Context ctx;

@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);

ctx = this.getApplicationContext();

// enable Cordova apps to be started in the background
Bundle extras = getIntent().getExtras();
if (extras != null && extras.getBoolean("cdvStartInBackground", false)) {
moveTaskToBack(true);
}

// Set by <content src="index.html" /> in config.xml
//loadUrl(launchUrl);

//String json= loadContentFromFile(ctx, "config.xml");
//String json= loadContentFromFile(ctx, "www/js/test.html");
//Log.d(TAG, json);

//writeToFile();

//String json2= loadContentFromFile(ctx, "test2.html");
//String json2 = readFileAsString(ctx ,"test2.html" );
//Log.d(TAG, json2);

//String html = readFileAsString(ctx,"test2.html");
//Log.d(TAG , "===================");
// Log.d(TAG , html);
//Log.d(TAG , "===================");
//html = "/data/user/0/com.test.hello/files/test2.html.html";
// try {
// loadUrl(html);
// }catch (Exception e){
// Log.d(TAG , e.toString());
// }
//UnZip();

String assetPath = "file:///android_asset";
String newFilePath = "www/";

try {
copyAsset("www");

String str = readFileAsString(ctx , "www/index.html");

}catch (Exception e){
Log.d(TAG,"1ERROR : " + e.toString());
}

Log.d(TAG, "LANCHEUER : "+launchUrl);//file:///android_asset/www/index.html
loadUrl("file:///storage/emulated/0/Android/data/com.test.hello/files/www/index.html");
}

/**
* Copy the asset at the specified path to this app's data directory. If the
* asset is a directory, its contents are also copied.
*
* @param path
* Path to asset, relative to app's assets directory.
*/
private void copyAsset(String path) {
AssetManager manager = getAssets();

// If we have a directory, we make it and recurse. If a file, we copy its
// contents.
try {
String[] contents = manager.list(path);

// The documentation suggests that list throws an IOException, but doesn't
// say under what conditions. It'd be nice if it did so when the path was
// to a file. That doesn't appear to be the case. If the returned array is
// null or has 0 length, we assume the path is to a file. This means empty
// directories will get turned into files.
if (contents == null || contents.length == 0)
throw new IOException();

// Make the directory.
File dir = new File(getExternalFilesDir(null), path);
dir.mkdirs();

// Recurse on the contents.
for (String entry : contents) {
copyAsset(path + "/" + entry);
}
} catch (IOException e) {
copyFileAsset(path);
}
}

/**
* Copy the asset file specified by path to app's data directory. Assumes
* parent directories have already been created.
*
* @param path
* Path to asset, relative to app's assets directory.
*/
private void copyFileAsset(String path) {
File file = new File(getExternalFilesDir(null), path);
String dirPath = file.getPath();
Log.d(TAG , "dirPath : " + dirPath );
try {
InputStream in = getAssets().open(path);
OutputStream out = new FileOutputStream(file);
byte[] buffer = new byte[1024];
int read = in.read(buffer);
while (read != -1) {
out.write(buffer, 0, read);
read = in.read(buffer);
Log.d(TAG, "FILE WRITE........");
}
out.close();
in.close();
} catch (IOException e) {
Log.e(TAG,e.toString());
}
}

public static void Zip(){
String backupDBPath = Environment.getExternalStorageDirectory().getPath() + "/Mobikul-Pos-db-backup";
final File backupDBFolder = new File(backupDBPath);
backupDBFolder.mkdirs();
final File backupDB = new File(backupDBFolder, "/db_pos.db");
String[] s = new String[1];
s[0] = backupDB.getAbsolutePath();
try {
ZipManager.zip(s, backupDBPath + "/pos_demo.zip");
}catch (Exception e){

}
}

public static void UnZip(){
String backupDBPath = Environment.getExternalStorageDirectory().getPath() + "/Mobikul-Pos-db-backup";
File sd = Environment.getExternalStorageDirectory();
if (sd.canWrite()) {
try {
final File backupDBFolder = new File(sd.getPath());
ZipManager.unzip(backupDBPath, backupDBFolder.getPath());
} catch (Exception e){

}
}
}


public static InputStream loadInputStreamFromAssetFile(Context context, String fileName){
AssetManager am = context.getAssets();
try {
InputStream is = am.open(fileName);
return is;
} catch (IOException e) {
e.printStackTrace();
}
return null;
}

public static String loadContentFromFile(Context context, String path){
String content = null;
try {
InputStream is = loadInputStreamFromAssetFile(context, path);
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();
content = new String(buffer, "UTF-8");
} catch (IOException ex) {
ex.printStackTrace();
return null;
}
return content;
}



//String S_NOTE = "note.txt";
private void writeToFile() {
AssetManager am = getResources().getAssets() ;
InputStream is = null ;
FileOutputStream fos = null ;
byte buf[] = new byte[1024] ;

String dir = "";
try {

is = am.open("www/js/test.html") ;
fos = new FileOutputStream(getFilesDir() + "/test2.html") ;


while (is.read(buf) > 0) {
fos.write(buf) ;
}

dir = getFilesDir() + "test2.html";
Log.d(TAG, dir);
fos.close() ;
is.close() ;

} catch (Exception e) {
e.printStackTrace();
Log.d(TAG, "파일을 쓰다" + e.toString());
}
}

public static String readFileAsString(Context context , String fileName) {
StringBuilder stringBuilder = new StringBuilder();
String line;
BufferedReader in = null;

try {
in = new BufferedReader(new FileReader(new File(context.getFilesDir(), fileName)));
while ((line = in.readLine()) != null) stringBuilder.append(line);

} catch (FileNotFoundException e) {
Log.d(TAG , e.toString());
//Log.d(TAG, e.toString());
} catch (IOException e) {
Log.d(TAG, e.toString());
}

return stringBuilder.toString();
}

}
Reply all
Reply to author
Forward
0 new messages