From 496fec57a35e9d607f426fc8d340b0fe7ebd7020 Mon Sep 17 00:00:00 2001 From: krishna Date: Thu, 11 Jan 2018 15:28:27 +0530 Subject: [PATCH 1/3] issue fix: Google drive image path null --- .../afilechooser/utils/FileUtils.java | 71 +++++++++++++------ 1 file changed, 49 insertions(+), 22 deletions(-) diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java index 25c8008..de5ae66 100644 --- a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java +++ b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java @@ -39,15 +39,17 @@ import java.util.Comparator; /** - * @version 2009-07-03 * @author Peli - * @version 2013-12-11 * @author paulburke (ipaulpro) + * @version 2013-12-11 */ public class FileUtils { - private FileUtils() {} //private constructor to enforce Singleton pattern - - /** TAG for log messages. */ + private FileUtils() { + } //private constructor to enforce Singleton pattern + + /** + * TAG for log messages. + */ static final String TAG = "FileUtils"; private static final boolean DEBUG = false; // Set to true to enable logging @@ -64,7 +66,7 @@ private FileUtils() {} //private constructor to enforce Singleton pattern * * @param uri * @return Extension including the dot("."); "" if there is no extension; - * null if uri was null. + * null if uri was null. */ public static String getExtension(String uri) { if (uri == null) { @@ -207,15 +209,15 @@ public static boolean isGooglePhotosUri(Uri uri) { * Get the value of the data column for this Uri. This is useful for * MediaStore Uris, and other file-based ContentProviders. * - * @param context The context. - * @param uri The Uri to query. - * @param selection (Optional) Filter used in the query. + * @param context The context. + * @param uri The Uri to query. + * @param selection (Optional) Filter 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. * @author paulburke */ public static String getDataColumn(Context context, Uri uri, String selection, - String[] selectionArgs) { + String[] selectionArgs) { Cursor cursor = null; final String column = "_data"; @@ -247,12 +249,12 @@ public static String getDataColumn(Context context, Uri uri, String selection, *
* Callers should check whether the path is local before assuming it * represents a local file. - * + * * @param context The context. - * @param uri The Uri to query. + * @param uri The Uri to query. + * @author paulburke * @see #isLocal(String) * @see #getFile(Context, Uri) - * @author paulburke */ public static String getPath(final Context context, final Uri uri) { @@ -265,7 +267,7 @@ public static String getPath(final Context context, final Uri uri) { ", Scheme: " + uri.getScheme() + ", Host: " + uri.getHost() + ", Segments: " + uri.getPathSegments().toString() - ); + ); final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT; @@ -294,8 +296,11 @@ else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); - - return getDataColumn(context, contentUri, null, null); + String dataColumn = getDataColumn(context, contentUri, null, null); + if (dataColumn == null) { + ContextWra + } else + return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) { @@ -313,7 +318,7 @@ else if (isMediaDocument(uri)) { } final String selection = "_id=?"; - final String[] selectionArgs = new String[] { + final String[] selectionArgs = new String[]{ split[1] }; @@ -327,7 +332,11 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) { if (isGooglePhotosUri(uri)) return uri.getLastPathSegment(); - return getDataColumn(context, uri, null, null); + String filePath = getDataColumn(context, uri, null, null); + if (filePath == null) { + filePath = getDriveImageFilePath(context, uri); + } + return filePath; } // File else if ("file".equalsIgnoreCase(uri.getScheme())) { @@ -337,13 +346,32 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) { return null; } + private static String getDriveImageFilePath(Context context, Uri uri) throws IOException { + ContextWrapper ctr = new ContextWrapper(context.getApplicationContext()); + // path to /data/data/yourapp/app_data/imageDir + File directory = ctr.getDir("imageDir", Context.MODE_PRIVATE); + // Create imageDir + File driveImageFile = new File(directory, "tempDriveImage.jpg"); + FileOutputStream fs = null; + InputStream is = context.getContentResolver().openInputStream(uri); + fs = new FileOutputStream(driveImageFile); + int n; + byte[] buffer = new byte[1024]; + if (is == null) throw new IOException(); + while ((n = is.read(buffer)) > -1) { + fs.write(buffer, 0, n); + } + fs.close(); + return driveImageFile.getAbsolutePath(); + } + /** * Convert Uri into File, if possible. * * @return file A local file that the Uri was pointing to, or null if the - * Uri is unsupported or pointed to a remote resource. - * @see #getPath(Context, Uri) + * Uri is unsupported or pointed to a remote resource. * @author paulburke + * @see #getPath(Context, Uri) */ public static File getFile(Context context, Uri uri) { if (uri != null) { @@ -448,8 +476,7 @@ public static Bitmap getThumbnail(Context context, Uri uri, String mimeType) { id, MediaStore.Video.Thumbnails.MINI_KIND, null); - } - else if (mimeType.contains(FileUtils.MIME_TYPE_IMAGE)) { + } else if (mimeType.contains(FileUtils.MIME_TYPE_IMAGE)) { bm = MediaStore.Images.Thumbnails.getThumbnail( resolver, id, From 1dfdc87abd8fc51cf83d003a4cd3c8f8e3225b8a Mon Sep 17 00:00:00 2001 From: krishna Date: Thu, 11 Jan 2018 15:33:16 +0530 Subject: [PATCH 2/3] get path for all types of google drive file --- .../com/ipaulpro/afilechooser/utils/FileUtils.java | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java index de5ae66..6a264f3 100644 --- a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java +++ b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java @@ -334,7 +334,7 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) { String filePath = getDataColumn(context, uri, null, null); if (filePath == null) { - filePath = getDriveImageFilePath(context, uri); + filePath = getDriveFilePath(context, uri); } return filePath; } @@ -346,15 +346,15 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) { return null; } - private static String getDriveImageFilePath(Context context, Uri uri) throws IOException { + private static String getDriveFilePath(Context context, Uri uri) throws IOException { ContextWrapper ctr = new ContextWrapper(context.getApplicationContext()); // path to /data/data/yourapp/app_data/imageDir - File directory = ctr.getDir("imageDir", Context.MODE_PRIVATE); + File directory = ctr.getDir("tempFilesDir", Context.MODE_PRIVATE); // Create imageDir - File driveImageFile = new File(directory, "tempDriveImage.jpg"); + File driveFile = new File(directory, "tempDriveFile"); FileOutputStream fs = null; InputStream is = context.getContentResolver().openInputStream(uri); - fs = new FileOutputStream(driveImageFile); + fs = new FileOutputStream(driveFile); int n; byte[] buffer = new byte[1024]; if (is == null) throw new IOException(); @@ -362,7 +362,7 @@ private static String getDriveImageFilePath(Context context, Uri uri) throws IOE fs.write(buffer, 0, n); } fs.close(); - return driveImageFile.getAbsolutePath(); + return driveFile.getAbsolutePath(); } /** From 2350160017214c482197aae2301ae10123d4f2f0 Mon Sep 17 00:00:00 2001 From: krishna Date: Thu, 11 Jan 2018 15:54:30 +0530 Subject: [PATCH 3/3] remove lines added by mistake --- .../src/com/ipaulpro/afilechooser/utils/FileUtils.java | 6 +----- 1 file changed, 1 insertion(+), 5 deletions(-) diff --git a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java index 6a264f3..b450e0b 100644 --- a/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java +++ b/aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java @@ -296,11 +296,7 @@ else if (isDownloadsDocument(uri)) { final String id = DocumentsContract.getDocumentId(uri); final Uri contentUri = ContentUris.withAppendedId( Uri.parse("content://downloads/public_downloads"), Long.valueOf(id)); - String dataColumn = getDataColumn(context, contentUri, null, null); - if (dataColumn == null) { - ContextWra - } else - return getDataColumn(context, contentUri, null, null); + return getDataColumn(context, contentUri, null, null); } // MediaProvider else if (isMediaDocument(uri)) {