diff --git a/plugin.xml b/plugin.xml index 360857d..ed31e3c 100644 --- a/plugin.xml +++ b/plugin.xml @@ -30,6 +30,7 @@ + diff --git a/src/android/FilePath.java b/src/android/FilePath.java index 67ae4fe..ec9626f 100644 --- a/src/android/FilePath.java +++ b/src/android/FilePath.java @@ -29,6 +29,14 @@ import java.util.List; import java.io.File; +import java.io.IOException; +import java.io.OutputStream; + +import android.content.pm.ApplicationInfo; + + +import org.apache.commons.io.IOUtils; + public class FilePath extends CordovaPlugin { private static final String TAG = "[FilePath plugin]: "; @@ -70,7 +78,6 @@ public void initialize(CordovaInterface cordova, final CordovaWebView webView) { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { this.callback = callbackContext; this.uriStr = args.getString(0); - if (action.equals("resolveNativePath")) { if (PermissionHelper.hasPermission(this, READ)) { resolveNativePath(); @@ -97,11 +104,18 @@ public void resolveNativePath() throws JSONException { JSONObject resultObj = new JSONObject(); /* content:///... */ Uri pvUrl = Uri.parse(this.uriStr); - Log.d(TAG, "URI: " + this.uriStr); Context appContext = this.cordova.getActivity().getApplicationContext(); - String filePath = getPath(appContext, pvUrl); + String filePath; + + final boolean isNougat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.N; + + if(isNougat) { + filePath = getFilePathFromURI(appContext, pvUrl); + } else { + filePath = getPath(appContext, pvUrl); + } //check result; send error/success callback if (filePath == GET_PATH_ERROR_ID) { @@ -123,6 +137,60 @@ else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) { } } + public static String getFilePathFromURI(Context context, Uri contentUri) { + //copy file and send new file path + String fileName = getFileName(contentUri); + if (!TextUtils.isEmpty(fileName)) { + File folder = new File (Environment.getExternalStorageDirectory().getPath() + + File.separator + getApplicationName(context)); + + boolean success = true; + + if (!folder.exists()) { + success = folder.mkdirs(); + } + + if (success) { + File copyFile = new File(Environment.getExternalStorageDirectory().getPath() + + File.separator + getApplicationName(context) + File.separator + fileName); + copy(context, contentUri, copyFile); + return copyFile.getAbsolutePath(); + } + + } + return null; + } + + public static String getFileName(Uri uri) { + if (uri == null) return null; + String fileName = null; + String path = uri.getPath(); + int cut = path.lastIndexOf('/'); + if (cut != -1) { + fileName = path.substring(cut + 1); + } + return fileName; + } + + public static void copy(Context context, Uri srcUri, File dstFile) { + try { + InputStream inputStream = context.getContentResolver().openInputStream(srcUri); + if (inputStream == null) return; + OutputStream outputStream = new FileOutputStream(dstFile); + IOUtils.copy(inputStream, outputStream); + inputStream.close(); + outputStream.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + public static String getApplicationName(Context context) { + ApplicationInfo applicationInfo = context.getApplicationInfo(); + int stringId = applicationInfo.labelRes; + return stringId == 0 ? applicationInfo.nonLocalizedLabel.toString() : context.getString(stringId); + } + public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { for (int r:grantResults) { if (r == PackageManager.PERMISSION_DENIED) { @@ -449,3 +517,4 @@ private static String getDriveFilePath(Uri uri,Context context){ return file.getPath(); } } +