-1

I'm new in developing Android app. Regarding my question, I've found the following post, but I'm not sure how to implement it to my existing project. How to copy files from 'assets' folder to sdcard?

I would like to implement it to ZhuangDict, a dictionary app capable of reading Stardict file. http://code.google.com/p/zhuang-dict/source/browse/trunk/ZhuangDict/src/cn/wangdazhuang/zdict/ZhuangDictActivity.java

ZhuanDict creates an empty directory called "zdict" on first start. What I wish to do is to copy my own stardict files from asset to zdict directory.

I have zero knowledge in programming. I would appreciate if you can provide me a step by step guide looking at the ZhuangDict source code from Google code.

Community
  • 1
  • 1
user2190227
  • 23
  • 1
  • 7
  • 1
    This site is targeted at programmers, who encounter specific problems. If you have zero knowledge in programming, perhaps find a programmer? – Shade Mar 27 '13 at 23:28

2 Answers2

0

Check this out

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());
    }
Sanket Pandya
  • 1,095
  • 7
  • 21
  • Thanks for your reply. I'm new in developing Android app without prior programming knowledge. Where should I put this code? In ZhuangDictActivity.java? Should I put it under which line? – user2190227 Mar 20 '13 at 10:12
  • You can place it anywhere when you want to copy the content.Place it in onCreate method for a try – Sanket Pandya Mar 20 '13 at 10:15
  • I'm sorry. I need a step by step guide. I have zero knowledge in programming. Can you look at the ZhuangDict source code from Google code and guide me? – user2190227 Mar 20 '13 at 10:24
  • Place it after line number 303 in your code – Sanket Pandya Mar 20 '13 at 10:31
  • I do not understand since your code mentioned about database. I do not intend to copy database. I just want to copy stardict .dict, .ifo and .idx files (there are 3 files) to sdcard/zdict/ – user2190227 Mar 20 '13 at 10:58
  • Thank you. The answer you provided may work, but I don't have enough knowledge to do it myself. Probably I'll learn the complete android development starting from the basic in the future. – user2190227 Mar 23 '13 at 12:19
0

You can also put your file in the raw resource (/res/raw) folder and use this standard function:

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());
        }
    }

Then in the function onCreate() of your application or activity, call:

CopyResource(R.raw.your_database, "MR_SBusPlugin.so", false);
Robin
  • 10,052
  • 6
  • 31
  • 52
  • I'm sorry. I need a step by step guide since I have zero knowledge in programming. Can you tell me where I should place the code in ZhuangDictActivity.java? – user2190227 Mar 20 '13 at 10:29