关闭

通过ContentProvider得到系统的音频,视频等

729人阅读 评论(0) 收藏 举报
分类:
package com.mct.scanallmediafilesdemo;

import android.app.Activity;
import android.content.ContentResolver;
import android.database.Cursor;
import android.os.Bundle;
import android.provider.MediaStore;
import android.util.Log;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		// scanMusic();
		scanImage();
	}

	private void scanVideo() {
		// 遍历视频
		ContentResolver contentResolver = getContentResolver();
		String[] projection = new String[] {
				MediaStore.Video.Media.MINI_THUMB_MAGIC,
				MediaStore.Video.Media.DATA };
		Cursor cursor = contentResolver.query(
				MediaStore.Video.Media.EXTERNAL_CONTENT_URI, projection, null,
				null, MediaStore.Video.Media.DEFAULT_SORT_ORDER);
		// int fileNum = cursor.getCount();

		while (cursor.moveToNext()) {
			Log.e("MainActivity",
					"file is: "
							+ cursor.getString(cursor
									.getColumnIndex(MediaStore.Video.Media.DATA)));
		}
		cursor.close();
	}

	private void scanMusic() {
		Cursor cursor = getContentResolver().query(
				MediaStore.Audio.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Audio.Media.DEFAULT_SORT_ORDER);
		cursor.moveToFirst();
		// int counter = cursor.getCount();

		while (cursor.moveToNext()) {
			String title = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.TITLE));
			String path = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Audio.Media.DATA));
			Log.e("tag", "title:" + title + " path:" + path);
		}
		cursor.close();
	}

	private void scanImage() {
		Cursor cursor = getContentResolver().query(
				MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
				MediaStore.Images.Media.DEFAULT_SORT_ORDER);
		// int counter = cursor.getCount();

		while (cursor.moveToNext()) {
			String title = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.TITLE));
			String path = cursor.getString(cursor
					.getColumnIndexOrThrow(MediaStore.Images.Media.DATA));
			Log.e("tag", "title:" + title + " path:" + path);
		}
		cursor.close();
	}

}

0
0

查看评论
* 以上用户言论只代表其个人观点,不代表CSDN网站的观点或立场
    个人资料
    • 访问:118031次
    • 积分:1952
    • 等级:
    • 排名:千里之外
    • 原创:54篇
    • 转载:102篇
    • 译文:1篇
    • 评论:18条
    文章分类
    最新评论