-
Notifications
You must be signed in to change notification settings - Fork 8
Expand file tree
/
Copy pathFileUtils.java
More file actions
162 lines (147 loc) · 5.74 KB
/
FileUtils.java
File metadata and controls
162 lines (147 loc) · 5.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
/*
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.wavemaker.cordova.plugin;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.OpenableColumns;
import android.util.Log;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
public class FileUtils {
/** TAG for log messages. */
static final String TAG = "FileUtils";
/**
* Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders.
*
* @param context The context.
* @param uri The Uri to query.
* @return The value of the _data column, which is typically a file path.
* @author paulburke
*/
private static String getDataColumn(Context context, Uri uri) {
Cursor cursor = null;
final String column = "_data";
final String[] projection = {
column
};
try {
cursor = context.getContentResolver().query(uri, projection, null, null,
null);
if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
/**
* Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.<br>
* <br>
* Callers should check whether the path is local before assuming it
* represents a local file.
*
* @param context The context.
* @param uri The Uri to query.
* @author paulburke
*/
public static String getPath(final Context context, final Uri uri) {
String path = null;
if ("content".equalsIgnoreCase(uri.getScheme())) {
path = getDataColumn(context, uri);
}
if (path == null && "file".equalsIgnoreCase(uri.getScheme())) {
path = uri.getPath();
}
if (path == null) {
File file = createFile(context, uri);
if (file != null) {
path = file.getAbsolutePath();
}
}
return path == null || path.startsWith("file://") ? path: "file://" + path;
}
private static String getName(Context context, Uri uri) {
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null, null);
String displayName = null;
if (cursor != null && cursor.moveToFirst()) {
displayName = cursor.getString(cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME));
}
if(cursor != null) {
cursor.close();
}
return displayName != null ? displayName : uri.getLastPathSegment();
}
private static File createFile(Context context, Uri uri) {
try {
String name = getName(context, uri);
File file = new File(context.getExternalCacheDir(), name);
if (file.exists()) {
int ext = name.lastIndexOf('.');
String prefix = name, suffix = "";
if(ext > 0) {
prefix = name.substring(0, ext);
suffix = name.substring(ext);
}
name = prefix + "_" + System.currentTimeMillis() + suffix;
file = new File(context.getExternalCacheDir(), name);
}
InputStream inputStream = context.getContentResolver().openInputStream(uri);
try (FileOutputStream outputStream = new FileOutputStream(file)) {
int read;
byte[] bytes = new byte[1024];
while ((read = inputStream.read(bytes)) != -1) {
outputStream.write(bytes, 0, read);
}
outputStream.flush();
}
long originalDate = getFileModifiedDate(context, uri);
file.setLastModified(originalDate);
return file;
} catch (Exception e) {
Log.e(TAG, "Exception occured", e);
}
return null;
}
private static Long getFileModifiedDate(Context context, Uri uri) {
Cursor cursor = context.getContentResolver().query(uri, null, null, null, null, null);
if (cursor != null && cursor.moveToFirst()) {
int columnIndex = cursor.getColumnIndex(MediaStore.MediaColumns.DATE_MODIFIED);
if (columnIndex == -1) {
columnIndex = cursor.getColumnIndex(DocumentsContract.Document.COLUMN_LAST_MODIFIED);
}
if (columnIndex != -1) {
String dateStr = cursor.getString(columnIndex);
if (dateStr != null) {
return Long.parseLong(dateStr);
}
}
}
return null;
}
}