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

Get native path from third party apps such as WhatsApp #53

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions plugin.xml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

<!-- android -->
<platform name="android">
<framework src="commons-io:commons-io:2.4" />
<js-module src="www/FilePath.js" name="FilePath">
<clobbers target="window.FilePath"/>
</js-module>
Expand Down
75 changes: 72 additions & 3 deletions src/android/FilePath.java
Original file line number Diff line number Diff line change
Expand Up @@ -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]: ";
Expand Down Expand Up @@ -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();
Expand All @@ -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) {
Expand All @@ -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) {
Expand Down Expand Up @@ -449,3 +517,4 @@ private static String getDriveFilePath(Uri uri,Context context){
return file.getPath();
}
}