Skip to content
This repository has been archived by the owner on Mar 30, 2022. It is now read-only.

Issue #90 fix: Not Working for Google Drive Uri #97

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
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
65 changes: 44 additions & 21 deletions aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand All @@ -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) {
Expand Down Expand Up @@ -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";
Expand Down Expand Up @@ -247,12 +249,12 @@ public static String getDataColumn(Context context, Uri uri, String selection,
* <br>
* 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) {

Expand All @@ -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;

Expand Down Expand Up @@ -294,7 +296,6 @@ 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);
}
// MediaProvider
Expand All @@ -313,7 +314,7 @@ else if (isMediaDocument(uri)) {
}

final String selection = "_id=?";
final String[] selectionArgs = new String[] {
final String[] selectionArgs = new String[]{
split[1]
};

Expand All @@ -327,7 +328,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 = getDriveFilePath(context, uri);
}
return filePath;
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
Expand All @@ -337,13 +342,32 @@ else if ("file".equalsIgnoreCase(uri.getScheme())) {
return null;
}

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("tempFilesDir", Context.MODE_PRIVATE);
// Create imageDir
File driveFile = new File(directory, "tempDriveFile");
FileOutputStream fs = null;
InputStream is = context.getContentResolver().openInputStream(uri);
fs = new FileOutputStream(driveFile);
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 driveFile.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) {
Expand Down Expand Up @@ -448,8 +472,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,
Expand Down