Skip to content
This repository has been archived by the owner on Nov 9, 2021. It is now read-only.

Commit

Permalink
Fix downloadeds files (#35)
Browse files Browse the repository at this point in the history
* Fix downloadeds files

* fix dependencies
  • Loading branch information
olaferlandsen authored and hiddentao committed Dec 13, 2018
1 parent e7ce911 commit db6b1d9
Showing 1 changed file with 28 additions and 12 deletions.
40 changes: 28 additions & 12 deletions src/android/FilePath.java
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.hiddentao.cordova.filepath;


import android.text.TextUtils;
import android.Manifest;
import android.content.ContentUris;
import android.content.Context;
Expand Down Expand Up @@ -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);
}
Expand Down Expand Up @@ -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.");

Expand All @@ -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:///... */
Expand Down Expand Up @@ -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();
}
Expand Down Expand Up @@ -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:", "");
Expand Down

0 comments on commit db6b1d9

Please sign in to comment.