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

Commit

Permalink
Get native path from third party apps such as WhatsApp
Browse files Browse the repository at this point in the history
  • Loading branch information
marvin-SL committed May 24, 2019
1 parent ca44b05 commit 319280b
Showing 1 changed file with 65 additions and 3 deletions.
68 changes: 65 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,12 +104,12 @@ 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 = getPath(appContext, pvUrl);
String filePath = getFilePathFromURI(appContext, pvUrl);
//check result; send error/success callback
if (filePath == GET_PATH_ERROR_ID) {
resultObj.put("code", GET_PATH_ERROR_CODE);
Expand All @@ -123,6 +130,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 +510,4 @@ private static String getDriveFilePath(Uri uri,Context context){
return file.getPath();
}
}

0 comments on commit 319280b

Please sign in to comment.