Android how can i get thumb image of video file from gallery -
i need show selected video file gallery in listview. have fetched uri of video file not able thumb image of video file. here code have used fetching image of video file uri, causes app crash. there other way image of video file..? please assist me if can achieved in other way....
public static string getfilemetadata(context context,string uri){ uri queryuri = uri.parse(uri); // relevant columns use later. string[] projection = { mediastore.files.filecolumns._id, mediastore.files.filecolumns.data, mediastore.files.filecolumns.date_added, mediastore.files.filecolumns.media_type, mediastore.files.filecolumns.mime_type, mediastore.files.filecolumns.title }; // return video , image metadata. string selection = mediastore.files.filecolumns.media_type + "=" + mediastore.files.filecolumns.media_type_image + " or " + mediastore.files.filecolumns.media_type + "=" + mediastore.files.filecolumns.media_type_video; cursorloader cursorloader = new cursorloader( context, queryuri, projection, selection, null, // selection args (none). mediastore.files.filecolumns.date_added + " desc" // sort order. ); cursor cursor = cursorloader.loadinbackground(); int columnindex = cursor.getcolumnindex(mediastore.video.thumbnails.data); log.d("mytag","***************** : " + cursor.getstring(columnindex)); cursor.close(); return null; }
from video file uri first have filepath of video file filepath can generate video thumb.
you can thumbnail using createvideothumbnail method of thumbnailutils class this.
video_imageview.setimagebitmap(thumbnailutils.createvideothumbnail(selectedvideofilepath, mediastore.video.thumbnails.full_screen_kind));
in selectedvideofilepath video file path. can file path using this.
string selectedvideofilepath = getfilepathfromdevice.getpath(this, videofileuri);
you can use getfilepathfromdevice class filepath given uri.
getfilepathfromdevice.java
@suppresslint("newapi") public final class getfilepathfromdevice { /** * file path uri * * @param context context of activity * @param uri uri of file * @return path of given uri */ public static string getpath(final context context, final uri uri) { final boolean iskitkat = build.version.sdk_int >= build.version_codes.kitkat; // documentprovider if (iskitkat && documentscontract.isdocumenturi(context, uri)) { // externalstorageprovider if (isexternalstoragedocument(uri)) { final string docid = documentscontract.getdocumentid(uri); final string[] split = docid.split(":"); final string type = split[0]; if ("primary".equalsignorecase(type)) { return environment.getexternalstoragedirectory() + "/" + split[1]; } } // 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)); return getdatacolumn(context, contenturi, null, null); } // mediaprovider else if (ismediadocument(uri)) { final string docid = documentscontract.getdocumentid(uri); final string[] split = docid.split(":"); final string type = split[0]; uri contenturi = null; 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]}; return getdatacolumn(context, contenturi, selection, selectionargs); } } // mediastore (and general) else if ("content".equalsignorecase(uri.getscheme())) { // return remote address if (isgooglephotosuri(uri)) return uri.getlastpathsegment(); return getdatacolumn(context, uri, null, null); } // file else if ("file".equalsignorecase(uri.getscheme())) { return uri.getpath(); } return null; } public static string getdatacolumn(context context, uri uri, string selection, string[] selectionargs) { cursor cursor = null; final string column = "_data"; final string[] projection = {column}; try { cursor = context.getcontentresolver().query(uri, projection, selection, selectionargs, null); if (cursor != null && cursor.movetofirst()) { final int index = cursor.getcolumnindexorthrow(column); return cursor.getstring(index); } } { if (cursor != null) cursor.close(); } return null; } public static boolean isexternalstoragedocument(uri uri) { return "com.android.externalstorage.documents".equals(uri.getauthority()); } public static boolean isdownloadsdocument(uri uri) { return "com.android.providers.downloads.documents".equals(uri.getauthority()); } public static boolean ismediadocument(uri uri) { return "com.android.providers.media.documents".equals(uri.getauthority()); } public static boolean isgooglephotosuri(uri uri) { return "com.google.android.apps.photos.content".equals(uri.getauthority()); } }
i hope helps!
Comments
Post a Comment