繁体   English   中英

如何在 android 的 data/files 文件夹中保存非 txt 文件

[英]How to save a non txt file in data/files folder in android

一种将非文本文件保存到 /data/files 文件夹的方法。 如果文件资源来自资产文件夹。

这应该这样做。

private void writeToSDCard() {
    try {
        File root = Environment.getExternalStorageDirectory();

        if (root.canWrite()){
            InputStream from = myContext.getResources().openRawResource(rID);
            File dir = new java.io.File (root, "pdf");
            dir.mkdir();
            File writeTo = new File(root, "pdf/" + attachmentName);
            FileOutputStream  to = new FileOutputStream(writeTo);

            byte[] buffer = new byte[4096];
            int bytesRead;

            while ((bytesRead = from.read(buffer)) != -1)
                to.write(buffer, 0, bytesRead); // write
            to.close();             
            from.close();
        } else {
            Log.d(TAG, "Unable to access SD card.");
        }
    } catch (Exception e) {
        Log.d(TAG, "writeToSDCard: " + e.getMessage());
    }
}       
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM