Advertisement
Guest User

Untitled

a guest
Mar 28th, 2017
49
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.12 KB | None | 0 0
  1. Intent intent = new Intent(Intent.ACTION_OPEN_DOCUMENT_TREE);
  2. startActivityForResult(intent, REQUEST_CODE_OPEN_DIRECTORY);
  3.  
  4. private String copyFile(String inputPath, String inputFile, Uri treeUri) {
  5. InputStream in = null;
  6. OutputStream out = null;
  7. String error = null;
  8. DocumentFile pickedDir = DocumentFile.fromTreeUri(getActivity(), treeUri);
  9. String extension = inputFile.substring(inputFile.lastIndexOf(".")+1,inputFile.length());
  10.  
  11. try {
  12. DocumentFile newFile = pickedDir.createFile("audio/"+extension, inputFile);
  13. out = getActivity().getContentResolver().openOutputStream(newFile.getUri());
  14. in = new FileInputStream(inputPath + inputFile);
  15.  
  16. byte[] buffer = new byte[1024];
  17. int read;
  18. while ((read = in.read(buffer)) != -1) {
  19. out.write(buffer, 0, read);
  20. }
  21. in.close();
  22. // write the output file (You have now copied the file)
  23. out.flush();
  24. out.close();
  25.  
  26. } catch (FileNotFoundException fnfe1) {
  27. error = fnfe1.getMessage();
  28. } catch (Exception e) {
  29. error = e.getMessage();
  30. }
  31. return error;
  32. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement