android通过ContentPrivider扫描sd卡所有图片,并显示
如题:
重点在于ContentPrivider的使用,和图片的显示上
首先在使用ContentPrivider查找图片前,先将sd卡整个扫描一遍,要不然的话你可能刚刚添加的图片是不会查找出来的。
[html] view plaincopyprint?
// 必须在查找前进行全盘的扫描,否则新加入的图片是无法得到显示的(加入对sd卡操作的权限)
public void allScan() {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}
// 必须在查找前进行全盘的扫描,否则新加入的图片是无法得到显示的(加入对sd卡操作的权限)
public void allScan() {
sendBroadcast(new Intent(
Intent.ACTION_MEDIA_MOUNTED,
Uri.parse("file://" + Environment.getExternalStorageDirectory())));
}加入对sd卡访问的权限:
[html] view plaincopyprint?
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.RESTART_PACKAGES" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
下一步就可以使用ContentPrivider了:
[html] view plaincopyprint?
/**
* @FILE:ImageService.java
* @AUTHOR:hui-ye
* @DATE:2013-5-21 下午3:30:41
**/
package com.image.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import android.content.ContentResolver;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
/*******************************************
*
* @CLASS:ImageService
* @DESCRIPTION:
* @AUTHOR:hui-ye
* @VERSION:v1.0
* @DATE:2013-5-21 下午3:30:41
*******************************************/
public class ImageService {
private Context context;
public ImageService(Context context) {
this.context = context;
}
/**
* @description:通过contentprovider获得sd卡上的图片
* @author:hui-ye
* @return:void
*/
public List<HashMap<String, String>> getImages() {
// 指定要查询的uri资源
Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
// 获取ContentResolver
ContentResolver contentResolver = context.getContentResolver();
// 查询的字段
String[] projection = { MediaStore.Images.Media._ID,
MediaStore.Images.Media.DISPLAY_NAME,
MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE };
// 条件
String selection = MediaStore.Images.Media.MIME_TYPE + "=?";
// 条件值(這裡的参数不是图片的格式,而是标准,所有不要改动)
String[] selectionArgs = { "image/jpeg" };
// 排序
String sortOrder = MediaStore.Images.Media.DATE_MODIFIED + " desc";
// 查询sd卡上的图片
Cursor cursor = contentResolver.query(uri, projection, selection,
selectionArgs, sortOrder);
List<HashMap<String, String>> imageList = new ArrayList<HashMap<String, String>>();
if (cursor != null) {
HashMap<String, String> imageMap = null;
cursor.moveToFirst();
while (cursor.moveToNext()) {
imageMap = new HashMap<String, String>();
// 获得图片的id
imageMap.put("imageID", cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media._ID)));
// 获得图片显示的名称
imageMap.put("imageName", cursor.getString(cursor
.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
// 获得图片的信息
imageMap.put(
"imageInfo",
""
+ cursor.getLong(cursor
.getColumnIndex(MediaStore.Images.Media.SIZE) / 1024)
 
补充:移动开发 , Android ,