How mediascanner generates and saves thumbnail

Source: Internet
Author: User

From: http://blog.csdn.net/qikaibinglan/article/details/6130589

This article briefly studies how mediascanner generates and saves thumbnail, and provides the code to quickly query thumbnail of an image,


1. When you manually execute Dev tools/media plugin or insert an SD card, mediascannerservice is started to update the media file to the media database,

If it is an image file, thumbnail is generated at the same time.

This part of the code is in packages/providers/mediaprovider

2. Structure of the image and thumbnail table in the database

ADB shell enters the directory/data/COM. Android. providers. Media/databases

We can see two database internal. DB external-xx.db, where external. DB stores media information in the SD card

We can check it with sqloud.

Sqlite3 external-xx.db

. Table

We can see tables related to multimedia files. We need to care about the tables images and thumbnails.

Images table structure:

View plaincopy to clipboardprint?
Images (_ id integer primary key, _ Data Text, _ s
Ize integer, _ display_name text, mime_type text, title text, date_added integer, date
_ Modified integer, description text, picasa_id text, isprivate integer, latitude Dou
Ble, longpolling double, datetaken integer, orientation integer, mini_thumb_magic inte
GER, bucket_id text, bucket_display_name text)

Thumbnails table structure:

View plaincopy to clipboardprint?
Thumbnails (_ id integer primary key ,_
Data Text, image_id integer, kind integer, width integer, height integer)

Let's take a look at the actual data on my simulator:

Select * from images;

View plaincopy to clipboardprint?
4 |/mnt/sdcard/Android/1289887991860.jpg | 40449 | 1289887991860.jpg | image/JPEG | 12898
87991860 | 1290054484 | 1290064872 | 1290064872000 | 0 |-5876966875320966333 | 1801299
020 | android
8 |/mnt/sdcard/20103476062805_hahaha.bmp | 65590 | 20103476062805_hahaha.bmp | image/X-
MS-BMP | 20103476062805_hahaha | 1290752887 | 1290752885 | 1290752885000 | 536666747
8445081544 |-1595679508 | sdcard
9 |/mnt/sdcard/20101_6064005_20101_6062805_hahaha.bmp | 65590 | 20101_6064005_2010
20176062805_haha.bmp | image/X-MS-BMP | 20101_6064005_20101_6062805_hahaha | 129075
3607 | 1290753606 | 1290753606000 | 2418262411059016544 |-1595679508 | sdcard

Select * From thumbnails;

View plaincopy to clipboardprint?
4 |/mnt/sdcard/dcim/. thumbnails/1290054484766.jpg | 4 | 1 | 400 | 300
6 |/mnt/sdcard/dcim/. thumbnails/1290752887313.jpg | 8 | 1 | 128 | 128
7 |/mnt/sdcard/dcim/. thumbnails/1290753608349.jpg | 9 | 1 | 128 | 128

3. Now we can see what the dcim/. thumbnails directory under the SD card is used. mediascanner places the generated thumbnail here,

Maintain the correspondence between thumbnail and images in the database:

The first column in the images table is the image ID, corresponding to the image_id item in the thumbnail table

Path of the second column of the image in the images table. The second column of the thumbnail table is the path of thumbnail.

4. From this result, we can query the thumbnail of an image (I think there may be a better way)

Current file path ---> query images table to get _ id ---> query thumbnials table with _ id = image_id to get the path of thumbrix

5. Based on the _ ID of the file path file_path:

View plaincopy to clipboardprint?
String [] projection2 = {
"_ Id ",
// "_ DATA"
};
Uri uri2 = mediastore. Images. Media. getcontenturi ("external ");
String where = string. Format (
"_ DATA = '% S '",
File_path );
Cursor C2 = media. Query (getcontentresolver (), uri2,
Projection2, where, null );
Long image_id = c2.getlong (c2.getcolumnindexorthrow ("_ id "));

6. query the thumbnail path based on image_id

View plaincopy to clipboardprint?
String [] projection = {
"_ Data ",
// "Image_id"
};

Cursor c = thumbnails. queryminithumbnail (getcontentresolver (), image_id,
Thumbnails. mini_kind, projection );

Thumbnail_path = C. getstring (C. getcolumnindexorthrow ("_ DATA "));

7. In this way, the corresponding thumbnail is obtained from the current file, provided that mediascanner has scanned you. Of course, you can also start the scan yourself in the code.

Then pass the thumnail path to the imageview you used to display.

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.