community-cordova-plugin-filepath 1.7.0 → 1.7.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -1,512 +1,518 @@
1
- package com.hiddentao.cordova.filepath;
2
-
3
- import android.text.TextUtils;
4
- import android.Manifest;
5
- import android.content.ContentUris;
6
- import android.content.Context;
7
- import android.content.pm.PackageManager;
8
- import android.net.Uri;
9
- import android.provider.OpenableColumns;
10
- import android.util.Log;
11
- import android.database.Cursor;
12
- import android.os.Build;
13
- import android.os.Environment;
14
- import android.provider.DocumentsContract;
15
- import android.provider.MediaStore;
16
- import android.support.v4.app.ActivityCompat;
17
-
18
- import org.apache.cordova.CallbackContext;
19
- import org.apache.cordova.CordovaInterface;
20
- import org.apache.cordova.CordovaPlugin;
21
- import org.apache.cordova.CordovaWebView;
22
- import org.apache.cordova.PermissionHelper;
23
- import org.json.JSONArray;
24
- import org.json.JSONObject;
25
- import org.json.JSONException;
26
-
27
- import java.io.FileOutputStream;
28
- import java.io.InputStream;
29
- import java.util.List;
30
- import java.io.File;
31
-
32
- import java.io.IOException;
33
- import java.io.OutputStream;
34
-
35
- import android.content.pm.ApplicationInfo;
36
-
37
-
38
- import org.apache.commons.io.IOUtils;
39
-
40
- public class FilePath extends CordovaPlugin {
41
-
42
- private static final String TAG = "[FilePath plugin]: ";
43
-
44
- private static final int INVALID_ACTION_ERROR_CODE = -1;
45
-
46
- private static final int GET_PATH_ERROR_CODE = 0;
47
- private static final String GET_PATH_ERROR_ID = null;
48
-
49
- private static final int GET_CLOUD_PATH_ERROR_CODE = 1;
50
- private static final String GET_CLOUD_PATH_ERROR_ID = "cloud";
51
-
52
- private static final int RC_READ_EXTERNAL_STORAGE = 5;
53
-
54
- private static CallbackContext callback;
55
- private static String uriStr;
56
-
57
- public static final int READ_REQ_CODE = 0;
58
-
59
- public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE;
60
-
61
- protected void getReadPermission(int requestCode) {
62
- PermissionHelper.requestPermission(this, requestCode, READ);
63
- }
64
-
65
- public void initialize(CordovaInterface cordova, final CordovaWebView webView) {
66
- super.initialize(cordova, webView);
67
- }
68
-
69
- /**
70
- * Executes the request and returns PluginResult.
71
- *
72
- * @param action The action to execute.
73
- * @param args JSONArry of arguments for the plugin.
74
- * @param callbackContext The callback context through which to return stuff to caller.
75
- * @return A PluginResult object with a status and message.
76
- */
77
- @Override
78
- public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
79
- this.callback = callbackContext;
80
- this.uriStr = args.getString(0);
81
- if (action.equals("resolveNativePath")) {
82
- if (PermissionHelper.hasPermission(this, READ)) {
83
- resolveNativePath();
84
- }
85
- else {
86
- getReadPermission(READ_REQ_CODE);
87
- }
88
-
89
- return true;
90
- }
91
- else {
92
- JSONObject resultObj = new JSONObject();
93
-
94
- resultObj.put("code", INVALID_ACTION_ERROR_CODE);
95
- resultObj.put("message", "Invalid action.");
96
-
97
- callbackContext.error(resultObj);
98
- }
99
-
100
- return false;
101
- }
102
-
103
- public void resolveNativePath() throws JSONException {
104
- JSONObject resultObj = new JSONObject();
105
- /* content:///... */
106
- Uri pvUrl = Uri.parse(this.uriStr);
107
- Log.d(TAG, "URI: " + this.uriStr);
108
-
109
- Context appContext = this.cordova.getActivity().getApplicationContext();
110
-
111
- //String filePath = getPath(appContext, pvUrl);
112
- String filePath = getFilePathFromURI(appContext, pvUrl);
113
- //check result; send error/success callback
114
- if (filePath == GET_PATH_ERROR_ID) {
115
- resultObj.put("code", GET_PATH_ERROR_CODE);
116
- resultObj.put("message", "Unable to resolve filesystem path.");
117
-
118
- this.callback.error(resultObj);
119
- }
120
- else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) {
121
- resultObj.put("code", GET_CLOUD_PATH_ERROR_CODE);
122
- resultObj.put("message", "Files from cloud cannot be resolved to filesystem, download is required.");
123
-
124
- this.callback.error(resultObj);
125
- }
126
- else {
127
- Log.d(TAG, "Filepath: " + filePath);
128
-
129
- this.callback.success("file://" + filePath);
130
- }
131
- }
132
-
133
- public static String getFilePathFromURI(Context context, Uri contentUri) {
134
- //copy file and send new file path
135
- String fileName = getFileName(contentUri);
136
- if (!TextUtils.isEmpty(fileName)) {
137
- File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath() +
138
- File.separator + getApplicationName(context));
139
-
140
- boolean success = true;
141
-
142
- if (!folder.exists()) {
143
- success = folder.mkdirs();
144
- }
145
-
146
- if (success) {
147
- File copyFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath() +
148
- File.separator + getApplicationName(context) + File.separator + fileName);
149
- copy(context, contentUri, copyFile);
150
- return copyFile.getAbsolutePath();
151
- }
152
-
153
- }
154
- return null;
155
- }
156
-
157
- public static String getFileName(Uri uri) {
158
- if (uri == null) return null;
159
- String fileName = null;
160
- String path = uri.getPath();
161
- int cut = path.lastIndexOf('/');
162
- if (cut != -1) {
163
- fileName = path.substring(cut + 1);
164
- }
165
- return fileName;
166
- }
167
-
168
- public static void copy(Context context, Uri srcUri, File dstFile) {
169
- try {
170
- InputStream inputStream = context.getContentResolver().openInputStream(srcUri);
171
- if (inputStream == null) return;
172
- OutputStream outputStream = new FileOutputStream(dstFile);
173
- IOUtils.copy(inputStream, outputStream);
174
- inputStream.close();
175
- outputStream.close();
176
- } catch (IOException e) {
177
- e.printStackTrace();
178
- }
179
- }
180
-
181
- public static String getApplicationName(Context context) {
182
- ApplicationInfo applicationInfo = context.getApplicationInfo();
183
- int stringId = applicationInfo.labelRes;
184
- return stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
185
- }
186
-
187
- public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
188
- for (int r:grantResults) {
189
- if (r == PackageManager.PERMISSION_DENIED) {
190
- JSONObject resultObj = new JSONObject();
191
- resultObj.put("code", 3);
192
- resultObj.put("message", "Filesystem permission was denied.");
193
-
194
- this.callback.error(resultObj);
195
- return;
196
- }
197
- }
198
-
199
- if (requestCode == READ_REQ_CODE) {
200
- resolveNativePath();
201
- }
202
- }
203
-
204
-
205
- /**
206
- * @param uri The Uri to check.
207
- * @return Whether the Uri authority is ExternalStorageProvider.
208
- */
209
- private static boolean isExternalStorageDocument(Uri uri) {
210
- return "com.android.externalstorage.documents".equals(uri.getAuthority());
211
- }
212
-
213
- /**
214
- * @param uri The Uri to check.
215
- * @return Whether the Uri authority is DownloadsProvider.
216
- */
217
- private static boolean isDownloadsDocument(Uri uri) {
218
- return "com.android.providers.downloads.documents".equals(uri.getAuthority());
219
- }
220
-
221
- /**
222
- * @param uri The Uri to check.
223
- * @return Whether the Uri authority is MediaProvider.
224
- */
225
- private static boolean isMediaDocument(Uri uri) {
226
- return "com.android.providers.media.documents".equals(uri.getAuthority());
227
- }
228
-
229
- /**
230
- * @param uri The Uri to check.
231
- * @return Whether the Uri authority is Google Photos.
232
- */
233
- private static boolean isGooglePhotosUri(Uri uri) {
234
- return ("com.google.android.apps.photos.content".equals(uri.getAuthority())
235
- || "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority()));
236
- }
237
-
238
- /**
239
- * @param uri The Uri to check.
240
- * @return Whether the Uri authority is Google Drive.
241
- */
242
- private static boolean isGoogleDriveUri(Uri uri) {
243
- return "com.google.android.apps.docs.storage".equals(uri.getAuthority()) || "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
244
- }
245
-
246
- /**
247
- * Get the value of the data column for this Uri. This is useful for
248
- * MediaStore Uris, and other file-based ContentProviders.
249
- *
250
- * @param context The context.
251
- * @param uri The Uri to query.
252
- * @param selection (Optional) Filter used in the query.
253
- * @param selectionArgs (Optional) Selection arguments used in the query.
254
- * @return The value of the _data column, which is typically a file path.
255
- */
256
- private static String getDataColumn(Context context, Uri uri, String selection,
257
- String[] selectionArgs) {
258
-
259
- Cursor cursor = null;
260
- final String column = "_data";
261
- final String[] projection = {
262
- column
263
- };
264
-
265
- try {
266
- cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
267
- null);
268
- if (cursor != null && cursor.moveToFirst()) {
269
- final int column_index = cursor.getColumnIndexOrThrow(column);
270
- return cursor.getString(column_index);
271
- }
272
- } finally {
273
- if (cursor != null)
274
- cursor.close();
275
- }
276
- return null;
277
- }
278
-
279
- /**
280
- * Get content:// from segment list
281
- * In the new Uri Authority of Google Photos, the last segment is not the content:// anymore
282
- * So let's iterate through all segments and find the content uri!
283
- *
284
- * @param segments The list of segment
285
- */
286
- private static String getContentFromSegments(List<String> segments) {
287
- String contentPath = "";
288
-
289
- for(String item : segments) {
290
- if (item.startsWith("content://")) {
291
- contentPath = item;
292
- break;
293
- }
294
- }
295
-
296
- return contentPath;
297
- }
298
-
299
- /**
300
- * Check if a file exists on device
301
- *
302
- * @param filePath The absolute file path
303
- */
304
- private static boolean fileExists(String filePath) {
305
- File file = new File(filePath);
306
-
307
- return file.exists();
308
- }
309
-
310
- /**
311
- * Get full file path from external storage
312
- *
313
- * @param pathData The storage type and the relative path
314
- */
315
- private static String getPathFromExtSD(String[] pathData) {
316
- final String type = pathData[0];
317
- final String relativePath = "/" + pathData[1];
318
- String fullPath = "";
319
-
320
- // on my Sony devices (4.4.4 & 5.1.1), `type` is a dynamic string
321
- // something like "71F8-2C0A", some kind of unique id per storage
322
- // don't know any API that can get the root path of that storage based on its id.
323
- //
324
- // so no "primary" type, but let the check here for other devices
325
- if ("primary".equalsIgnoreCase(type)) {
326
- fullPath = Environment.getExternalStorageDirectory() + relativePath;
327
- if (fileExists(fullPath)) {
328
- return fullPath;
329
- }
330
- }
331
-
332
- // Environment.isExternalStorageRemovable() is `true` for external and internal storage
333
- // so we cannot relay on it.
334
- //
335
- // instead, for each possible path, check if file exists
336
- // we'll start with secondary storage as this could be our (physically) removable sd card
337
- fullPath = System.getenv("SECONDARY_STORAGE") + relativePath;
338
- if (fileExists(fullPath)) {
339
- return fullPath;
340
- }
341
-
342
- fullPath = System.getenv("EXTERNAL_STORAGE") + relativePath;
343
- if (fileExists(fullPath)) {
344
- return fullPath;
345
- }
346
-
347
- return fullPath;
348
- }
349
-
350
- /**
351
- * Get a file path from a Uri. This will get the the path for Storage Access
352
- * Framework Documents, as well as the _data field for the MediaStore and
353
- * other file-based ContentProviders.<br>
354
- * <br>
355
- * Callers should check whether the path is local before assuming it
356
- * represents a local file.
357
- *
358
- * @param context The context.
359
- * @param uri The Uri to query.
360
- */
361
- private static String getPath(final Context context, final Uri uri) {
362
-
363
- Log.d(TAG, "File - " +
364
- "Authority: " + uri.getAuthority() +
365
- ", Fragment: " + uri.getFragment() +
366
- ", Port: " + uri.getPort() +
367
- ", Query: " + uri.getQuery() +
368
- ", Scheme: " + uri.getScheme() +
369
- ", Host: " + uri.getHost() +
370
- ", Segments: " + uri.getPathSegments().toString()
371
- );
372
-
373
- final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
374
-
375
- // DocumentProvider
376
- if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
377
- // ExternalStorageProvider
378
- if (isExternalStorageDocument(uri)) {
379
- final String docId = DocumentsContract.getDocumentId(uri);
380
- final String[] split = docId.split(":");
381
- final String type = split[0];
382
-
383
- String fullPath = getPathFromExtSD(split);
384
- if (fullPath != "") {
385
- return fullPath;
386
- }
387
- else {
388
- return null;
389
- }
390
- }
391
- // DownloadsProvider
392
- else if (isDownloadsDocument(uri)) {
393
- // thanks to https://github.com/hiddentao/cordova-plugin-filepath/issues/34#issuecomment-430129959
394
- Cursor cursor = null;
395
- try {
396
- cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
397
- if (cursor != null && cursor.moveToFirst()) {
398
- String fileName = cursor.getString(0);
399
- String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
400
- if (!TextUtils.isEmpty(path)) {
401
- return path;
402
- }
403
- }
404
- } finally {
405
- if (cursor != null)
406
- cursor.close();
407
- }
408
- //
409
- final String id = DocumentsContract.getDocumentId(uri);
410
- try {
411
- final Uri contentUri = ContentUris.withAppendedId(
412
- Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
413
-
414
- return getDataColumn(context, contentUri, null, null);
415
- } catch(NumberFormatException e) {
416
- //In Android 8 and Android P the id is not a number
417
- return uri.getPath().replaceFirst("^/document/raw:", "").replaceFirst("^raw:", "");
418
- }
419
- }
420
- // MediaProvider
421
- else if (isMediaDocument(uri)) {
422
- final String docId = DocumentsContract.getDocumentId(uri);
423
- final String[] split = docId.split(":");
424
- final String type = split[0];
425
-
426
- Uri contentUri = null;
427
- if ("image".equals(type)) {
428
- contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
429
- } else if ("video".equals(type)) {
430
- contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
431
- } else if ("audio".equals(type)) {
432
- contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
433
- }
434
-
435
- final String selection = "_id=?";
436
- final String[] selectionArgs = new String[] {
437
- split[1]
438
- };
439
-
440
- return getDataColumn(context, contentUri, selection, selectionArgs);
441
- }
442
- else if(isGoogleDriveUri(uri)){
443
- return getDriveFilePath(uri,context);
444
- }
445
- }
446
- // MediaStore (and general)
447
- else if ("content".equalsIgnoreCase(uri.getScheme())) {
448
-
449
- // Return the remote address
450
- if (isGooglePhotosUri(uri)) {
451
- String contentPath = getContentFromSegments(uri.getPathSegments());
452
- if (contentPath != "") {
453
- return getPath(context, Uri.parse(contentPath));
454
- }
455
- else {
456
- return null;
457
- }
458
- }
459
-
460
- if(isGoogleDriveUri(uri)){
461
- return getDriveFilePath(uri,context);
462
- }
463
-
464
- return getDataColumn(context, uri, null, null);
465
- }
466
- // File
467
- else if ("file".equalsIgnoreCase(uri.getScheme())) {
468
- return uri.getPath();
469
- }
470
-
471
- return null;
472
- }
473
-
474
- private static String getDriveFilePath(Uri uri,Context context){
475
- Uri returnUri =uri;
476
- Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
477
- /*
478
- * Get the column indexes of the data in the Cursor,
479
- * * move to the first row in the Cursor, get the data,
480
- * * and display it.
481
- * */
482
- int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
483
- int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
484
- returnCursor.moveToFirst();
485
- String name = (returnCursor.getString(nameIndex));
486
- String size = (Long.toString(returnCursor.getLong(sizeIndex)));
487
- File file = new File(context.getCacheDir(),name);
488
- try {
489
- InputStream inputStream = context.getContentResolver().openInputStream(uri);
490
- FileOutputStream outputStream = new FileOutputStream(file);
491
- int read = 0;
492
- int maxBufferSize = 1 * 1024 * 1024;
493
- int bytesAvailable = inputStream.available();
494
-
495
- //int bufferSize = 1024;
496
- int bufferSize = Math.min(bytesAvailable, maxBufferSize);
497
-
498
- final byte[] buffers = new byte[bufferSize];
499
- while ((read = inputStream.read(buffers)) != -1) {
500
- outputStream.write(buffers, 0, read);
501
- }
502
- Log.e("File Size","Size " + file.length());
503
- inputStream.close();
504
- outputStream.close();
505
- Log.e("File Path","Path " + file.getPath());
506
- Log.e("File Size","Size " + file.length());
507
- }catch (Exception e){
508
- Log.e("Exception",e.getMessage());
509
- }
510
- return file.getPath();
511
- }
512
- }
1
+ package com.hiddentao.cordova.filepath;
2
+
3
+ import android.text.TextUtils;
4
+ import android.Manifest;
5
+ import android.content.ContentUris;
6
+ import android.content.Context;
7
+ import android.content.pm.PackageManager;
8
+ import android.net.Uri;
9
+ import android.provider.OpenableColumns;
10
+ import android.util.Log;
11
+ import android.database.Cursor;
12
+ import android.os.Build;
13
+ import android.os.Environment;
14
+ import android.provider.DocumentsContract;
15
+ import android.provider.MediaStore;
16
+ import android.support.v4.app.ActivityCompat;
17
+
18
+ import org.apache.cordova.CallbackContext;
19
+ import org.apache.cordova.CordovaInterface;
20
+ import org.apache.cordova.CordovaPlugin;
21
+ import org.apache.cordova.CordovaWebView;
22
+ import org.apache.cordova.PermissionHelper;
23
+ import org.json.JSONArray;
24
+ import org.json.JSONObject;
25
+ import org.json.JSONException;
26
+
27
+ import java.io.FileOutputStream;
28
+ import java.io.InputStream;
29
+ import java.util.List;
30
+ import java.io.File;
31
+
32
+ import java.io.IOException;
33
+ import java.io.OutputStream;
34
+
35
+ import android.content.pm.ApplicationInfo;
36
+
37
+
38
+ import org.apache.commons.io.IOUtils;
39
+
40
+ public class FilePath extends CordovaPlugin {
41
+
42
+ private static final String TAG = "[FilePath plugin]: ";
43
+
44
+ private static final int INVALID_ACTION_ERROR_CODE = -1;
45
+
46
+ private static final int GET_PATH_ERROR_CODE = 0;
47
+ private static final String GET_PATH_ERROR_ID = null;
48
+
49
+ private static final int GET_CLOUD_PATH_ERROR_CODE = 1;
50
+ private static final String GET_CLOUD_PATH_ERROR_ID = "cloud";
51
+
52
+ private static final int RC_READ_EXTERNAL_STORAGE = 5;
53
+
54
+ private static CallbackContext callback;
55
+ private static String uriStr;
56
+
57
+ public static final int READ_REQ_CODE = 0;
58
+
59
+ public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE;
60
+
61
+ protected void getReadPermission(int requestCode) {
62
+ PermissionHelper.requestPermission(this, requestCode, READ);
63
+ }
64
+
65
+ public void initialize(CordovaInterface cordova, final CordovaWebView webView) {
66
+ super.initialize(cordova, webView);
67
+ }
68
+
69
+ /**
70
+ * Executes the request and returns PluginResult.
71
+ *
72
+ * @param action The action to execute.
73
+ * @param args JSONArry of arguments for the plugin.
74
+ * @param callbackContext The callback context through which to return stuff to caller.
75
+ * @return A PluginResult object with a status and message.
76
+ */
77
+ @Override
78
+ public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
79
+ this.callback = callbackContext;
80
+ this.uriStr = args.getString(0);
81
+ if (action.equals("resolveNativePath")) {
82
+
83
+ if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
84
+ //need to use Android permissions plugin to request Android 13 permissions (READ_MEDIA_IMAGES,READ_MEDIA_VIDEO)
85
+ resolveNativePath();
86
+ return true;
87
+ } else{
88
+ if (PermissionHelper.hasPermission(this, READ)) {
89
+ resolveNativePath();
90
+ }
91
+ else {
92
+ getReadPermission(READ_REQ_CODE);
93
+ }
94
+ return true;
95
+ }
96
+ }
97
+ else {
98
+ JSONObject resultObj = new JSONObject();
99
+
100
+ resultObj.put("code", INVALID_ACTION_ERROR_CODE);
101
+ resultObj.put("message", "Invalid action.");
102
+
103
+ callbackContext.error(resultObj);
104
+ }
105
+
106
+ return false;
107
+ }
108
+
109
+ public void resolveNativePath() throws JSONException {
110
+ JSONObject resultObj = new JSONObject();
111
+ /* content:///... */
112
+ Uri pvUrl = Uri.parse(this.uriStr);
113
+ Log.d(TAG, "URI: " + this.uriStr);
114
+
115
+ Context appContext = this.cordova.getActivity().getApplicationContext();
116
+
117
+ //String filePath = getPath(appContext, pvUrl);
118
+ String filePath = getFilePathFromURI(appContext, pvUrl);
119
+ //check result; send error/success callback
120
+ if (filePath == GET_PATH_ERROR_ID) {
121
+ resultObj.put("code", GET_PATH_ERROR_CODE);
122
+ resultObj.put("message", "Unable to resolve filesystem path.");
123
+
124
+ this.callback.error(resultObj);
125
+ }
126
+ else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) {
127
+ resultObj.put("code", GET_CLOUD_PATH_ERROR_CODE);
128
+ resultObj.put("message", "Files from cloud cannot be resolved to filesystem, download is required.");
129
+
130
+ this.callback.error(resultObj);
131
+ }
132
+ else {
133
+ Log.d(TAG, "Filepath: " + filePath);
134
+
135
+ this.callback.success("file://" + filePath);
136
+ }
137
+ }
138
+
139
+ public static String getFilePathFromURI(Context context, Uri contentUri) {
140
+ //copy file and send new file path
141
+ String fileName = getFileName(contentUri);
142
+ if (!TextUtils.isEmpty(fileName)) {
143
+ File folder = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath() +
144
+ File.separator + getApplicationName(context));
145
+
146
+ boolean success = true;
147
+
148
+ if (!folder.exists()) {
149
+ success = folder.mkdirs();
150
+ }
151
+
152
+ if (success) {
153
+ File copyFile = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS).getPath() +
154
+ File.separator + getApplicationName(context) + File.separator + fileName);
155
+ copy(context, contentUri, copyFile);
156
+ return copyFile.getAbsolutePath();
157
+ }
158
+
159
+ }
160
+ return null;
161
+ }
162
+
163
+ public static String getFileName(Uri uri) {
164
+ if (uri == null) return null;
165
+ String fileName = null;
166
+ String path = uri.getPath();
167
+ int cut = path.lastIndexOf('/');
168
+ if (cut != -1) {
169
+ fileName = path.substring(cut + 1);
170
+ }
171
+ return fileName;
172
+ }
173
+
174
+ public static void copy(Context context, Uri srcUri, File dstFile) {
175
+ try {
176
+ InputStream inputStream = context.getContentResolver().openInputStream(srcUri);
177
+ if (inputStream == null) return;
178
+ OutputStream outputStream = new FileOutputStream(dstFile);
179
+ IOUtils.copy(inputStream, outputStream);
180
+ inputStream.close();
181
+ outputStream.close();
182
+ } catch (IOException e) {
183
+ e.printStackTrace();
184
+ }
185
+ }
186
+
187
+ public static String getApplicationName(Context context) {
188
+ ApplicationInfo applicationInfo = context.getApplicationInfo();
189
+ int stringId = applicationInfo.labelRes;
190
+ return stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId);
191
+ }
192
+
193
+ public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
194
+ for (int r:grantResults) {
195
+ if (r == PackageManager.PERMISSION_DENIED) {
196
+ JSONObject resultObj = new JSONObject();
197
+ resultObj.put("code", 3);
198
+ resultObj.put("message", "Filesystem permission was denied.");
199
+
200
+ this.callback.error(resultObj);
201
+ return;
202
+ }
203
+ }
204
+
205
+ if (requestCode == READ_REQ_CODE) {
206
+ resolveNativePath();
207
+ }
208
+ }
209
+
210
+
211
+ /**
212
+ * @param uri The Uri to check.
213
+ * @return Whether the Uri authority is ExternalStorageProvider.
214
+ */
215
+ private static boolean isExternalStorageDocument(Uri uri) {
216
+ return "com.android.externalstorage.documents".equals(uri.getAuthority());
217
+ }
218
+
219
+ /**
220
+ * @param uri The Uri to check.
221
+ * @return Whether the Uri authority is DownloadsProvider.
222
+ */
223
+ private static boolean isDownloadsDocument(Uri uri) {
224
+ return "com.android.providers.downloads.documents".equals(uri.getAuthority());
225
+ }
226
+
227
+ /**
228
+ * @param uri The Uri to check.
229
+ * @return Whether the Uri authority is MediaProvider.
230
+ */
231
+ private static boolean isMediaDocument(Uri uri) {
232
+ return "com.android.providers.media.documents".equals(uri.getAuthority());
233
+ }
234
+
235
+ /**
236
+ * @param uri The Uri to check.
237
+ * @return Whether the Uri authority is Google Photos.
238
+ */
239
+ private static boolean isGooglePhotosUri(Uri uri) {
240
+ return ("com.google.android.apps.photos.content".equals(uri.getAuthority())
241
+ || "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority()));
242
+ }
243
+
244
+ /**
245
+ * @param uri The Uri to check.
246
+ * @return Whether the Uri authority is Google Drive.
247
+ */
248
+ private static boolean isGoogleDriveUri(Uri uri) {
249
+ return "com.google.android.apps.docs.storage".equals(uri.getAuthority()) || "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
250
+ }
251
+
252
+ /**
253
+ * Get the value of the data column for this Uri. This is useful for
254
+ * MediaStore Uris, and other file-based ContentProviders.
255
+ *
256
+ * @param context The context.
257
+ * @param uri The Uri to query.
258
+ * @param selection (Optional) Filter used in the query.
259
+ * @param selectionArgs (Optional) Selection arguments used in the query.
260
+ * @return The value of the _data column, which is typically a file path.
261
+ */
262
+ private static String getDataColumn(Context context, Uri uri, String selection,
263
+ String[] selectionArgs) {
264
+
265
+ Cursor cursor = null;
266
+ final String column = "_data";
267
+ final String[] projection = {
268
+ column
269
+ };
270
+
271
+ try {
272
+ cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
273
+ null);
274
+ if (cursor != null && cursor.moveToFirst()) {
275
+ final int column_index = cursor.getColumnIndexOrThrow(column);
276
+ return cursor.getString(column_index);
277
+ }
278
+ } finally {
279
+ if (cursor != null)
280
+ cursor.close();
281
+ }
282
+ return null;
283
+ }
284
+
285
+ /**
286
+ * Get content:// from segment list
287
+ * In the new Uri Authority of Google Photos, the last segment is not the content:// anymore
288
+ * So let's iterate through all segments and find the content uri!
289
+ *
290
+ * @param segments The list of segment
291
+ */
292
+ private static String getContentFromSegments(List<String> segments) {
293
+ String contentPath = "";
294
+
295
+ for(String item : segments) {
296
+ if (item.startsWith("content://")) {
297
+ contentPath = item;
298
+ break;
299
+ }
300
+ }
301
+
302
+ return contentPath;
303
+ }
304
+
305
+ /**
306
+ * Check if a file exists on device
307
+ *
308
+ * @param filePath The absolute file path
309
+ */
310
+ private static boolean fileExists(String filePath) {
311
+ File file = new File(filePath);
312
+
313
+ return file.exists();
314
+ }
315
+
316
+ /**
317
+ * Get full file path from external storage
318
+ *
319
+ * @param pathData The storage type and the relative path
320
+ */
321
+ private static String getPathFromExtSD(String[] pathData) {
322
+ final String type = pathData[0];
323
+ final String relativePath = "/" + pathData[1];
324
+ String fullPath = "";
325
+
326
+ // on my Sony devices (4.4.4 & 5.1.1), `type` is a dynamic string
327
+ // something like "71F8-2C0A", some kind of unique id per storage
328
+ // don't know any API that can get the root path of that storage based on its id.
329
+ //
330
+ // so no "primary" type, but let the check here for other devices
331
+ if ("primary".equalsIgnoreCase(type)) {
332
+ fullPath = Environment.getExternalStorageDirectory() + relativePath;
333
+ if (fileExists(fullPath)) {
334
+ return fullPath;
335
+ }
336
+ }
337
+
338
+ // Environment.isExternalStorageRemovable() is `true` for external and internal storage
339
+ // so we cannot relay on it.
340
+ //
341
+ // instead, for each possible path, check if file exists
342
+ // we'll start with secondary storage as this could be our (physically) removable sd card
343
+ fullPath = System.getenv("SECONDARY_STORAGE") + relativePath;
344
+ if (fileExists(fullPath)) {
345
+ return fullPath;
346
+ }
347
+
348
+ fullPath = System.getenv("EXTERNAL_STORAGE") + relativePath;
349
+ if (fileExists(fullPath)) {
350
+ return fullPath;
351
+ }
352
+
353
+ return fullPath;
354
+ }
355
+
356
+ /**
357
+ * Get a file path from a Uri. This will get the the path for Storage Access
358
+ * Framework Documents, as well as the _data field for the MediaStore and
359
+ * other file-based ContentProviders.<br>
360
+ * <br>
361
+ * Callers should check whether the path is local before assuming it
362
+ * represents a local file.
363
+ *
364
+ * @param context The context.
365
+ * @param uri The Uri to query.
366
+ */
367
+ private static String getPath(final Context context, final Uri uri) {
368
+
369
+ Log.d(TAG, "File - " +
370
+ "Authority: " + uri.getAuthority() +
371
+ ", Fragment: " + uri.getFragment() +
372
+ ", Port: " + uri.getPort() +
373
+ ", Query: " + uri.getQuery() +
374
+ ", Scheme: " + uri.getScheme() +
375
+ ", Host: " + uri.getHost() +
376
+ ", Segments: " + uri.getPathSegments().toString()
377
+ );
378
+
379
+ final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
380
+
381
+ // DocumentProvider
382
+ if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
383
+ // ExternalStorageProvider
384
+ if (isExternalStorageDocument(uri)) {
385
+ final String docId = DocumentsContract.getDocumentId(uri);
386
+ final String[] split = docId.split(":");
387
+ final String type = split[0];
388
+
389
+ String fullPath = getPathFromExtSD(split);
390
+ if (fullPath != "") {
391
+ return fullPath;
392
+ }
393
+ else {
394
+ return null;
395
+ }
396
+ }
397
+ // DownloadsProvider
398
+ else if (isDownloadsDocument(uri)) {
399
+ // thanks to https://github.com/hiddentao/cordova-plugin-filepath/issues/34#issuecomment-430129959
400
+ Cursor cursor = null;
401
+ try {
402
+ cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null);
403
+ if (cursor != null && cursor.moveToFirst()) {
404
+ String fileName = cursor.getString(0);
405
+ String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName;
406
+ if (!TextUtils.isEmpty(path)) {
407
+ return path;
408
+ }
409
+ }
410
+ } finally {
411
+ if (cursor != null)
412
+ cursor.close();
413
+ }
414
+ //
415
+ final String id = DocumentsContract.getDocumentId(uri);
416
+ try {
417
+ final Uri contentUri = ContentUris.withAppendedId(
418
+ Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
419
+
420
+ return getDataColumn(context, contentUri, null, null);
421
+ } catch(NumberFormatException e) {
422
+ //In Android 8 and Android P the id is not a number
423
+ return uri.getPath().replaceFirst("^/document/raw:", "").replaceFirst("^raw:", "");
424
+ }
425
+ }
426
+ // MediaProvider
427
+ else if (isMediaDocument(uri)) {
428
+ final String docId = DocumentsContract.getDocumentId(uri);
429
+ final String[] split = docId.split(":");
430
+ final String type = split[0];
431
+
432
+ Uri contentUri = null;
433
+ if ("image".equals(type)) {
434
+ contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
435
+ } else if ("video".equals(type)) {
436
+ contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
437
+ } else if ("audio".equals(type)) {
438
+ contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
439
+ }
440
+
441
+ final String selection = "_id=?";
442
+ final String[] selectionArgs = new String[] {
443
+ split[1]
444
+ };
445
+
446
+ return getDataColumn(context, contentUri, selection, selectionArgs);
447
+ }
448
+ else if(isGoogleDriveUri(uri)){
449
+ return getDriveFilePath(uri,context);
450
+ }
451
+ }
452
+ // MediaStore (and general)
453
+ else if ("content".equalsIgnoreCase(uri.getScheme())) {
454
+
455
+ // Return the remote address
456
+ if (isGooglePhotosUri(uri)) {
457
+ String contentPath = getContentFromSegments(uri.getPathSegments());
458
+ if (contentPath != "") {
459
+ return getPath(context, Uri.parse(contentPath));
460
+ }
461
+ else {
462
+ return null;
463
+ }
464
+ }
465
+
466
+ if(isGoogleDriveUri(uri)){
467
+ return getDriveFilePath(uri,context);
468
+ }
469
+
470
+ return getDataColumn(context, uri, null, null);
471
+ }
472
+ // File
473
+ else if ("file".equalsIgnoreCase(uri.getScheme())) {
474
+ return uri.getPath();
475
+ }
476
+
477
+ return null;
478
+ }
479
+
480
+ private static String getDriveFilePath(Uri uri,Context context){
481
+ Uri returnUri =uri;
482
+ Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
483
+ /*
484
+ * Get the column indexes of the data in the Cursor,
485
+ * * move to the first row in the Cursor, get the data,
486
+ * * and display it.
487
+ * */
488
+ int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
489
+ int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
490
+ returnCursor.moveToFirst();
491
+ String name = (returnCursor.getString(nameIndex));
492
+ String size = (Long.toString(returnCursor.getLong(sizeIndex)));
493
+ File file = new File(context.getCacheDir(),name);
494
+ try {
495
+ InputStream inputStream = context.getContentResolver().openInputStream(uri);
496
+ FileOutputStream outputStream = new FileOutputStream(file);
497
+ int read = 0;
498
+ int maxBufferSize = 1 * 1024 * 1024;
499
+ int bytesAvailable = inputStream.available();
500
+
501
+ //int bufferSize = 1024;
502
+ int bufferSize = Math.min(bytesAvailable, maxBufferSize);
503
+
504
+ final byte[] buffers = new byte[bufferSize];
505
+ while ((read = inputStream.read(buffers)) != -1) {
506
+ outputStream.write(buffers, 0, read);
507
+ }
508
+ Log.e("File Size","Size " + file.length());
509
+ inputStream.close();
510
+ outputStream.close();
511
+ Log.e("File Path","Path " + file.getPath());
512
+ Log.e("File Size","Size " + file.length());
513
+ }catch (Exception e){
514
+ Log.e("Exception",e.getMessage());
515
+ }
516
+ return file.getPath();
517
+ }
518
+ }