开发者

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

A 开发者_运维问答way to save a non text file to /data/files folder. If file resource is from assets folder.


this should do it.

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

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