Android read local photo and video album instance code

preface

Local photos or videos are often selected in projects. It takes too much time to scan the entire SD card. In fact, the Android system has saved the multimedia file information (file name, type, size, etc.) in the whole device to the database when it is started, and then provided the API contentprivider to manage the database, We can use contentprivider to get all photos and videos.

Contentprivider first met

Let's see where the managed database is first

Data / data / Directory: there are many such folders (calendar, contacts, download management, multimedia, etc.)

The photos and videos we need are under media. Go in and have a look. Go in and find the database, and then open external.db to see multiple tables (audio, files, logs, images, videos, etc.)

Photo album

Then it is OK to get photos and directly read the images database through the ContentProvider. Here, start the working thread to read all. JPEG and. PNG images, and attach the code segment:

There are four points to note:

Video album

Obtaining video files is basically the same as above, but just change the query conditions. In practice, there is a problem: obtaining video covers.

First, the video cover thumbnail is in the videoThumbnails database, and the photo thumbnail is in thumbnails. The corresponding local SD card is in the sdcard / DCIM /. Thumbnails / folder (some devices may be different)

PS: this folder is hidden, so you know why the storage space of your mobile phone is getting smaller and smaller. The thumbnails of photos are all here... Very, very much

In practice, it is found that the newly recorded video cover cannot be read. You need to manually call a method to generate the cover, and then you can read it in videoThumbnails:

reference resources: http://stackoverflow.com/questions/27903264/how-to-get-the-video-thumbnail-path-and-not-the-bitmap

And the cover page and video here are not in the same database and need to be read in two cursors

Here I get the MP4 format video of the whole SD. The code segment is as follows:

Postscript

In fact, Android has provided an API called cursorloader to do this. There is no need to manually new worker threads. It is very simple to use. If necessary, the above code can be modified.

The above is the whole content of this article. I hope it will be helpful to your study, and I hope you can support programming tips.

The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>