4
4
import android .net .Uri ;
5
5
6
6
import java .io .File ;
7
+ import java .io .FileNotFoundException ;
8
+ import java .io .FileOutputStream ;
9
+ import java .io .IOException ;
10
+ import java .io .InputStream ;
11
+ import java .io .OutputStream ;
7
12
import java .util .Random ;
8
13
9
14
import timber .log .Timber ;
@@ -29,9 +34,9 @@ public static Uri saveFileBeingUploadedTemporarily(Context context, Uri URIfromC
29
34
// TODO add exceptions for Google Drive URİ is needed
30
35
Uri result = null ;
31
36
32
- if (FileUtils . checkIfDirectoryExists (TEMP_EXTERNAL_DIRECTORY )) {
37
+ if (checkIfDirectoryExists (TEMP_EXTERNAL_DIRECTORY )) {
33
38
String destinationFilename = decideTempDestinationFileName ();
34
- result = FileUtils . saveFileFromURI (context , URIfromContentProvider , destinationFilename );
39
+ result = saveFileFromURI (context , URIfromContentProvider , destinationFilename );
35
40
} else { // If directory doesn't exist, create it and recursive call current method to check again
36
41
37
42
File file = new File (TEMP_EXTERNAL_DIRECTORY );
@@ -53,29 +58,25 @@ public static void removeTemporaryFile(Uri tempFileUri) {
53
58
//TODO: do I have to notify file system about deletion?
54
59
File tempFile = new File (tempFileUri .getPath ());
55
60
if (tempFile .exists ()) {
56
- boolean isDeleted = tempFile .delete ();
61
+ boolean isDeleted = tempFile .delete ();
57
62
Timber .e ("removeTemporaryFile() parameters: URI tempFileUri %s, deleted status %b" , tempFileUri , isDeleted );
58
63
}
59
64
}
60
65
61
66
private static String decideTempDestinationFileName () {
62
67
int i = 0 ;
63
- while (true ) {
64
- if (new File (TEMP_EXTERNAL_DIRECTORY +File .separatorChar +i +"_tmp" ).exists ()) {
65
- // This file is in use, try enother file
66
- i ++;
67
- } else {
68
- // Use time stamp for file name, so that two temporary file never has same file name
69
- // to prevent previous file reference bug
70
- Long tsLong = System .currentTimeMillis ()/1000 ;
71
- String ts = tsLong .toString ();
72
-
73
- // For multiple uploads, time randomisation should be combined with another random
74
- // parameter, since they created at same time
75
- int multipleUploadRandomParameter = new Random ().nextInt (100 );
76
- return TEMP_EXTERNAL_DIRECTORY +File .separatorChar +ts +multipleUploadRandomParameter +"_tmp" ;
77
- }
68
+ while (new File (TEMP_EXTERNAL_DIRECTORY + File .separatorChar + i + "_tmp" ).exists ()) {
69
+ i ++;
78
70
}
71
+
72
+ // Use time stamp for file name, so that two temporary file never has same file name
73
+ // to prevent previous file reference bug
74
+ String timeStamp = String .valueOf (System .currentTimeMillis () / 1000 );
75
+
76
+ // For multiple uploads, time randomisation should be combined with another random
77
+ // parameter, since they created at same time
78
+ int multipleUploadRandomParameter = new Random ().nextInt (100 );
79
+ return TEMP_EXTERNAL_DIRECTORY + File .separatorChar + timeStamp + multipleUploadRandomParameter + "_tmp" ;
79
80
}
80
81
81
82
public static void emptyTemporaryDirectory () {
@@ -91,4 +92,53 @@ public static void emptyTemporaryDirectory() {
91
92
}
92
93
}
93
94
}
95
+
96
+ /**
97
+ * Saves file from source URI to destination.
98
+ * @param sourceUri Uri which points to file to be saved
99
+ * @param destinationFilename where file will be located at
100
+ * @return Uri points to file saved
101
+ */
102
+ private static Uri saveFileFromURI (Context context , Uri sourceUri , String destinationFilename ) {
103
+ File file = new File (destinationFilename );
104
+ if (file .exists ()) {
105
+ file .delete ();
106
+ }
107
+
108
+ InputStream in = null ;
109
+ OutputStream out = null ;
110
+ try {
111
+ in = context .getContentResolver ().openInputStream (sourceUri );
112
+ out = new FileOutputStream (new File (destinationFilename ));
113
+
114
+ byte [] buf = new byte [1024 ];
115
+ int len ;
116
+ while ((len =in .read (buf )) > 0 ) {
117
+ out .write (buf , 0 , len );
118
+ }
119
+ } catch (FileNotFoundException e ) {
120
+ e .printStackTrace ();
121
+ } catch (IOException e ) {
122
+ e .printStackTrace ();
123
+ } finally {
124
+ try {
125
+ if (out != null ) out .close ();
126
+ if (in != null ) in .close ();
127
+ } catch (IOException e ) {
128
+ e .printStackTrace ();
129
+ }
130
+ }
131
+
132
+ return Uri .parse ("file://" + destinationFilename );
133
+ }
134
+
135
+ /**
136
+ * Checks if directory exists
137
+ * @param pathToCheck path of directory to check
138
+ * @return true if directory exists, false otherwise
139
+ */
140
+ private static boolean checkIfDirectoryExists (String pathToCheck ) {
141
+ File dir = new File (pathToCheck );
142
+ return dir .exists () && dir .isDirectory ();
143
+ }
94
144
}
0 commit comments