[Android] thumbnail thumbnails

Source: Internet
Author: User

In Android, multimedia files (videos and images) all have thumbnails. In many applications, we need to obtain these thumbnails. For example, if you are recently working on an application similar to an album, you need to scan the image in the album, obtain its thumbnail, and use the gridview to display the thumbnail. After you click it, you need to obtain its original image, the related requirements are as follows:

1) obtain the thumbnail (The question is: Do all images and videos have thumbnails ?);

2) associate the thumbnail with the original image;

 

About 1 ):

The following method is used:

1 Options options=new Options();2 options.inSampleSize=32;3 Bitmap bitmap=BitmapFactory.decodeFile(url, options);4 Drawable drawable=new BitmapDrawable(bitmap);

But there is a problem with this method: it is difficult to grasp the size of insamplesize (here 32 is already very exaggerated, but from the perspective of album, the number of images is in the unit, the problem I encountered was that my images were not of the same size. I downloaded some small images from the Internet and sent them to my mobile phone. The original Big Picture setting was no problem, but the thumbnail is obviously too small ). I 'd like to know if the Android system can help me scale it to a proper size (another reason is that I have to write a function myself to traverse all the folders and find the file in the image format by skipping the thumbnail, this greatly reduces program performance ). The following study extracts thumbnails directly.

You can directly access the thumbnails of extracted images and videos:

1 android.provider.MediaStore.Images.Thumbnails2 android.provider.MediaStore.Video.Thumbnails

The two databases can be used to query the thumbnails (this is contentprovider. For details, see [Android] contentprovider)

I have been very interested in Android data storage in this regard. I really want to know how it stores the data and what I can get from the data. Today I exported the database, I checked the storage and explained it below:

You can first check the/data/COM. Android. provider. Media Directory under databases: external-f042911.db and Internal. DB,

After selecting, click the icon with an arrow in the upper right corner to export the database to the computer folder, and then download an SQLite database to view the software. I am under MAC, the downloaded software is called mesasqlite, and then the database is opened. Let's take a look:

The first is the external-f042911.db database, which has a lot of tables, as follows:

There are obviously two thumbnails tables. I am viewing the thumbnails table. The following table content:

There are a total of 11 thumbnails. We can see that each thumbnail has a _ id and an image_id (which is the key ?).

The expected table is images. What is this table store? As shown in the following figure, the table header is very long. I can intercept the two ends:

This is the first one. What can I see ?? First of all, it is of course the same _ id field. Is this a good match with the above ?? Secondly, we can find attributes such as the image size, type, and name. The most important thing is its absolute physical path!

This is the second one. It is the attribute of the following section of the table. The most important column in this figure is "bucket_display_name ", we can find that the folder where the image is stored is recorded here (this is required for album creation !)

Then I can view the two tables:

The first is the albums table. What is in this table?

This is a surprise to me. qiduo is a folder I created. I stored some files and an AMR recording file in it, so I was curious, how does Android determine a folder as album?

Let's look at another table: audio_meta, which is even more surprising:

In this magic table, I recorded my recorded AMR audio. What do you see? There are absolute paths, sizes, and sometimes lengths! Amazing!

So now I have the following questions:

1) does all multimedia files (I mean videos and images) have thumbnails?

2) How does the thumbnail correspond to the original image?

3) How is album defined?

First answer question 2): Actually question 2) write a program to verify it. The fact is not what we guess, the two _ id fields correspond,:

The thumbnails and images tables are associated with images. _ id through thumbnails. image_id. The ing relationship between images in the thumbnails table and those in the images table can be found through the _ id of images. The position of the original image is the value of the _ data field in the images table.

About 1st), we seem to want to know how the thumbnail is implemented? There is a class: mediascanner (detailed to be visible: http://blog.csdn.net/zqiang_55/article/details/7060171) This class is responsible for scanning all the pictures and storing the pictures into mediastore (mediascannernerverer is used to receive the task, after it receives the broadcast, starts mediaservice for scanning. Complicated look ...) Insert a picture:

Mediascanner can be manually controlled. In Android, three events have been customized to trigger mediascanner to scan Disk Files: action_boot_completed, action_media_mounted, and action_media_scanner_scan_file. Here, action_boot_completed is the message sent after the system starts, action_media_mounted is the message triggered by the plug-in event, and action_media_scanner_scan_file is generally after some file operations, A message manually sent by a developer to rescan a multimedia file. The sendbroadcast function is used to send messages. For example, to broadcast an action_media_mounted message:

1 sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"+ Environment.getExternalStorageDirectory())));

It can be seen from the above that a broadcast (transfer the corresponding scan requirements) is sent to trigger a re-scan disk event, so we can guess that the system must have a broadcast receiver (when and where to register ?), After receiving the broadcast message, start mediascannerservice with the corresponding parameters. Mediascannerservice calls a public class mediascanner to handle real work. Mediascannerreceiver maintains two types of scan directories: internal volume (internal volume) pointing to $ (android_root)/media. the other is that the external volume points to $ (external_storage), and the scan position can be modified (generally, the external volume is sdcard by default, modify the inand path name based on the driver name ).

So for question 1), if you store images but do not start disk scanning, it will cause incomplete thumbnails.

