在APK中加入文件将它们存储在SD卡中

时间:2013-05-21 12:17:46

标签: android android-sdcard

由于某种原因,我需要加入一些defaut文件与我的apk但我需要写入SD卡因为我的应用程序尝试读取SD卡我需要存储我的默认文件在SD卡

如何做到这一点我无法将我的数据粘贴到我的根文件夹android拒绝它如果我将它粘贴在一个drawable文件夹上它似乎可以编译但我仍然duno如何获取内容并写在我的SD卡

我看到很多tuto tu从sd卡得到一个drawable但我不相反我会在sd卡上写一个文件从我加入到apk的默认文件

有什么想法吗?我没有代码,因为我没有任何提示,

1 个答案:

答案 0 :(得分:0)

压缩文件并将zip文件(将其命名为my_raw_files.zip)放入Eclipse中的文件夹res/raw,它将与您的应用程序一起打包。然后,您可以在应用程序首次启动时将其复制到外部存储(“sdcard”):

private static final File USER_DIR = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator
        + "myfolder");
...
boolean dirCreated = USER_DIR.mkdirs();
if (dirCreated)
{
  FilesUtil.unzipFiles(this.getResources().openRawResource(R.raw.my_raw_files),
                    USER_DIR.getAbsolutePath());
}
...
static public void unzipFiles(InputStream zipIS, String outputPath)
{
    try
    {
        // unzip files into existing folder structure
        ZipInputStream zin = new ZipInputStream(zipIS);
        try
        {
            ZipEntry ze;
            while ((ze = zin.getNextEntry()) != null)
            {
                if (!ze.isDirectory())
                {
                    Log.d(TAG, "unzip " + ze.getName());

                    ByteArrayOutputStream baos = new ByteArrayOutputStream();
                    byte[] buffer = new byte[1024];
                    int count;
                    FileOutputStream fout = new FileOutputStream(outputPath + File.separator + ze.getName());
                    while ((count = zin.read(buffer)) != -1)
                    {
                        baos.write(buffer, 0, count);
                        byte[] bytes = baos.toByteArray();
                        fout.write(bytes);
                        baos.reset();
                    }
                    fout.close();
                }
            }
        } finally
        {
            zin.close();
        }

    } catch (Exception e)
    {
        Log.e(TAG, "unzip", e);
    }

}