MediaStore usage example in Android

Source: Internet
Author: User
Package cn.com; import java. io. IOException; import android. app. activity; import android. content. contentResolver; import android. database. cursor; import android. media. exifInterface; import android. OS. bundle; import android. provider. mediaStore; public class MainActivity extends Activity {@ Override public void onCreate (Bundle savedInstanceState) {super. onCreate (savedInstanceState); setContentView (R. layout. main); // Test 1 // getVideosInfo (); // Test 2 // getPhotosInfo (); // Test 3 getAudiosInfo ();} // obtain all the video information on the device. private void getVideosInfo () {ContentResolver contentResolver = getContentResolver (); String [] videoColumns = new String [] {MediaStore. video. media. _ ID, MediaStore. video. media. DATA, MediaStore. video. media. TITLE, MediaStore. video. media. MIME_TYPE}; // both methods can be used // Cursor cursor = // this. managedQuery (MediaStore. video. media. EXTERNAL_CONTENT_URI, mediaColumns, null, null); Cursor cursor = contentResolver. query (MediaStore. video. media. EXTERNAL_CONTENT_URI, videoColumns, null, null); while (cursor. moveToNext () {String _ id = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. video. media. _ ID); String filePath = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. video. media. DATA); String title = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. video. media. TITLE); String mime_type = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. video. media. MIME_TYPE); System. out. println ("_ id =" + _ id); System. out. println ("title =" + title); System. out. println ("filePath =" + filePath); System. out. println ("mime_type =" + mime_type) ;}// obtain all the photo information on the device. private void getPhotosInfo () {ContentResolver contentResolver = getContentResolver (); string [] photoColumns = new String [] {MediaStore. images. media. _ ID, MediaStore. images. media. DATA, MediaStore. images. media. TITLE, MediaStore. images. media. MIME_TYPE, MediaStore. images. media. SIZE, MediaStore. images. media. ORIENTATION}; // either of the two methods can be // Cursor cursor = // this. managedQuery (MediaStore. images. media. EXTERNAL_CONTENT_URI, mediaColumns, null, null); Cursor cursor = contentResolver. query (MediaStore. images. media. EXTERNAL_CONTENT_URI, photoColumns, null); while (cursor. moveToNext () {String _ id = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. images. media. _ ID); String filePath = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. images. media. DATA); String title = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. images. media. TITLE); String mime_type = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. images. media. MIME_TYPE); String size = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. images. media. SIZE); // obtain the image rotation angle method-String orientation0 = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. images. media. ORIENTATION); System. out. println ("_ id =" + _ id); System. out. println ("size =" + size); System. out. println ("title =" + title); System. out. println ("filePath =" + filePath); System. out. println ("mime_type =" + mime_type); System. out. println ("first orientation0 =" + orientation0); try {ExifInterface exifInterface = new ExifInterface (filePath); String image_length = exifInterface. getAttribute (ExifInterface. TAG_IMAGE_LENGTH); String image_width = exifInterface. getAttribute (ExifInterface. TAG_IMAGE_WIDTH); String orientation1 = exifInterface. getAttribute (ExifInterface. TAG_ORIENTATION); String dateTime = exifInterface. getAttribute (ExifInterface. TAG_DATETIME); System. out. println ("image_length =" + image_length); System. out. println ("image_width =" + image_width); System. out. println ("dateTime =" + dateTime); // obtain the image rotation angle. method 2 // use the ExifInterface source code for analysis. // The switch (Integer. valueOf (orientation1) {case 1: System. out. println ("Second Rotation Angle =" + 0); break; case 2: // matrix. invert (matrix); break; case 3: // matrix. setRotate (180); System. out. println ("Second Rotation Angle =" + 180); break; case 4: // matrix. invert (matrix); // matrix. setRotate (180); System. out. println ("Second Rotation Angle =" + 180); break; case 5: // matrix. setRotate (90); // matrix. invert (matrix); System. out. println ("Second Rotation Angle =" + 90); break; case 6: // matrix. setRotate (90); System. out. println ("Second Rotation Angle =" + 90); break; case 7: // matrix. invert (matrix); // matrix. setRotate (90); System. out. println ("Second Rotation Angle =" + 90); break; case 8: // matrix. setRotate (270); System. out. println ("Second Rotation Angle =" + 270); break; default: break;} System. out. println ("xxxxxxxxxxxxxxxxxxxxx");} catch (IOException e) {e. printStackTrace () ;}}// obtain all the Audio Information on the device. private void getAudiosInfo () {ContentResolver contentResolver = getContentResolver (); string [] audioColumns = new String [] {MediaStore. audio. media. _ ID, MediaStore. audio. media. DATA, MediaStore. audio. media. TITLE, MediaStore. audio. media. MIME_TYPE}; // both methods can be used // Cursor cursor = // this. managedQuery (MediaStore. audio. media. EXTERNAL_CONTENT_URI, mediaColumns, null, null); Cursor cursor = contentResolver. query (MediaStore. audio. media. EXTERNAL_CONTENT_URI, audioColumns, null); while (cursor. moveToNext () {String _ id = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. audio. media. _ ID); String filePath = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. audio. media. DATA); String title = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. audio. media. TITLE); String mime_type = cursor. getString (cursor. getColumnIndexOrThrow (MediaStore. audio. media. MIME_TYPE); System. out. println ("_ id =" + _ id); System. out. println ("title =" + title); System. out. println ("filePath =" + filePath); System. out. println ("mime_type =" + mime_type );}}}

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.