Android gets thumbnails of images and videos

Source: Internet
Author: User

Retrieve image thumbnails:

Java code


  1. Byte [] imagebyte = getimagefromurl (urlpath. Trim ());
  2. // The following is to convert the image into a thumbnail and then load it
  3. Bitmapfactory. Options = new bitmapfactory. Options ();
  4. Options. injustdecodebounds = true;
  5. Bitmapfactory. decodebytearray (imagebyte, 0, imagebyte. length, <span style = "background-color: RGB (255,255,255);"> options </span>); <span style = "line-Height: 25px; font-size: 14px;
    White-space: normal; "> // The returned bitmap is empty </span>
Byte [] imagebyte = getimagefromurl (urlpath. trim (); // The following is to convert the image into a thumbnail and then load bitmapfactory. options = new bitmapfactory. options (); options. injustdecodebounds = true; bitmapfactory. decodebytearray (imagebyte, 0, imagebyte. length, options); // The returned bitmap is null.
Java code


  1. Options. injustdecodebounds = false;
  2. Int be = (INT) (options. outheight/(float) 200 );
  3. If (be <= 0 ){
  4. Be = 1;
  5. }
  6. Options. insamplesize = be;
  7. Return bitmapfactory. decodebytearray (imagebyte,
    0, imagebyte. length, options); // returns a thumbnail.
Options. injustdecodebounds = false; int be = (INT) (options. outheight/(float) 200); If (be <= 0) {Be = 1;} options. insamplesize = be; return bitmapfactory. decodebytearray (imagebyte, 0, imagebyte. length, options); // returns a thumbnail.

Get the video thumbnail:

/**

* Obtain the specified video thumbnail Based on the video URI address.

* @ Param cr

* @ Param URI indicates the URI of the local video.

* @ Return returns bitmap data.

*/

Public static bitmap getvideothumbnail (contentresolver Cr, Uri URI ){

Java code


  1. Bitmap bitmap = NULL;
  2. Bitmapfactory. Options = new bitmapfactory. Options ();
  3. Options. indither = false;
  4. Options. inpreferredconfig = bitmap. config. argb_8888;
  5. Cursor cursor = Cr. Query (Uri, new string [] {mediastore. Video. Media. _ id },
    Null, null,
    Null );
  6. If (cursor = NULL | cursor. getcount () =
    0 ){
  7. Return NULL;
  8. }
  9. Cursor. movetofirst ();
  10. String videoid = cursor. getstring (cursor. getcolumnindex (mediastore. Video. Media. _ id ));
    // Image ID in image table. s
  11. If (videoid = NULL ){
  12. Return NULL;
  13. }
  14. Cursor. Close ();
  15. Long videoidlong = long. parselong (videoid );
  16. Bitmap = mediastore. Video. thumbnails. getthumbnail (Cr, videoidlong, images. thumbnails. micro_kind, options );
  17. Return bitmap;
  18. }
Bitmap bitmap = NULL; bitmapfactory. options = new bitmapfactory. options (); options. indither = false; options. inpreferredconfig = bitmap. config. argb_8888; cursor = CR. query (Uri, new string [] {mediastore. video. media. _ id}, null); If (cursor = NULL | cursor. getcount () = 0) {return NULL;} cursor. movetofirst (); string videoid = cursor. getstring (cursor. getcolumnindex (mediastore. video. media. _ id); // image ID in image table. sif (videoid = NULL) {return NULL;} cursor. close (); long videoidlong = long. parselong (videoid); bitmap = mediastore. video. thumbnails. getthumbnail (Cr, videoidlong, images. thumbnails. micro_kind, options); Return bitmap ;}

/**

* Obtain the specified video thumbnail Based on the video address path in the mobile phone.

* @ Param cr

* @ Param filename local video address

* @ Return returns bitmap data.

*/

Java code


  1. Public static bitmap getvideothumbnail (contentresolver Cr, Uri URI ){
  2. Bitmap bitmap = NULL;
  3. Bitmapfactory. Options = new bitmapfactory. Options ();
  4. Options. indither = false;
  5. Options. inpreferredconfig = bitmap. config. argb_8888;
  6. Cursor cursor = Cr. Query (Uri, new string [] {mediastore. Video. Media. _ id },
    Null, null,
    Null );
  7. If (cursor = NULL | cursor. getcount () =
    0 ){
  8. Return NULL;
  9. }
  10. Cursor. movetofirst ();
  11. String videoid = cursor. getstring (cursor. getcolumnindex (mediastore. Video. Media. _ id ));
    // Image ID in image table. s
  12. If (videoid = NULL ){
  13. Return NULL;
  14. }
  15. Cursor. Close ();
  16. Long videoidlong = long. parselong (videoid );
  17. Bitmap = mediastore. Video. thumbnails. getthumbnail (Cr, videoidlong, images. thumbnails. micro_kind, options );
  18. Return bitmap;
  19. }
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.