Uri profile usage in Android

1.Uri

Universal Resource Identifier (Uri for short)

Uri represents the data to be operated, and resources (images, video clips) available on Android can be represented by Uri

        Android's Uri consists of the following three parts

        1. "content://", data path, identification ID (optional)

e.g. all images

        1. Uri:content://media/external

        2. Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

a picture

Uri:content://media/external/images/media/4

Android provides two tool classes for manipulating Uri, namely UriMatcher and ContentUris

2.UriMatcher

The UriMatcher class is mainly used to match Uri

Instructions

 //从系统相册中获取照片
    public static ArrayList<String> getSystemPhotoList(Context context)
    {
        ArrayList<String> pictureList = new ArrayList<String>();
        Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;

        ContentResolver contentResolver = context.getContentResolver();  //上下文解算器
        Cursor cursor = contentResolver.query(uri, null, null, null, null);
        if (cursor == null || cursor.getCount() <= 0) return null; // 没有图片
        while (cursor.moveToNext())
        {
            int index = cursor
                    .getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
            String path = cursor.getString(index); // 文件地址
            File file = new File(path);
            if (file.exists())
            {
                pictureList.add(path);
                Log.d(TAG, path);
            }
        }

        return pictureList ;
    }

3.ContentUris

The ContentUris class is used to get the ID behind the Uri path

        3.1 Add an ID to the path


Uri uri = Uri.parse("content://com.yfz.Lesson/people")  
 
Uri uri = Uri.parse("content://com.yfz.Lesson/people")  
 
通过withAppendedId方法,为该Uri加上ID
 
Uri resultUri = ContentUris.withAppendedId(uri, 10);  
 
Uri resultUri = ContentUris.withAppendedId(uri, 10);  
 

        3.2 Get the ID from the path


Uri uri = Uri.parse("content://com.yfz.Lesson/people/10")  
 
long personid = ContentUris.parseId(uri);  
 
Uri uri = Uri.parse("content://com.yfz.Lesson/people/10")  
 
long personid = ContentUris.parseId(uri);

Supongo que te gusta

Origin blog.csdn.net/I_am_a_loser/article/details/120327869
Recomendado
Clasificación