【问题标题】:How to add sd-card's files in app?如何在应用程序中添加 sd 卡的文件?
【发布时间】:2012-09-02 17:13:24
【问题描述】:

我需要在我的应用程序中添加一些文件,并且这些文件必须在应用程序安装后解压到 sd 卡。在安卓上可以吗?我该怎么做?

【问题讨论】:

    标签: android file apk sd-card


    【解决方案1】:

    将文件添加到您的资源/原始目录。当应用程序运行时,您可以检查目标文件是否存在。如果不存在,unpack and write to the SD card:

    File dest = Environment.getExternalStorageDirectory();
    InputStream in = context.getResources().openRawResource(R.raw.file);
    // Used the File-constructor
    OutputStream out = new FileOutputStream(new File(dest, "file.zip"));
    
    // Transfer bytes from in to out
    byte[] buf = new byte[1024];
    int len;
    try {
        // A little more explicit
        while ( (len = in.read(buf, 0, buf.length)) != -1){
             out.write(buf, 0, len);
        }
    } finally {
        // Ensure the Streams are closed:
        in.close();
        out.close();
    }
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-03
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多