From db6b1d995bb2ed98bb040fbb7cb7687c8b8e38e2 Mon Sep 17 00:00:00 2001 From: Olaf Erlandsen Date: Thu, 13 Dec 2018 19:20:58 -0300 Subject: [PATCH] Fix downloadeds files (#35) * Fix downloadeds files * fix dependencies --- src/android/FilePath.java | 40 +++++++++++++++++++++++++++------------ 1 file changed, 28 insertions(+), 12 deletions(-) diff --git a/src/android/FilePath.java b/src/android/FilePath.java index be0a19b..67ae4fe 100644 --- a/src/android/FilePath.java +++ b/src/android/FilePath.java @@ -1,6 +1,6 @@ package com.hiddentao.cordova.filepath; - +import android.text.TextUtils; import android.Manifest; import android.content.ContentUris; import android.content.Context; @@ -42,14 +42,14 @@ public class FilePath extends CordovaPlugin { private static final String GET_CLOUD_PATH_ERROR_ID = "cloud"; private static final int RC_READ_EXTERNAL_STORAGE = 5; - + private static CallbackContext callback; private static String uriStr; - + public static final int READ_REQ_CODE = 0; - + public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE; - + protected void getReadPermission(int requestCode) { PermissionHelper.requestPermission(this, requestCode, READ); } @@ -83,7 +83,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo } else { JSONObject resultObj = new JSONObject(); - + resultObj.put("code", INVALID_ACTION_ERROR_CODE); resultObj.put("message", "Invalid action."); @@ -92,7 +92,7 @@ public boolean execute(String action, JSONArray args, CallbackContext callbackCo return false; } - + public void resolveNativePath() throws JSONException { JSONObject resultObj = new JSONObject(); /* content:///... */ @@ -122,19 +122,19 @@ else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) { this.callback.success("file://" + filePath); } } - + public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { for (int r:grantResults) { if (r == PackageManager.PERMISSION_DENIED) { JSONObject resultObj = new JSONObject(); resultObj.put("code", 3); resultObj.put("message", "Filesystem permission was denied."); - + this.callback.error(resultObj); return; } } - + if (requestCode == READ_REQ_CODE) { resolveNativePath(); } @@ -329,12 +329,28 @@ private static String getPath(final Context context, final Uri uri) { } // DownloadsProvider else if (isDownloadsDocument(uri)) { + // thanks to https://github.com/hiddentao/cordova-plugin-filepath/issues/34#issuecomment-430129959 + Cursor cursor = null; + try { + cursor = context.getContentResolver().query(uri, new String[]{MediaStore.MediaColumns.DISPLAY_NAME}, null, null, null); + if (cursor != null && cursor.moveToFirst()) { + String fileName = cursor.getString(0); + String path = Environment.getExternalStorageDirectory().toString() + "/Download/" + fileName; + if (!TextUtils.isEmpty(path)) { + return path; + } + } + } finally { + if (cursor != null) + cursor.close(); + } + // final String id = DocumentsContract.getDocumentId(uri); try { final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); - - return getDataColumn(context, contentUri, null, null); + + return getDataColumn(context, contentUri, null, null); } catch(NumberFormatException e) { //In Android 8 and Android P the id is not a number return uri.getPath().replaceFirst("^/document/raw:", "").replaceFirst("^raw:", "");