| 582 | } |
| 583 | |
| 584 | private void writeFile( |
| 585 | final String filename, |
| 586 | final String content, |
| 587 | final Boolean isArrayBuffer, |
| 588 | final CallbackContext callback |
| 589 | ) { |
| 590 | final Context context = this.context; |
| 591 | |
| 592 | cordova |
| 593 | .getThreadPool() |
| 594 | .execute( |
| 595 | new Runnable() { |
| 596 | public void run() { |
| 597 | try { |
| 598 | DocumentFile file = getFile(filename); |
| 599 | if (file == null) { |
| 600 | callback.error("File not found."); |
| 601 | return; |
| 602 | } |
| 603 | if (canWrite(file.getUri())) { |
| 604 | OutputStream op = context |
| 605 | .getContentResolver() |
| 606 | .openOutputStream(file.getUri(), "rwt"); |
| 607 | |
| 608 | PrintWriter pw = new PrintWriter(op, true); |
| 609 | |
| 610 | if (isArrayBuffer) { |
| 611 | byte[] bytes = Base64.decode(content, Base64.DEFAULT); |
| 612 | // write bytes to file |
| 613 | op.write(bytes); |
| 614 | } else { |
| 615 | pw.print(content); |
| 616 | } |
| 617 | |
| 618 | pw.flush(); |
| 619 | pw.close(); |
| 620 | op.close(); |
| 621 | callback.success("OK"); |
| 622 | } else { |
| 623 | callback.error("No write permission"); |
| 624 | } |
| 625 | } catch (Exception e) { |
| 626 | callback.error(e.toString()); |
| 627 | } |
| 628 | } |
| 629 | } |
| 630 | ); |
| 631 | } |
| 632 | |
| 633 | private void createDir(String parent, String name, CallbackContext callback) { |
| 634 | create(parent, name, Document.MIME_TYPE_DIR, callback); |