Guest User

Untitled

a guest
Jan 21st, 2018
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.94 KB | None | 0 0
  1. public class BinaryRename_sd {
  2. CountDownLatch countDownLatch;
  3.  
  4. public boolean rename(DocumentFile pickedDir, String path, String new_path, ProgressDialog dialog, Context context) throws IOException {
  5.  
  6. DocumentFile newFile = pickedDir.createFile("", new_path);
  7. for(int wait=0;wait<10;wait++){
  8. newFile=pickedDir.findFile(new_path);
  9. try {
  10. if(newFile!=null){
  11. wait=10;
  12. }else{
  13. countDownLatch = new CountDownLatch(1);
  14. new Thread(new Runnable() {
  15. @Override
  16. public void run() {
  17. try {
  18. Thread.sleep(500);
  19. } catch (InterruptedException e) {
  20. e.printStackTrace();
  21. }
  22. countDownLatch.countDown();
  23. }
  24. }).start();
  25.  
  26. countDownLatch.await(510, TimeUnit.MILLISECONDS);
  27. }
  28.  
  29. } catch (InterruptedException e) {
  30. e.printStackTrace();
  31. }
  32. }
  33. FileInputStream in = new FileInputStream(path);
  34. double fsize = in.available();
  35.  
  36. OutputStream fos=conntext.getContentResolver().openOutputStream(newFile.getUri());
  37.  
  38. try {
  39. int len_body_read;
  40. int buf =1024000*1;
  41. byte buf_body[]=new byte[buf];
  42. int loaded=50;
  43. double degree = 0.0;
  44.  
  45. while((len_body_read=in.read(buf_body))!=-1){
  46. try {
  47. fos.write(buf_body,0,len_body_read);
  48.  
  49. loaded=loaded+len_body_read;
  50. degree = (loaded/1000*100)/(fsize/1000*2);
  51.  
  52. if(dialog!=null){
  53. if((int) Math.round(degree)>95){
  54. dialog.setProgress(95);
  55. }else{
  56. dialog.setProgress((int) Math.round(degree)+50);
  57. }
  58. }
  59. } catch(Exception e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. fos.flush();
  65. fos.close();
  66.  
  67. }
  68. finally {
  69. in.close();
  70. }
  71.  
  72. File delFile = new File(path);
  73. delFile.delete();
  74. delFile=null;
  75.  
  76.  
  77. return true;
Add Comment
Please, Sign In to add comment