Android中MediaStore使用示例

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);
        //测试1
        //getVideosInfo();
        //测试2
        //getPhotosInfo();
        //测试3
        getAudiosInfo();
    }
    
    //获取设备上所有的视频信息
	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
		};
//      两种方法均可		
//		Cursor cursor=
//	    this.managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
		Cursor cursor=contentResolver.query
		(MediaStore.Video.Media.EXTERNAL_CONTENT_URI, videoColumns, null, 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);
		}
	}
	
	 //获取设备上所有的照片信息
	 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
			};
//	      两种方法均可		
//			Cursor cursor=
//		    this.managedQuery(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
			Cursor cursor=contentResolver.query
			(MediaStore.Images.Media.EXTERNAL_CONTENT_URI, photoColumns, null, null, 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));
				//得到照片旋转角度方法一
				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("第一处 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);
					//得到照片旋转角度方法二
					//应该结合ExifInterface源码分析.
					//此处有待于进一步分析和验证
					switch (Integer.valueOf(orientation1)) {
					case 1:
						System.out.println("第二处旋转角度="+0);
						break;
					case 2:
						//matrix.invert(matrix);
						break;
					case 3:
						//matrix.setRotate(180);
						System.out.println("第二处旋转角度="+180);
						break;
					case 4:
						//matrix.invert(matrix);
						//matrix.setRotate(180);
						System.out.println("第二处旋转角度="+180);
						break;
					case 5:
						//matrix.setRotate(90);
						//matrix.invert(matrix);
						System.out.println("第二处旋转角度="+90);
						break;
					case 6:
						//matrix.setRotate(90);
						System.out.println("第二处旋转角度="+90);
						break; 
					case 7: 
						//matrix.invert(matrix);
						//matrix.setRotate(90);
						System.out.println("第二处旋转角度="+90);
						break;    
					case 8:
						//matrix.setRotate(270);
						System.out.println("第二处旋转角度="+270);
						break;
					default: 
						break;
					}
					System.out.println("XXXXXXXXXXXXXXXXXXX");
				} catch (IOException e) {
					e.printStackTrace();
				}
				
			}
		 
	 }
	 
	 //获取设备上所有的音频信息
	 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
			};
//	      两种方法均可		
//			Cursor cursor=
//		    this.managedQuery(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, mediaColumns, null, null, null);
			Cursor cursor=contentResolver.query
			(MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, audioColumns, null, null, 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);
				
			}
		 
	 }
   
}

发布了1019 篇原创文章 · 获赞 1913 · 访问量 233万+
展开阅读全文

没有更多推荐了,返回首页

©️2019 CSDN 皮肤主题: 精致技术 设计师: CSDN官方博客

分享到微信朋友圈

×

扫一扫,手机浏览