Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class BinaryRename_sd {
- CountDownLatch countDownLatch;
- public boolean rename(DocumentFile pickedDir, String path, String new_path, ProgressDialog dialog, Context context) throws IOException {
- DocumentFile newFile = pickedDir.createFile("", new_path);
- for(int wait=0;wait<10;wait++){
- newFile=pickedDir.findFile(new_path);
- try {
- if(newFile!=null){
- wait=10;
- }else{
- countDownLatch = new CountDownLatch(1);
- new Thread(new Runnable() {
- @Override
- public void run() {
- try {
- Thread.sleep(500);
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- countDownLatch.countDown();
- }
- }).start();
- countDownLatch.await(510, TimeUnit.MILLISECONDS);
- }
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- FileInputStream in = new FileInputStream(path);
- double fsize = in.available();
- OutputStream fos=conntext.getContentResolver().openOutputStream(newFile.getUri());
- try {
- int len_body_read;
- int buf =1024000*1;
- byte buf_body[]=new byte[buf];
- int loaded=50;
- double degree = 0.0;
- while((len_body_read=in.read(buf_body))!=-1){
- try {
- fos.write(buf_body,0,len_body_read);
- loaded=loaded+len_body_read;
- degree = (loaded/1000*100)/(fsize/1000*2);
- if(dialog!=null){
- if((int) Math.round(degree)>95){
- dialog.setProgress(95);
- }else{
- dialog.setProgress((int) Math.round(degree)+50);
- }
- }
- } catch(Exception e) {
- e.printStackTrace();
- }
- }
- fos.flush();
- fos.close();
- }
- finally {
- in.close();
- }
- File delFile = new File(path);
- delFile.delete();
- delFile=null;
- return true;
Add Comment
Please, Sign In to add comment