update cordova-plugin-filepath to latest version

This commit is contained in:
Morgan McMillian 2018-09-02 12:03:39 -07:00
parent 1c970a50e3
commit 10bc57a281
5 changed files with 442 additions and 438 deletions

8
package-lock.json generated
View file

@ -2140,7 +2140,7 @@
}, },
"cordova-android": { "cordova-android": {
"version": "7.1.0", "version": "7.1.0",
"resolved": "https://registry.npmjs.org/cordova-android/-/cordova-android-7.1.0.tgz", "resolved": "http://registry.npmjs.org/cordova-android/-/cordova-android-7.1.0.tgz",
"integrity": "sha1-HNNu57nRm61Q0+mEK3Mf5ZCSTdU=", "integrity": "sha1-HNNu57nRm61Q0+mEK3Mf5ZCSTdU=",
"requires": { "requires": {
"android-versions": "1.3.0", "android-versions": "1.3.0",
@ -2389,9 +2389,9 @@
"integrity": "sha1-+lnpe4zdkSYL4L7EYNuOxd6TSI8=" "integrity": "sha1-+lnpe4zdkSYL4L7EYNuOxd6TSI8="
}, },
"cordova-plugin-filepath": { "cordova-plugin-filepath": {
"version": "1.3.0", "version": "1.4.2",
"resolved": "https://registry.npmjs.org/cordova-plugin-filepath/-/cordova-plugin-filepath-1.3.0.tgz", "resolved": "https://registry.npmjs.org/cordova-plugin-filepath/-/cordova-plugin-filepath-1.4.2.tgz",
"integrity": "sha512-Dwd76FcVciSssAZ/FPfwfOlfHrmx7CAjYTNzCxeJ4NsMkCfTysCR3vHdX/03XExGi+74DImUB17uXsnyyOYnAQ==" "integrity": "sha512-6AKL6dIUkY+iOYesP+3/9LC1cxIilDIo0Hdu4tdDNM/ELIxrmxKywTqGy4x76Fv/LIl6l7g8R2tgCbBXTaBvMw=="
}, },
"cordova-plugin-inappbrowser": { "cordova-plugin-inappbrowser": {
"version": "3.0.0", "version": "3.0.0",

View file

@ -37,7 +37,7 @@
"cordova-plugin-file": "^6.0.1", "cordova-plugin-file": "^6.0.1",
"cordova-plugin-file-transfer": "^1.7.1", "cordova-plugin-file-transfer": "^1.7.1",
"cordova-plugin-filechooser": "^1.0.1", "cordova-plugin-filechooser": "^1.0.1",
"cordova-plugin-filepath": "^1.3.0", "cordova-plugin-filepath": "^1.4.2",
"cordova-plugin-inappbrowser": "^3.0.0", "cordova-plugin-inappbrowser": "^3.0.0",
"cordova-plugin-share-content": "^1.0.0", "cordova-plugin-share-content": "^1.0.0",
"cordova-plugin-splashscreen": "^5.0.2", "cordova-plugin-splashscreen": "^5.0.2",

View file

@ -434,6 +434,6 @@
"cordova-plugin-device": "2.0.2", "cordova-plugin-device": "2.0.2",
"cordova-plugin-console": "1.1.0", "cordova-plugin-console": "1.1.0",
"cordova-android-support-gradle-release": "1.4.4", "cordova-android-support-gradle-release": "1.4.4",
"cordova-plugin-filepath": "1.0.2" "cordova-plugin-filepath": "1.4.2"
} }
} }

View file

@ -1,431 +1,435 @@
package com.hiddentao.cordova.filepath; package com.hiddentao.cordova.filepath;
import android.Manifest; import android.Manifest;
import android.content.ContentUris; import android.content.ContentUris;
import android.content.Context; import android.content.Context;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.net.Uri; import android.net.Uri;
import android.provider.OpenableColumns; import android.provider.OpenableColumns;
import android.util.Log; import android.util.Log;
import android.database.Cursor; import android.database.Cursor;
import android.os.Build; import android.os.Build;
import android.os.Environment; import android.os.Environment;
import android.provider.DocumentsContract; import android.provider.DocumentsContract;
import android.provider.MediaStore; import android.provider.MediaStore;
import android.support.v4.app.ActivityCompat; import android.support.v4.app.ActivityCompat;
import org.apache.cordova.CallbackContext; import org.apache.cordova.CallbackContext;
import org.apache.cordova.CordovaInterface; import org.apache.cordova.CordovaInterface;
import org.apache.cordova.CordovaPlugin; import org.apache.cordova.CordovaPlugin;
import org.apache.cordova.CordovaWebView; import org.apache.cordova.CordovaWebView;
import org.apache.cordova.PermissionHelper; import org.apache.cordova.PermissionHelper;
import org.json.JSONArray; import org.json.JSONArray;
import org.json.JSONObject; import org.json.JSONObject;
import org.json.JSONException; import org.json.JSONException;
import java.io.FileOutputStream; import java.io.FileOutputStream;
import java.io.InputStream; import java.io.InputStream;
import java.util.List; import java.util.List;
import java.io.File; import java.io.File;
public class FilePath extends CordovaPlugin { public class FilePath extends CordovaPlugin {
private static final String TAG = "[FilePath plugin]: "; private static final String TAG = "[FilePath plugin]: ";
private static final int INVALID_ACTION_ERROR_CODE = -1; private static final int INVALID_ACTION_ERROR_CODE = -1;
private static final int GET_PATH_ERROR_CODE = 0; private static final int GET_PATH_ERROR_CODE = 0;
private static final String GET_PATH_ERROR_ID = null; private static final String GET_PATH_ERROR_ID = null;
private static final int GET_CLOUD_PATH_ERROR_CODE = 1; private static final int GET_CLOUD_PATH_ERROR_CODE = 1;
private static final String GET_CLOUD_PATH_ERROR_ID = "cloud"; private static final String GET_CLOUD_PATH_ERROR_ID = "cloud";
private static final int RC_READ_EXTERNAL_STORAGE = 5; private static final int RC_READ_EXTERNAL_STORAGE = 5;
private static CallbackContext callback; private static CallbackContext callback;
private static String uriStr; private static String uriStr;
public static final int READ_REQ_CODE = 0; public static final int READ_REQ_CODE = 0;
public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE; public static final String READ = Manifest.permission.READ_EXTERNAL_STORAGE;
protected void getReadPermission(int requestCode) { protected void getReadPermission(int requestCode) {
PermissionHelper.requestPermission(this, requestCode, READ); PermissionHelper.requestPermission(this, requestCode, READ);
} }
public void initialize(CordovaInterface cordova, final CordovaWebView webView) { public void initialize(CordovaInterface cordova, final CordovaWebView webView) {
super.initialize(cordova, webView); super.initialize(cordova, webView);
} }
/** /**
* Executes the request and returns PluginResult. * Executes the request and returns PluginResult.
* *
* @param action The action to execute. * @param action The action to execute.
* @param args JSONArry of arguments for the plugin. * @param args JSONArry of arguments for the plugin.
* @param callbackContext The callback context through which to return stuff to caller. * @param callbackContext The callback context through which to return stuff to caller.
* @return A PluginResult object with a status and message. * @return A PluginResult object with a status and message.
*/ */
@Override @Override
public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException { public boolean execute(String action, JSONArray args, CallbackContext callbackContext) throws JSONException {
this.callback = callbackContext; this.callback = callbackContext;
this.uriStr = args.getString(0); this.uriStr = args.getString(0);
if (action.equals("resolveNativePath")) { if (action.equals("resolveNativePath")) {
if (PermissionHelper.hasPermission(this, READ)) { if (PermissionHelper.hasPermission(this, READ)) {
resolveNativePath(); resolveNativePath();
} }
else { else {
getReadPermission(READ_REQ_CODE); getReadPermission(READ_REQ_CODE);
} }
return true; return true;
} }
else { else {
JSONObject resultObj = new JSONObject(); JSONObject resultObj = new JSONObject();
resultObj.put("code", INVALID_ACTION_ERROR_CODE); resultObj.put("code", INVALID_ACTION_ERROR_CODE);
resultObj.put("message", "Invalid action."); resultObj.put("message", "Invalid action.");
callbackContext.error(resultObj); callbackContext.error(resultObj);
} }
return false; return false;
} }
public void resolveNativePath() throws JSONException { public void resolveNativePath() throws JSONException {
JSONObject resultObj = new JSONObject(); JSONObject resultObj = new JSONObject();
/* content:///... */ /* content:///... */
Uri pvUrl = Uri.parse(this.uriStr); Uri pvUrl = Uri.parse(this.uriStr);
Log.d(TAG, "URI: " + this.uriStr); Log.d(TAG, "URI: " + this.uriStr);
Context appContext = this.cordova.getActivity().getApplicationContext(); Context appContext = this.cordova.getActivity().getApplicationContext();
String filePath = getPath(appContext, pvUrl); String filePath = getPath(appContext, pvUrl);
//check result; send error/success callback //check result; send error/success callback
if (filePath == GET_PATH_ERROR_ID) { if (filePath == GET_PATH_ERROR_ID) {
resultObj.put("code", GET_PATH_ERROR_CODE); resultObj.put("code", GET_PATH_ERROR_CODE);
resultObj.put("message", "Unable to resolve filesystem path."); resultObj.put("message", "Unable to resolve filesystem path.");
this.callback.error(resultObj); this.callback.error(resultObj);
} }
else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) { else if (filePath.equals(GET_CLOUD_PATH_ERROR_ID)) {
resultObj.put("code", GET_CLOUD_PATH_ERROR_CODE); resultObj.put("code", GET_CLOUD_PATH_ERROR_CODE);
resultObj.put("message", "Files from cloud cannot be resolved to filesystem, download is required."); resultObj.put("message", "Files from cloud cannot be resolved to filesystem, download is required.");
this.callback.error(resultObj); this.callback.error(resultObj);
} }
else { else {
Log.d(TAG, "Filepath: " + filePath); Log.d(TAG, "Filepath: " + filePath);
this.callback.success("file://" + filePath); this.callback.success("file://" + filePath);
} }
} }
public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException { public void onRequestPermissionResult(int requestCode, String[] permissions, int[] grantResults) throws JSONException {
for (int r:grantResults) { for (int r:grantResults) {
if (r == PackageManager.PERMISSION_DENIED) { if (r == PackageManager.PERMISSION_DENIED) {
JSONObject resultObj = new JSONObject(); JSONObject resultObj = new JSONObject();
resultObj.put("code", 3); resultObj.put("code", 3);
resultObj.put("message", "Filesystem permission was denied."); resultObj.put("message", "Filesystem permission was denied.");
this.callback.error(resultObj); this.callback.error(resultObj);
return; return;
} }
} }
if (requestCode == READ_REQ_CODE) { if (requestCode == READ_REQ_CODE) {
resolveNativePath(); resolveNativePath();
} }
} }
/** /**
* @param uri The Uri to check. * @param uri The Uri to check.
* @return Whether the Uri authority is ExternalStorageProvider. * @return Whether the Uri authority is ExternalStorageProvider.
*/ */
private static boolean isExternalStorageDocument(Uri uri) { private static boolean isExternalStorageDocument(Uri uri) {
return "com.android.externalstorage.documents".equals(uri.getAuthority()); return "com.android.externalstorage.documents".equals(uri.getAuthority());
} }
/** /**
* @param uri The Uri to check. * @param uri The Uri to check.
* @return Whether the Uri authority is DownloadsProvider. * @return Whether the Uri authority is DownloadsProvider.
*/ */
private static boolean isDownloadsDocument(Uri uri) { private static boolean isDownloadsDocument(Uri uri) {
return "com.android.providers.downloads.documents".equals(uri.getAuthority()); return "com.android.providers.downloads.documents".equals(uri.getAuthority());
} }
/** /**
* @param uri The Uri to check. * @param uri The Uri to check.
* @return Whether the Uri authority is MediaProvider. * @return Whether the Uri authority is MediaProvider.
*/ */
private static boolean isMediaDocument(Uri uri) { private static boolean isMediaDocument(Uri uri) {
return "com.android.providers.media.documents".equals(uri.getAuthority()); return "com.android.providers.media.documents".equals(uri.getAuthority());
} }
/** /**
* @param uri The Uri to check. * @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos. * @return Whether the Uri authority is Google Photos.
*/ */
private static boolean isGooglePhotosUri(Uri uri) { private static boolean isGooglePhotosUri(Uri uri) {
return ("com.google.android.apps.photos.content".equals(uri.getAuthority()) return ("com.google.android.apps.photos.content".equals(uri.getAuthority())
|| "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority())); || "com.google.android.apps.photos.contentprovider".equals(uri.getAuthority()));
} }
/** /**
* @param uri The Uri to check. * @param uri The Uri to check.
* @return Whether the Uri authority is Google Drive. * @return Whether the Uri authority is Google Drive.
*/ */
private static boolean isGoogleDriveUri(Uri uri) { private static boolean isGoogleDriveUri(Uri uri) {
return "com.google.android.apps.docs.storage".equals(uri.getAuthority()) || "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority()); return "com.google.android.apps.docs.storage".equals(uri.getAuthority()) || "com.google.android.apps.docs.storage.legacy".equals(uri.getAuthority());
} }
/** /**
* Get the value of the data column for this Uri. This is useful for * Get the value of the data column for this Uri. This is useful for
* MediaStore Uris, and other file-based ContentProviders. * MediaStore Uris, and other file-based ContentProviders.
* *
* @param context The context. * @param context The context.
* @param uri The Uri to query. * @param uri The Uri to query.
* @param selection (Optional) Filter used in the query. * @param selection (Optional) Filter used in the query.
* @param selectionArgs (Optional) Selection arguments used in the query. * @param selectionArgs (Optional) Selection arguments used in the query.
* @return The value of the _data column, which is typically a file path. * @return The value of the _data column, which is typically a file path.
*/ */
private static String getDataColumn(Context context, Uri uri, String selection, private static String getDataColumn(Context context, Uri uri, String selection,
String[] selectionArgs) { String[] selectionArgs) {
Cursor cursor = null; Cursor cursor = null;
final String column = "_data"; final String column = "_data";
final String[] projection = { final String[] projection = {
column column
}; };
try { try {
cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs, cursor = context.getContentResolver().query(uri, projection, selection, selectionArgs,
null); null);
if (cursor != null && cursor.moveToFirst()) { if (cursor != null && cursor.moveToFirst()) {
final int column_index = cursor.getColumnIndexOrThrow(column); final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index); return cursor.getString(column_index);
} }
} finally { } finally {
if (cursor != null) if (cursor != null)
cursor.close(); cursor.close();
} }
return null; return null;
} }
/** /**
* Get content:// from segment list * Get content:// from segment list
* In the new Uri Authority of Google Photos, the last segment is not the content:// anymore * In the new Uri Authority of Google Photos, the last segment is not the content:// anymore
* So let's iterate through all segments and find the content uri! * So let's iterate through all segments and find the content uri!
* *
* @param segments The list of segment * @param segments The list of segment
*/ */
private static String getContentFromSegments(List<String> segments) { private static String getContentFromSegments(List<String> segments) {
String contentPath = ""; String contentPath = "";
for(String item : segments) { for(String item : segments) {
if (item.startsWith("content://")) { if (item.startsWith("content://")) {
contentPath = item; contentPath = item;
break; break;
} }
} }
return contentPath; return contentPath;
} }
/** /**
* Check if a file exists on device * Check if a file exists on device
* *
* @param filePath The absolute file path * @param filePath The absolute file path
*/ */
private static boolean fileExists(String filePath) { private static boolean fileExists(String filePath) {
File file = new File(filePath); File file = new File(filePath);
return file.exists(); return file.exists();
} }
/** /**
* Get full file path from external storage * Get full file path from external storage
* *
* @param pathData The storage type and the relative path * @param pathData The storage type and the relative path
*/ */
private static String getPathFromExtSD(String[] pathData) { private static String getPathFromExtSD(String[] pathData) {
final String type = pathData[0]; final String type = pathData[0];
final String relativePath = "/" + pathData[1]; final String relativePath = "/" + pathData[1];
String fullPath = ""; String fullPath = "";
// on my Sony devices (4.4.4 & 5.1.1), `type` is a dynamic string // on my Sony devices (4.4.4 & 5.1.1), `type` is a dynamic string
// something like "71F8-2C0A", some kind of unique id per storage // something like "71F8-2C0A", some kind of unique id per storage
// don't know any API that can get the root path of that storage based on its id. // don't know any API that can get the root path of that storage based on its id.
// //
// so no "primary" type, but let the check here for other devices // so no "primary" type, but let the check here for other devices
if ("primary".equalsIgnoreCase(type)) { if ("primary".equalsIgnoreCase(type)) {
fullPath = Environment.getExternalStorageDirectory() + relativePath; fullPath = Environment.getExternalStorageDirectory() + relativePath;
if (fileExists(fullPath)) { if (fileExists(fullPath)) {
return fullPath; return fullPath;
} }
} }
// Environment.isExternalStorageRemovable() is `true` for external and internal storage // Environment.isExternalStorageRemovable() is `true` for external and internal storage
// so we cannot relay on it. // so we cannot relay on it.
// //
// instead, for each possible path, check if file exists // instead, for each possible path, check if file exists
// we'll start with secondary storage as this could be our (physically) removable sd card // we'll start with secondary storage as this could be our (physically) removable sd card
fullPath = System.getenv("SECONDARY_STORAGE") + relativePath; fullPath = System.getenv("SECONDARY_STORAGE") + relativePath;
if (fileExists(fullPath)) { if (fileExists(fullPath)) {
return fullPath; return fullPath;
} }
fullPath = System.getenv("EXTERNAL_STORAGE") + relativePath; fullPath = System.getenv("EXTERNAL_STORAGE") + relativePath;
if (fileExists(fullPath)) { if (fileExists(fullPath)) {
return fullPath; return fullPath;
} }
return fullPath; return fullPath;
} }
/** /**
* Get a file path from a Uri. This will get the the path for Storage Access * Get a file path from a Uri. This will get the the path for Storage Access
* Framework Documents, as well as the _data field for the MediaStore and * Framework Documents, as well as the _data field for the MediaStore and
* other file-based ContentProviders.<br> * other file-based ContentProviders.<br>
* <br> * <br>
* Callers should check whether the path is local before assuming it * Callers should check whether the path is local before assuming it
* represents a local file. * represents a local file.
* *
* @param context The context. * @param context The context.
* @param uri The Uri to query. * @param uri The Uri to query.
*/ */
private static String getPath(final Context context, final Uri uri) { private static String getPath(final Context context, final Uri uri) {
Log.d(TAG, "File - " + Log.d(TAG, "File - " +
"Authority: " + uri.getAuthority() + "Authority: " + uri.getAuthority() +
", Fragment: " + uri.getFragment() + ", Fragment: " + uri.getFragment() +
", Port: " + uri.getPort() + ", Port: " + uri.getPort() +
", Query: " + uri.getQuery() + ", Query: " + uri.getQuery() +
", Scheme: " + uri.getScheme() + ", Scheme: " + uri.getScheme() +
", Host: " + uri.getHost() + ", Host: " + uri.getHost() +
", Segments: " + uri.getPathSegments().toString() ", Segments: " + uri.getPathSegments().toString()
); );
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// DocumentProvider // DocumentProvider
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) { if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
// ExternalStorageProvider // ExternalStorageProvider
if (isExternalStorageDocument(uri)) { if (isExternalStorageDocument(uri)) {
final String docId = DocumentsContract.getDocumentId(uri); final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":"); final String[] split = docId.split(":");
final String type = split[0]; final String type = split[0];
String fullPath = getPathFromExtSD(split); String fullPath = getPathFromExtSD(split);
if (fullPath != "") { if (fullPath != "") {
return fullPath; return fullPath;
} }
else { else {
return null; return null;
} }
} }
// DownloadsProvider // DownloadsProvider
else if (isDownloadsDocument(uri)) { else if (isDownloadsDocument(uri)) {
final String id = DocumentsContract.getDocumentId(uri);
final String id = DocumentsContract.getDocumentId(uri); try {
final Uri contentUri = ContentUris.withAppendedId( final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
return getDataColumn(context, contentUri, null, null); return getDataColumn(context, contentUri, null, null);
} } catch(NumberFormatException e) {
// MediaProvider //In Android 8 and Android P the id is not a number
else if (isMediaDocument(uri)) { return uri.getPath().replaceFirst("^/document/raw:", "").replaceFirst("^raw:", "");
final String docId = DocumentsContract.getDocumentId(uri); }
final String[] split = docId.split(":"); }
final String type = split[0]; // MediaProvider
else if (isMediaDocument(uri)) {
Uri contentUri = null; final String docId = DocumentsContract.getDocumentId(uri);
if ("image".equals(type)) { final String[] split = docId.split(":");
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI; final String type = split[0];
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI; Uri contentUri = null;
} else if ("audio".equals(type)) { if ("image".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI; contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} } else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
final String selection = "_id=?"; } else if ("audio".equals(type)) {
final String[] selectionArgs = new String[] { contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
split[1] }
};
final String selection = "_id=?";
return getDataColumn(context, contentUri, selection, selectionArgs); final String[] selectionArgs = new String[] {
} split[1]
else if(isGoogleDriveUri(uri)){ };
return getDriveFilePath(uri,context);
} return getDataColumn(context, contentUri, selection, selectionArgs);
} }
// MediaStore (and general) else if(isGoogleDriveUri(uri)){
else if ("content".equalsIgnoreCase(uri.getScheme())) { return getDriveFilePath(uri,context);
}
// Return the remote address }
if (isGooglePhotosUri(uri)) { // MediaStore (and general)
String contentPath = getContentFromSegments(uri.getPathSegments()); else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (contentPath != "") {
return getPath(context, Uri.parse(contentPath)); // Return the remote address
} if (isGooglePhotosUri(uri)) {
else { String contentPath = getContentFromSegments(uri.getPathSegments());
return null; if (contentPath != "") {
} return getPath(context, Uri.parse(contentPath));
} }
else {
if(isGoogleDriveUri(uri)){ return null;
return getDriveFilePath(uri,context); }
} }
return getDataColumn(context, uri, null, null); if(isGoogleDriveUri(uri)){
} return getDriveFilePath(uri,context);
// File }
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath(); return getDataColumn(context, uri, null, null);
} }
// File
return null; else if ("file".equalsIgnoreCase(uri.getScheme())) {
} return uri.getPath();
}
private static String getDriveFilePath(Uri uri,Context context){
Uri returnUri =uri; return null;
Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null); }
/*
* Get the column indexes of the data in the Cursor, private static String getDriveFilePath(Uri uri,Context context){
* * move to the first row in the Cursor, get the data, Uri returnUri =uri;
* * and display it. Cursor returnCursor = context.getContentResolver().query(returnUri, null, null, null, null);
* */ /*
int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME); * Get the column indexes of the data in the Cursor,
int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE); * * move to the first row in the Cursor, get the data,
returnCursor.moveToFirst(); * * and display it.
String name = (returnCursor.getString(nameIndex)); * */
String size = (Long.toString(returnCursor.getLong(sizeIndex))); int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
File file = new File(context.getCacheDir(),name); int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
try { returnCursor.moveToFirst();
InputStream inputStream = context.getContentResolver().openInputStream(uri); String name = (returnCursor.getString(nameIndex));
FileOutputStream outputStream = new FileOutputStream(file); String size = (Long.toString(returnCursor.getLong(sizeIndex)));
int read = 0; File file = new File(context.getCacheDir(),name);
int maxBufferSize = 1 * 1024 * 1024; try {
int bytesAvailable = inputStream.available(); InputStream inputStream = context.getContentResolver().openInputStream(uri);
FileOutputStream outputStream = new FileOutputStream(file);
//int bufferSize = 1024; int read = 0;
int bufferSize = Math.min(bytesAvailable, maxBufferSize); int maxBufferSize = 1 * 1024 * 1024;
int bytesAvailable = inputStream.available();
final byte[] buffers = new byte[bufferSize];
while ((read = inputStream.read(buffers)) != -1) { //int bufferSize = 1024;
outputStream.write(buffers, 0, read); int bufferSize = Math.min(bytesAvailable, maxBufferSize);
}
Log.e("File Size","Size " + file.length()); final byte[] buffers = new byte[bufferSize];
inputStream.close(); while ((read = inputStream.read(buffers)) != -1) {
outputStream.close(); outputStream.write(buffers, 0, read);
Log.e("File Path","Path " + file.getPath()); }
Log.e("File Size","Size " + file.length()); Log.e("File Size","Size " + file.length());
}catch (Exception e){ inputStream.close();
Log.e("Exception",e.getMessage()); outputStream.close();
} Log.e("File Path","Path " + file.getPath());
return file.getPath(); Log.e("File Size","Size " + file.length());
} }catch (Exception e){
} Log.e("Exception",e.getMessage());
}
return file.getPath();
}
}

View file

@ -280,7 +280,7 @@ module.exports.metadata =
"cordova-plugin-device": "2.0.2", "cordova-plugin-device": "2.0.2",
"cordova-plugin-console": "1.1.0", "cordova-plugin-console": "1.1.0",
"cordova-android-support-gradle-release": "1.4.4", "cordova-android-support-gradle-release": "1.4.4",
"cordova-plugin-filepath": "1.0.2" "cordova-plugin-filepath": "1.4.2"
}; };
// BOTTOM OF METADATA // BOTTOM OF METADATA
}); });