Use of the Sql-android URI (reproduced)

Source: Internet
Author: User

Today in the operation of Android, using the database access, on the Internet to learn about the database of knowledge. Where access to the database is done through URIs, so here is a summary of the application of the Android URI.

The following reference http://www.cnblogs.com/lingyun1120/archive/2012/04/18/2455212.html

Generic resource Identifier (Universal Resource Identifier, referred to as "URI").

URIs represent the data to be manipulated, and each resource available on Android-images, video clips, and so on-can be represented by URIs.

URIs are generally made up of three parts:

The naming mechanism for accessing resources.

Host name of the storage resource.

The name of the resource itself, represented by the path.

  The URI of Android consists of the following three parts: "content://", the path of the data, the ID of the indicator (optional)

  For example, such as:

Uri:content://contacts/people of all contact persons

The URI:CONTENT://CONTACTS/PEOPLE/5 of a contact.

All pictures uri:content://media/external

The URI:CONTENT://MEDIA/EXTERNAL/IMAGES/MEDIA/4 of a picture

Let's look at how Android manages the information for multimedia files (audio, video, pictures). With DDMS, we found the database file under/data/data/com.android.providers.media.

External.db Open Images List

The operation is as follows

[Email protected]_y100:/data/Data # CD Com.android.providers.media[email protected]_y100:/data/data/com.android.providers.media #lscachedatabaseslibshared_prefsdatabases/<[email protected]_y100:/data/data/com.android.providers.media/databases #lsexternal.dbexternal.db-shmexternal.db-walinternal.dbinternal.db-shminternal.db-Wale3 external.db<SQLite version3.7. One  --Geneva- -  One: *: -Enter". Help"  forInstructionsenter SQL statements terminated with a";"SQLite>. Tablesalbum_art audio_genres Log Album_info Audio_genres_map Search albums Audio_genres_map_noid Searchhelpertitle android_metadata Audio_me                Ta thumbnails artist_info audio_playlists video Artists               Audio_playlists_map videothumbnails artists_albums_map Files Audio images SQLite>Select*from images; -|/storage/sdcard0/dcim/camera/img_20150101_021647.jpg|1207033| Img_20150101_021647.jpg|image/jpeg| img_20150101_021647|1420078609|1420078609||||||1420078607883|0|2289287690547999733|1509922574| camera|1872|3328|||||||||||0|0|-1|0|0|0|0|0|0|0SQLite>. Schema imagescreate VIEW images as SELECT _id,_data,_size,_display_name,mime_type,title,date_added,date_ MODIFIED,DESCRIPTION,PICASA_ID,ISPRIVATE,LATITUDE,LONGITUDE,DATETAKEN,ORIENTATION,MINI_THUMB_MAGIC,BUCKET_ID, Bucket_display_name,width,height,is_drm,drm_content_uri,drm_offset,drm_datalen,drm_rights_issuer,drm_content_ Name,drm_content_description,drm_content_vendor,drm_icon_uri,drm_method,stereo_type,mpo_type,convergence,group _id,group_index,focus_value_high,focus_value_low,is_best_shot,group_count,is_mtk_3d from Files WHERE media_type =1; SQLite>

Let's pull out the form, as follows

_id,_data,_size,_display_name,mime_type,title,date_added,date_modified,description,picasa_id,isprivate, LATITUDE,LONGITUDE,DATETAKEN,ORIENTATION,MINI_THUMB_MAGIC,BUCKET_ID,BUCKET_DISPLAY_NAME,WIDTH,HEIGHT,IS_DRM, Drm_content_uri,drm_offset,drm_datalen,drm_rights_issuer,drm_content_name,drm_content_description,drm_content_ Vendor,drm_icon_uri,drm_method,stereo_type,mpo_type,convergence,group_id,group_index,focus_value_high,focus_ Value_low,is_best_shot,group_count,is_mtk_3d from Files WHERE media_type=1; -|/storage/sdcard0/dcim/camera/img_20150101_021647.jpg|1207033| Img_20150101_021647.jpg|image/jpeg| img_20150101_021647|1420078609|1420078609||||||1420078607883|0|2289287690547999733|1509922574| camera|1872|3328|||||||||||0|0|-1|0|0|0|0|0|0|0 

You can see the correspondence between the file path (_data) and the identifier ID (_id) of the URI.

  Used in code

1. Obtaining a file path from a URI

String myimageurl = "content://media/external/images/media/***";//string conversion to URIUri uri =Uri.parse (myimageurl);//this seems to be the access to the database must be, as to what is the use of Proj, temporarily do not knowString[] proj ={MediaStore.Images.Media.DATA}; Cursor Actualimagecursor= This. Ctx.managedquery (Uri,proj,NULL,NULL,NULL); //get the ID of the column      intActual_image_column_index =Actualimagecursor.getcolumnindexorthrow (MediaStore.Images.Media.DATA); Actualimagecursor.movetofirst ();//get the path to the IDString Img_path =actualimagecursor.getstring (Actual_image_column_index); File File=NewFile (Img_path); Uri Fileuri= Uri.fromfile (file);

2. Uri obtained by file path

  

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)) {intRingtoneid =cursor.getint (cursor. Getcolumnindex (mediastore.mediacolumns._id)); Mimageuri= Uri.withappendedpath (MUri, "" +Ringtoneid);  Break;             } cursor.movetonext (); }

Here is an example of how much it reflects the operation of the database.

The code is as follows

}Elseif ("Downloads". Equals (Mfileuri.getauthority ())) {string[] proj={"_data","MimeType"}; cursor cursor =Managedquery (Mfileuri, Proj,Null,Null,Null);if (NULL = =Cursor) {return2; }int Pathcolumnid = Cursor.getcolumnindexorthrow ("_data"); int mimecolumnid = Cursor.getcolumnindexorthrow ("mimetype"); if (!  Cursor.movetofirst ()) {return 2;} Mfilepath = cursor.getstring (pathcolumnid); mmimetype =  Cursor.getstring (Mimecolumnid); ANDROID.UTIL.LOG.E ("zhangshuli", "mfilepath== change 3" +mfilepath+mmimetype);}} 

Print results

mfilepath== Change 3/storage/sdcard0/download/img_20150402_124553.jpgimage/jpeg

Use of the Sql-android URI (reproduced)

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.