开发者

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

开发者 https://www.devze.com 2023-03-16 22:40 出处:网络
A 开发者_运维问答way to save a non text file to /data/files folder. If file resource is from assets folder.this should do it.

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

精彩评论

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

关注公众号