android下使用java代码将streamingasset下的文件拷贝到persistence下——1

https://zhidao.baidu.com/question/647822595961002005.html

package com.cf.perfectsdk;

import android.app.Activity;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
import android.os.Looper;
import android.util.Log;

import java.io.InputStream;

public class api {
    
    
    private static api _instance;
    public static api instance()
    {
    
    
        if(null == _instance)
            _instance = new api();
        return _instance;
    }

    public int add(int a, int b)
    {
    
    
        return a+b;
    }

    public  static ClipboardManager clipboard = null;

    // 向剪贴板中添加文本
    public void copyTextToClipboard(final Context activity, final String str)
    {
    
    
        Log.d("TAG", "copyTextToClipboard333");
        if (Looper.myLooper() == null){
    
    
            Looper.prepare();
        }
        Log.d("TAG", "getSystemService333");
        clipboard = (ClipboardManager) activity.getSystemService(Activity.CLIPBOARD_SERVICE);
        ClipData textCd = ClipData.newPlainText("data", str);
        clipboard.setPrimaryClip(textCd);
    }


    public  String Unzip(final Context activity)
    {
    
    
        try
        {
    
    
            InputStream inputStream = activity.getAssets().open("hello.lua.txt");
            int length = inputStream.available();
            byte[] buffer = new byte[length];
            inputStream.read(buffer);
            String str = new String(buffer, 0, buffer.length, "UTF-8");
            Log.i("read file", str);
            return str;
        }
        catch (Exception ex)
        {
    
    
            Log.e("open file", "Unzip: open file hello.lua.txt error!");
            return "read hello.lua.txt error!";
        }
    }
}

using UnityEngine;
using System.Collections;
using System.Collections.Generic;
using XLua;
using System;
using System.IO;
using UnityEngine.UI;

namespace Tutorial
{
    
    
    public class CSCallLua : MonoBehaviour
    {
    
    
        public Text m_text;
        private AndroidJavaObject m_javaObject;

        LuaEnv luaenv = null;
        public class DClass
        {
    
    
            public int f1;
            public int f2;
        }

        [CSharpCallLua]
        public delegate int FDelegate(int a, string b, out DClass c, ref int x, ref string y);

        void Start()
        {
    
    
            m_javaObject = new AndroidJavaObject("com.cf.perfectsdk.MainActivity");
            luaenv = new LuaEnv();
            luaenv.AddLoader(SelfDefineLoader);
            luaenv.DoString("require 'hello'");
        }

        /// <summary>
        /// 自定义的路径的加载器
        /// </summary>
        /// <param name="filepath"></param>
        /// <returns></returns>
        public byte[] SelfDefineLoader(ref string filepath)
        {
    
    
            filepath = filepath.Replace('.', '/');
            filepath = Application.streamingAssetsPath + "/" + filepath + ".lua.txt";
            Debug.Log(filepath);
            if (File.Exists(filepath))
            {
    
    
                return File.ReadAllBytes(filepath);
            }
            else
            {
    
    
                return null;
            }
        }

        void OnDestroy()
        {
    
    
            luaenv.Dispose();
        }

        public void DoFile(string lua, LuaTable env = null)
        {
    
    
            string path = Application.persistentDataPath + "/" + lua + ".lua.txt";
            var txt = File.ReadAllText(path);
            luaenv.DoString(txt, lua, env);
        }

        public void Add()
        {
    
    
            int add = m_javaObject.Call<int>("add", 1, 2);
            m_text.text = "add=" + add;
        }

        public void SayHello()
        {
    
    
            //string str = m_javaObject.Call<string>("Unzip");
            //m_text.text = str;
            AndroidJavaObject androidObject = new AndroidJavaObject("com.cf.perfectsdk.api");
            AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");
            if (activity == null)
                return;
            string str = androidObject.Call<string>("Unzip", activity);
            m_text.text = str;
        }
    }
}

在这里插入图片描述
在这里插入图片描述

重点是这个函数:
InputStream inputStream = activity.getAssets().open(“hello.lua.txt”);
这里的 activity一定是主的activity,也就是:

AndroidJavaObject activity = new AndroidJavaClass("com.unity3d.player.UnityPlayer").GetStatic<AndroidJavaObject>("currentActivity");

传递给java端。

在这里插入图片描述
同时注意函数的签名。

Guess you like

Origin blog.csdn.net/wodownload2/article/details/120205717