About the third point to be continued ~

Code:

1) obtain the thumbnail:

 1 cr = getContentResolver(); 2 String[] projection = { Thumbnails._ID, Thumbnails.IMAGE_ID, Thumbnails.DATA }; 3 Cursor cursor = cr.query(Thumbnails.EXTERNAL_CONTENT_URI, projection, null, null, null); 4 getColumnData(cursor); 5  6 private void getColumnData(Cursor cur) { 7     if (cur.moveToFirst()) { 8         int _id; 9         int image_id;10         String image_path;11         int _idColumn = cur.getColumnIndex(Thumbnails._ID);12         int image_idColumn = cur.getColumnIndex(Thumbnails.IMAGE_ID);13         int dataColumn = cur.getColumnIndex(Thumbnails.DATA);14 15         do {16             // Get the field values17             _id = cur.getInt(_idColumn);18             image_id = cur.getInt(image_idColumn);19             image_path = cur.getString(dataColumn);20 21             // Do something with the values.22             Log.i(TAG, _id + " image_id:" + image_id + " path:"23                         + image_path + "---");24             HashMap<String, String> hash = new HashMap<String, String>();25             hash.put("image_id", image_id + "");26             hash.put("path", image_path);27             list.add(hash);28 29         } while (cur.moveToNext());30 31      }32 }

2) Get the actual image

1 string columns [] = new string [] {media. data, media. _ id, media. title, media. display_name, media. size}; 2 // get a cursor 3 cursor = This. getcontentresolver (). query (media. external_content_uri, columns, null); 4 // obtain the index of the specified Column 5 photoindex = cursor. getcolumnindexorthrow (media. data); 6 photonameindex = cursor. getcolumnindexorthrow (media. display_name); 7 photoidindex = cursor. getcolumnindexorthrow (media. _ id); 8 phototitleindex = cursor. getcolumnindexorthrow (media. title); 9 photosizeindex = cursor. getcolumnindexorthrow (media. size); 10 // obtain the total number of images 11 totalnum = cursor. getcount ();

3) The relationship between the thumbnail and the original image

 1 OnItemClickListener listener = new OnItemClickListener() {    2     @Override   3     public void onItemClick(AdapterView<?> parent, View view, int position, long id) {   4         // TODO Auto-generated method stub   5         String image_id = list.get(position).get("image_id");   6         Log.i(TAG, "---(^o^)----" + image_id);   7        String[] projection = { Media._ID, Media.DATA };   8        Cursor cursor = cr.query(Media.EXTERNAL_CONTENT_URI, projection,   9                              Media._ID + "=" + image_id, null, null);  10        if (cursor != null) {  11            cursor.moveToFirst();  12            String path = cursor.getString(cursor.getColumnIndex(Media.DATA));  13            Intent intent = new Intent(ThumbnailActivity.this, ImageViewer.class);  14            intent.putExtra("path", path);  15            startActivity(intent);  16        } else {  17            Toast.makeText(ThumbnailActivity.this, "Image doesn't exist!",  18                                  Toast.LENGTH_SHORT).show();  19           }20       }  21 };

For specific thumbnails, you can use getthumbnail (contentresolver Cr, long origid, int kind, bitmapfactory. options options) or getthumbnail (contentresolver Cr, long origid, long groupid, int kind, bitmapfactory. options options). The two methods return the bitmap type, while the thumbnail resolution can be extracted from the height and width fields. on Android, the thumbnails are divided into two types.
The kind fields are obtained by using the micro_kind and mini-kind modes, respectively. The former has lower resolution. In this way, we can directly call the system thumbnails when obtaining an image preview of the file system, instead of re-computing it by ourselves.

The thumbnail is saved in the dcim directory of the SD card. The. thumbnails in the directory is an image, and. video_thumbnails is a video. The two folders are hidden.

A thumbnail thumbnailutils class is added from android2.2, which is located in the framework's Android. media. the location of thumbnailutils can help us obtain thumbnails of video or image files in the system from mediaprovider. This class provides three static methods that can be called directly.

1. Static bitmap createvideothumbnail (string filepath, int kind)
// Obtain the thumbnail of a video file. The first parameter is the location of the video file, for example,/sdcard/android123.3gp. The second parameter can be mini_kind or micro_kind, which is ultimately related to the resolution.

2. Static bitmap extractthumbnail (bitmap source, int width, int height, int options)
// Scale down bitmap directly. The last parameter is defined as options_recycle_input to recycle resources.

3. Static bitmap extractthumbnail (bitmap source, int width, int height)
// This is the same as the above method, and there is no options Option

Http://www.cnblogs.com/lqminn/archive/2012/10/16/2726583.html
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.