-1

我是开发 Android 应用程序的新手。关于我的问题,我找到了以下帖子,但我不确定如何将它实施到我现有的项目中。 如何将文件从“资产”文件夹复制到 SD 卡?

我想将它实现到 ZhuangDict,一个能够读取 Stardict 文件的字典应用程序。 http://code.google.com/p/zhuang-dict/source/browse/trunk/ZhuangDict/src/cn/wangdazhuang/zdict/ZhuangDictActivity.java

ZhuangDict 在第一次启动时会创建一个名为“zdict”的空目录。我想做的是将我自己的 stardict 文件从资产复制到 zdict 目录。

我的编程知识为零。如果您能给我提供一个查看 Google 代码中的 ZhuangDict 源代码的分步指南,我将不胜感激。

4

2 回答 2

0

看一下这个

private static String DB_PATH = "/data/data/com.packagename.myapp/databases/";

   try {
        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(
                "dbname.sqlite");
        // Path to the just created empty db
        String outFileName = DB_PATH + DATABASE_NAME;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[myInput.available()];
        int read;
        while ((read = myInput.read(buffer)) != -1) {
            myOutput.write(buffer, 0, read);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();

    } catch (IOException e) {
        Log.e(TAG, "copyDataBase Error : " + e.getMessage());
    }
于 2013-03-20T10:07:40.543 回答
0

您还可以将文件放在原始资源 (/res/raw) 文件夹中并使用此标准功能:

protected void CopyResource(int aInputIDResource, String aOutputFileName, boolean afForceWrite) {
        if (afForceWrite == false) {
            InputStream theTestExist;
            try {
                theTestExist = openFileInput(aOutputFileName);
                theTestExist.close();
                return;
            } catch (IOException e) {
            }
        }

        char[] theBuffer = new char[1024];
        int theLength;

        try {
            OutputStreamWriter out = new OutputStreamWriter(openFileOutput(
                    aOutputFileName, MODE_WORLD_READABLE), "ISO-8859-1");
            InputStreamReader in = new InputStreamReader(getResources()
                    .openRawResource(aInputIDResource), "ISO-8859-1");

            while ((theLength = in.read(theBuffer)) > 0)
                out.write(theBuffer, 0, theLength);

            out.flush();
            out.close();
        } catch (Exception e) {
            Log.e("TAG", "Error: " + e.getMessage());
        }
    }

然后在您的应用程序或活动的 onCreate() 函数中,调用:

CopyResource(R.raw.your_database, "MR_SBusPlugin.so", false);
于 2013-03-20T10:20:49.743 回答