Skip to content

Instantly share code, notes, and snippets.

@bGorle
Last active August 29, 2015 14:23
Show Gist options
  • Save bGorle/a503e458caac432e80eb to your computer and use it in GitHub Desktop.
Save bGorle/a503e458caac432e80eb to your computer and use it in GitHub Desktop.
Get Attachment details from Uri
import java.io.Serializable;
import java.lang.Override;
import java.lang.String;
import android.net.Uri;
public class Attachment implements Serializable{
private static final long serialVersionUID = 5579721311992534823L;
private long id;
private String serverId;
private String uri;
private String name;a
private int attachmentType;
private String attachmentUrl;
public static final short TYPE_IMAGE = 1;
public static final short TYPE_VIDEO = 2;
public static final short TYPE_NETWORK_IMAGE_URL = 3;
public static final short TYPE_NETWORK_VIDEO_URL = 4;
public Attachment() {
}
public long getId() {
return id;
}
public void setId(long id) {
this.id = id;
}
public String getServerId() {
return serverId;
}
public void setServerId(String serverId) {
this.serverId = serverId;
}
public String getUri() {
return uri;
}
public void setUri(String url) {
this.uri = url;
}
public void setUri(Uri uri) {
this.uri = uri.toString();
}
public int getType() {
return attachmentType;
}
public void setType(int type) {
this.attachmentType = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getAttachmentUrl() {
return attachmentUrl;
}
public void setAttachmentUrl(String attachmentUrl) {
this.attachmentUrl = attachmentUrl;
}
@Override
public String toString() {
return "Attachment{" +
"id=" + id +
", serverId='" + serverId + '\'' +
", uri='" + uri + '\'' +
", name='" + name + '\'' +
", attachmentType=" + attachmentType +
", attachmentUrl='" + attachmentUrl + '\'' +
'}';
}
}
import android.annotation.SuppressLint;
import android.content.ContentUris;
import android.content.Context;
import android.database.Cursor;
import android.database.DatabaseUtils;
import android.net.Uri;
import android.os.Build;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.provider.MediaStore.MediaColumns;
import android.util.Log;
public class FileAttachments {
public static final int EXTERNAL_STORAGE_DOC = 1;
public static final int DOWNLOAD_DOC = 2;
public static final int MEDIA_DOC = 3;
public static final int GOOGLE_PHOTO = 4;
public static final int DRIVE_PHOTO = 5;
public static final int DEFAULT_CONTENT = 6;
public static final int EXTERNAL_IMAGE = 7;
public static final int EXTERNAL_VIDEO = 8;
public static final int EXTERNAL_AUDIO = 9;
public static final int FILE_URI = 10;
static final String TAG = "ImageFilePath";
/**
* Method for return file path of Gallery image
*
* @param context
* @param uri
* @return path of the selected image <span id="IL_AD2" class="IL_AD">file
* from</span> gallery
*/
@SuppressLint("NewApi")
public static Attachment getAttachment(final Context context, final Uri uri) {
final boolean isKitKat = Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT;
// checking uri is belongs to KITKAT version
if (isKitKat && DocumentsContract.isDocumentUri(context, uri)) {
Log.d(TAG, "is kitkat");
int ch = getUriCode(uri);
Uri contentUri = null;
switch (ch) {
case EXTERNAL_STORAGE_DOC:
break;
case DOWNLOAD_DOC:
final String id = DocumentsContract.getDocumentId(uri);
contentUri = ContentUris.withAppendedId(
Uri.parse("content://downloads/public_downloads"),
Long.valueOf(id));
return prepareAttachment(context, contentUri, null, null);
case MEDIA_DOC:
Log.d(TAG, "media doc");
final String docId = DocumentsContract.getDocumentId(uri);
final String[] split = docId.split(":");
final String type = split[0];
if ("image".equals(type)) {
contentUri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
} else if ("video".equals(type)) {
contentUri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
} else if ("audio".equals(type)) {
contentUri = MediaStore.Audio.Media.EXTERNAL_CONTENT_URI;
}
final String selection = "_id=?";
final String[] selectionArgs = new String[] {
split[1] };
Attachment attachment = prepareAttachment(context, contentUri, selection,
selectionArgs);
attachment.setUri(uri);
return attachment;
}
} else {
int ch2 = getSchemeCode(uri);
Attachment attachment = null;
switch (ch2) {
case DEFAULT_CONTENT:
Log.d(TAG, "DEFAULT_CONTENT : " + getUriCode(uri));
switch (getUriCode(uri)) {
case GOOGLE_PHOTO:
case DRIVE_PHOTO:
Cursor cursor = context.getContentResolver().query(uri,
null, null, null, null);
cursor.moveToFirst();
DatabaseUtils.dumpCursor(cursor);
Log.d(TAG, "Google photo doc");
attachment = new Attachment();
attachment.setName(cursor.getString(cursor.getColumnIndex(MediaColumns.DISPLAY_NAME)));
attachment.setAttachmentUrl(uri.getLastPathSegment());
attachment.setType(Utils.getNetworkAttachmentType(cursor.getString(cursor.getColumnIndex(MediaColumns.MIME_TYPE))));
attachment.setUri(uri.toString());
return attachment;
case EXTERNAL_IMAGE:
case EXTERNAL_VIDEO:
case EXTERNAL_AUDIO:
return prepareAttachment(context, uri, null, null);
}
case FILE_URI:
attachment = new Attachment();
attachment.setName(uri.getLastPathSegment());
attachment.setUri(uri.toString());
attachment.setAttachmentUrl(uri.getPath());
attachment.setType(Utils.getAttachmentType(Utils.getMimeType(uri.getLastPathSegment())));
return attachment;
}
}
return null;
}
public static int getUriCode(Uri uri) {
Log.d(TAG, "getUriCode" + uri.toString());
return "com.android.externalstorage.documents".equals(uri
.getAuthority()) ? EXTERNAL_STORAGE_DOC
: "com.android.providers.downloads.documents".equals(uri
.getAuthority()) ? DOWNLOAD_DOC
: "com.android.providers.media.documents".equals(uri
.getAuthority()) ? MEDIA_DOC
: "com.google.android.apps.photos.content"
.equals(uri.getAuthority()) ? GOOGLE_PHOTO
: "com.google.android.apps.docs.files"
.equals(uri.getAuthority()) ? DRIVE_PHOTO
: uri.toString().contains("image") ? EXTERNAL_IMAGE
: uri.toString().contains("video") ? EXTERNAL_VIDEO
: uri.toString().contains("audio") ? EXTERNAL_AUDIO
: -1;
}
public static int getSchemeCode(Uri uri) {
return "content".equalsIgnoreCase(uri.getScheme()) ? DEFAULT_CONTENT
: "file".equalsIgnoreCase(uri.getScheme()) ? FILE_URI
: -1;
}
/**
* @param uri The Uri to check.
* @return Whether the Uri authority is Google Photos.
*/
public static boolean isGooglePhotosUri(Uri uri) {
return "com.google.android.apps.photos.content".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
* (<span id="IL_AD11" class="IL_AD">Optional</span>) 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.
*/
public static Attachment prepareAttachment(Context context, Uri uri,
String selection, String[] selectionArgs) {
Log.d(TAG, "Datacolumn doc");
Cursor cursor = null;
try {
cursor = context.getContentResolver().query(uri, null,
selection, selectionArgs, null);
if (cursor != null && cursor.moveToFirst()) {
DatabaseUtils.dumpCursor(cursor);
Attachment attachment = new Attachment();
attachment.setName(cursor.getString(cursor
.getColumnIndex(MediaColumns.DISPLAY_NAME)));
attachment.setAttachmentUrl(cursor.getString(cursor
.getColumnIndex(MediaColumns.DATA)));
attachment.setUri(uri.toString());
String mimetype = null;
if(! (cursor
.getColumnIndex(MediaColumns.MIME_TYPE) == -1)) {
mimetype = cursor.getString(cursor
.getColumnIndex(MediaColumns.MIME_TYPE));
} else {
mimetype = cursor.getString(cursor
.getColumnIndex("mimetype"));
}
attachment.setType(Util.getAttachmentType(mimetype));
return attachment;
}
} finally {
if (cursor != null)
cursor.close();
}
return null;
}
}
/**
* Return an integer basing on attachment type.
*
* @param str
* @return
*/
public static int getNetworkAttachmentType(String str) {
if (str.contains("image")) {
return Attachment.TYPE_NETWORK_IMAGE_URL;
} else if (str.contains("video")) {
return Attachment.TYPE_NETWORK_VIDEO_URL;
}
return -1;
}
/**
* Return an integer basing on attachment type.
*
* @param str
* @return
*/
public static int getAttachmentType(String str) {
if (str.contains("image")) {
return Attachment.TYPE_IMAGE;
} else if (str.contains("video")) {
return Attachment.TYPE_VIDEO;
}
return -1;
}
/**
* Returns the mime type from url basing on the extension.
*
* @param url
* @return
*/
public static String getMimeType(String url) {
String type = null;
String extension = url.substring(url.lastIndexOf('.'));
extension = extension.replace(".", "");
Log.d("", "extension : " + extension);
if (extension != null) {
MimeTypeMap mime = MimeTypeMap.getSingleton();
type = mime.getMimeTypeFromExtension(extension);
}
return type;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment