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

Whatsapp file sharing and local download file sharing bug fixed #96

Open
wants to merge 2 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
98 changes: 66 additions & 32 deletions aFileChooser/src/com/ipaulpro/afilechooser/utils/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.TextUtils;
import android.util.Log;
import android.webkit.MimeTypeMap;

Expand All @@ -38,16 +39,19 @@
import java.text.DecimalFormat;
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 +68,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 @@ -203,22 +207,32 @@ public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".equals(uri.getAuthority());
}

/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Whatsapp.
* @author nieldeokar
*/
public static boolean isWhatsappUri(Uri uri) {
return "com.whatsapp.provider.media".equals(uri.getAuthority());
}

/**
* 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.
* @param columnName (Optional) name of the column default is _data
* @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,String columnName) {

Cursor cursor = null;
final String column = "_data";
final String column = columnName == null ? "_data" : columnName;
final String[] projection = {
column
};
Expand All @@ -231,7 +245,7 @@ public static String getDataColumn(Context context, Uri uri, String selection,
DatabaseUtils.dumpCursor(cursor);

final int column_index = cursor.getColumnIndexOrThrow(column);
return cursor.getString(column_index);
return column.equals("_data") ? cursor.getString(column_index) : uri.toString() + "/" + cursor.getString(column_index);
}
} finally {
if (cursor != null)
Expand All @@ -247,12 +261,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 +279,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 All @@ -290,12 +304,22 @@ else if (isExternalStorageDocument(uri)) {
}
// DownloadsProvider
else if (isDownloadsDocument(uri)) {

final String id = DocumentsContract.getDocumentId(uri);
final Uri contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
Uri contentUri;
try {
contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
} catch (NumberFormatException e) {

if (!TextUtils.isEmpty(id)) {
if (id.startsWith("raw:")) {
return id.replaceFirst("raw:", "");
}
}
contentUri = ContentUris.withAppendedId(Uri.parse("content://downloads/public_downloads"), Long.valueOf(id));
}

return getDataColumn(context, contentUri, null, null);

return getDataColumn(context, contentUri, null, null, null);
}
// MediaProvider
else if (isMediaDocument(uri)) {
Expand All @@ -313,11 +337,11 @@ else if (isMediaDocument(uri)) {
}

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

return getDataColumn(context, contentUri, selection, selectionArgs);
return getDataColumn(context, contentUri, selection, selectionArgs, null);
}
}
// MediaStore (and general)
Expand All @@ -327,23 +351,34 @@ else if ("content".equalsIgnoreCase(uri.getScheme())) {
if (isGooglePhotosUri(uri))
return uri.getLastPathSegment();

return getDataColumn(context, uri, null, null);
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
if (isWhatsappUri(uri)) {

String path = getDataColumn(context, uri, null, null, "_display_name");

if (path != null) {
File file = new File(path);
if (!file.canRead()) {
return null;
}
}
return path;
}
// File
else if ("file".equalsIgnoreCase(uri.getScheme())) {
return uri.getPath();
}
}
return null;
}


/**
* 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 +483,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