android 从媒体库去数据

package com.iban.dao;

import java.io.File;
import java.util.ArrayList;
import java.util.List;

import com.iban.util.IbanConstant;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.net.Uri;
import android.provider.MediaStore;
import android.provider.MediaStore.Images.Media;

public class ImgDao {

	
	
public  List<String> getAlbumName(Context context){
		List<String> list = new ArrayList<String>();
		//获取相册列表
		String[] columns = { Media.BUCKET_DISPLAY_NAME };  
		String groupby = " 1=1 ) group by ("+Media.BUCKET_DISPLAY_NAME;  
		Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, groupby, null, null);  
		while (cursor.moveToNext())  
		{  
		    String name = cursor.getString(cursor.getColumnIndexOrThrow(Media.BUCKET_DISPLAY_NAME));  
		    System.out.println("name:"+name);
		    list.add(name);
		}  
		cursor.close();
		return list;
		
		//发送一个广播更新媒体数据库信息  
		//sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://" + Environment.getExternalStorageDirectory())))
		/*
		*/
	}
	
	public void gets(Context context){
		String[] columns = { Media._ID,Media.DATA,Media.DISPLAY_NAME };  
		String where = Media.DATA + " like  \'/mnt/sdcard/iban/%\'";  
		// 得到游标  
		Cursor cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, where, null, null);  
		while (cursor.moveToNext())   
		{  
		   // 获得制定列的值  
			int id = cursor.getInt(cursor.getColumnIndexOrThrow(Media._ID)); 
		   String filePath = cursor.getString(cursor.getColumnIndexOrThrow(Media.DATA));  
		   String name = cursor.getString(cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME));
		   System.out.println("name:"+name);
		}  
		cursor.close();
	}



	public void getImg(){
		/*Uri mUri = Uri.parse("content://media/external/images/media"); 
        Uri mImageUri = null;

        Cursor cursor = managedQuery(
                MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null,
                null, MediaStore.Images.Media.DEFAULT_SORT_ORDER);
        cursor.moveToFirst();

        while (!cursor.isAfterLast()) {
            String data = cursor.getString(cursor
                    .getColumnIndex(MediaStore.MediaColumns.DATA));
            if (picPath.equals(data)) {
                int ringtoneID = cursor.getInt(cursor
                        .getColumnIndex(MediaStore.MediaColumns._ID));
                mImageUri = Uri.withAppendedPath(mUri, ""
                        + ringtoneID);
                break;
            }
            cursor.moveToNext();
        }*/
		
	
	}
	
	public  void getImageContentUri(Context context) {
		Cursor cursor = null;
		 String columns[] = new String[]{  Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME };  
	        //cursor = this.managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);  
	        cursor = context.getContentResolver().query(Media.EXTERNAL_CONTENT_URI, columns, null, null, null);  
	        
	       int photoIndex = cursor.getColumnIndexOrThrow(Media.DATA);  
	        //titleIndex = cursor.getColumnIndexOrThrow(Media.TITLE);  
	        int nameIndex = cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);  
	          String filse = cursor.getString(photoIndex);
	       System.out.println("nameIndex:"+nameIndex+"  photoIndex"+photoIndex+"  filse:"+filse);
	        //显示第一张图片,但是首先要判断一下,Cursor是否有值  
	}
	
	public void updateImg(){
		String path  = IbanConstant.DIR+"temp.jpg";
		File file = new File(path);
		if(file.exists()){
			file.delete();
		}
	}
}

你可能感兴趣的:(android 从媒体库去数据)