androidmanagedquery
① android中调用系统的图片浏览器后,返回了下面这样的uri----content://media/external/images/media/2
从URI获得文件路径:
string myImageUrl = "content://media/external/images/media/***";Uri uri = Uri.parse(myImageUrl); String[] proj = { MediaStore.Images.Media.DATA }; Cursor actualimagecursor = this.ctx.managedQuery(uri,proj,null,null,null); int actual_image_column_index = actualimagecursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); actualimagecursor.moveToFirst(); String img_path = actualimagecursor.getString(actual_image_column_index);
File file = new File(img_path);Uri fileUri = Uri.fromFile(file);
② 【Android开发】怎么在ListView中做一个图片批量上传的队列
先是两个layout:
1、main.xml
复制代码
复制代码
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="vertical"
4 android:layout_width="fill_parent"
5 android:layout_height="fill_parent">
6 <ListView
7 android:layout_width="fill_parent"
8 android:layout_height="fill_parent"
9 android:focusable="false"
10 android:id="@+id/lvImageList" >
11 </ListView>
12 </LinearLayout>
复制代码
复制代码
2、listitem.xml
复制代码
复制代码
1 <?xml version="1.0" encoding="utf-8"?>
2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3 android:orientation="horizontal"
4 android:layout_width="fill_parent"
5 android:layout_height="?android:attr/listPreferredItemHeight">
6 <ImageView
7 android:id="@+id/itemImgImageInfo"
8 android:layout_marginTop="4dip"
9 android:layout_marginBottom="4dip"
10 android:layout_width="?android:attr/listPreferredItemHeight"
11 android:layout_height="?android:attr/listPreferredItemHeight">
12 </ImageView>
13 <TwoLineListItem xmlns:android="http://schemas.android.com/apk/res/android"
14 android:layout_width="fill_parent"
15 android:layout_height="fill_parent"
16 android:paddingLeft="4dip"
17 android:mode="twoLine">
18 <CheckedTextView
19 android:id="@+id/itemChkImageInfo"
20 android:layout_width="fill_parent"
21 android:layout_height="wrap_content"
22 android:gravity="center_vertical"
23 android:textAppearance="?android:attr/textAppearanceSmall"
24 android:checkMark="?android:attr/listChoiceIndicatorMultiple">
25 </CheckedTextView>
26 <TextView
27 android:id="@+id/itemTxtImageInfo"
28 android:layout_width="fill_parent"
29 android:layout_height="wrap_content"
30 android:gravity="center_vertical|top"
31 android:layout_marginBottom="4dip"
32 android:layout_below="@+id/itemChkImageInfo"
33 android:textAppearance="?android:attr/textAppearanceSmall">
34 </TextView>
35 </TwoLineListItem>
36 </LinearLayout>
复制代码
复制代码
接着是代码:
复制代码
复制代码
1 package com.android.MultipleChoiceImageList;
2
3 import java.util.ArrayList;
4 import java.util.HashMap;
5 import java.util.List;
6 import java.util.Map;
7
8 import android.app.Activity;
9 import android.content.Context;
10 import android.database.Cursor;
11 import android.graphics.Bitmap;
12 import android.net.Uri;
13 import android.os.Bundle;
14 import android.provider.MediaStore;
15 import android.provider.MediaStore.Images;
16 import android.view.LayoutInflater;
17 import android.view.View;
18 import android.view.ViewGroup;
19 import android.widget.AdapterView;
20 import android.widget.CheckedTextView;
21 import android.widget.ImageView;
22 import android.widget.ListView;
23 import android.widget.SimpleAdapter;
24 import android.widget.TextView;
25 import android.widget.AdapterView.OnItemClickListener;
26
27 public class MainActivity extends Activity {
28
29 private ListView lvImageList;
30
31 private String imageID= "imageID";
32 private String imageName = "imageName";
33 private String imageInfo = "imageInfo";
34
35 private ArrayList<String> fileNames = new ArrayList<String>();
36
37 private mAdapter;
38
39 /** Called when the activity is first created. */
40 @Override
41 public void onCreate(Bundle savedInstanceState) {
42 super.onCreate(savedInstanceState);
43 setContentView(R.layout.main);
44
45 lvImageList=(ListView) this.findViewById(R.id.lvImageList);
46 lvImageList.setItemsCanFocus(false);
47 lvImageList.setOnItemClickListener(new OnItemClickListener() {
48 @Override
49 public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
50
51 CheckedTextView checkedTextView = (CheckedTextView) view.findViewById(R.id.itemChkImageInfo);
52 checkedTextView.toggle();
53 mAdapter.setCheckItem(position, checkedTextView.isChecked());
54 }
55 });
56 try{
57 String[] from = {imageID, imageName, imageInfo};
58 int[] to = {R.id.itemImgImageInfo, R.id.itemChkImageInfo, R.id.itemTxtImageInfo};
59 mAdapter = new (MainActivity.this, GetImageList(), R.layout.listitem, from, to);
60 lvImageList.setAdapter(mAdapter);
61 }
62 catch(Exception ex){
63 return;
64 }
65 }
66
67 //获取图片列表
68 private ArrayList<Map<String, String>> GetImageList(){
69
70 ArrayList<Map<String, String>> imageList = new ArrayList<Map<String,String>>();
71 HashMap<String, String> imageMap;
72
73 //读取SD卡中所有图片
74 Uri uri = MediaStore.Images.Media.EXTERNAL_CONTENT_URI;
75 String[] projection = { MediaStore.Images.Media._ID, MediaStore.Images.Media.DISPLAY_NAME,MediaStore.Images.Media.DATA, MediaStore.Images.Media.SIZE};
76 String selection = MediaStore.Images.Media.MIME_TYPE + "=?";
77 String[] selectionArg ={"image/jpeg"};
78 Cursor mCursor = this.managedQuery(uri, projection, selection, selectionArg, MediaStore.Images.Media.DISPLAY_NAME);
79 imageList.clear();
80 if (mCursor != null) {
81 mCursor.moveToFirst();
82 while (mCursor.getPosition() != mCursor.getCount())
83 {
84 imageMap= new HashMap<String, String>();
85 imageMap.put(imageID, mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media._ID)));
86 imageMap.put(imageName, mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME)));
87 imageMap.put(imageInfo, " " + (mCursor.getLong(mCursor.getColumnIndex(MediaStore.Images.Media.SIZE))/1024)+"KB");
88 imageList.add(imageMap);
89 fileNames.add(mCursor.getString(mCursor.getColumnIndex(MediaStore.Images.Media.DATA)));
90 mCursor.moveToNext();
91 }
92 mCursor.close();
93 }
94 return imageList;
95 }
96
97 //可多选图片列表适配器
98 class extends SimpleAdapter {
99
100 private Map<Integer, Boolean> map;
101 private List<Integer> state;
102 private List<? extends Map<String, ?>> mList;
103
104 LayoutInflater mInflater;
105
106 public (Context context, List<Map<String, String>> data, int resource, String[] from, int[] to) {
107 super(context, data, resource, from, to);
108 map = new HashMap<Integer, Boolean>();
109 mInflater = LayoutInflater.from(context);
110 mList = data;
111 for(int i = 0; i < data.size(); i++) {
112 map.put(i, false);
113 }
114 state = new ArrayList<Integer>();
115 }
116
117 @Override
118 public int getCount() {
119 return mList.size();
120 }
121
122 @Override
123 public Object getItem(int position) {
124 return position;
125 }
126
127 @Override
128 public long getItemId(int position) {
129 return position;
130 }
131
132 //设置条目选中状态
133 public void setCheckItem(int position, Boolean isChecked){
134 map.put(position, isChecked);
135 if (state.contains(position))
136 state.remove((Object)position);
137 if (isChecked){
138 state.add(position);
139 }
140 }
141
142 //获取列表中已选中条目
143 public long[] getCheckItemIds(){
144 int count = state.size();
145 long[] ids = new long[count];
146 for (int i = 0; i < count; i++) {
147 ids[i]= (long)state.get(i);
148 }
149 return ids;
150 }
151
152 @Override
153 public View getView(int position, View convertView, ViewGroup parent) {
154 if(convertView == null) {
155 convertView = mInflater.inflate(R.layout.listitem, null);
156 }
157
158 CheckedTextView checkedTextView = (CheckedTextView) convertView.findViewById(R.id.itemChkImageInfo);
159 checkedTextView.setChecked(map.get(position));
160 checkedTextView.setText((String)mList.get(position).get(imageName));
161
162 TextView textView = (TextView) convertView.findViewById(R.id.itemTxtImageInfo);
163 textView.setText((String)mList.get(position).get(imageInfo));
164
165 //显示图片缩略图
166 ImageView image = (ImageView) convertView.findViewById(R.id.itemImgImageInfo);
167 Bitmap bm = MediaStore.Images.Thumbnails.getThumbnail(getContentResolver(), Long.parseLong((String)mList.get(position).get(imageID)), Images.Thumbnails.MICRO_KIND, null);
168 image.setImageBitmap(bm);
169
170 return convertView;
171 }
172 }
173 }
③ 如何调用android通讯录
这个过程有三步:
1)许可向你的应用程序的manifest中添加读取通讯录数据的许可
<uses-permission android:name="android.permission.READ_CONTACTS"/
2)调用Contact Picker
在你的activity中,创建一个Intent,用来请求系统查找一个能够执行一个从Contacts URI的项目中发出的PICK动作的activity,
Intent intent = new Intent(Intent.ACTION_PICK, ContactsContract.Contacts.CONTENT_URI);
调用startActivityForResult在Intent中传递(和一个整数的请求代码,在这个例子中是PICK CONTACT )。这将导致android启动一个已经被等级的activity,用来支持在People.CONTENT URI上的ACTION_PICK ,然后当按下选择(或者取消)的时候返回到这个activity。startActivityForResult(intent, PICK_CONTACT);
3)仍然在你的activity中,重写onActivityResul方法来监听在你的第二个步骤中启动的activi'select a contact' activity的回调。你应该检查反馈的请求代码是否匹配你的期望值,结果代码是否是 RESULT_OK。
通过在数据Intent参数中调用getData()你可以获得选择的联系人的URI。为了获得选择的联系人的名字你需要使用URI从返回的游标中创建一个新的查询和提取的参数。
@Overridepublic void onActivityResult(int reqCode, int resultCode, Intent data) {
super.onActivityResult(reqCode, resultCode, data);
switch (reqCode) {
case (PICK_CONTACT) :
if (resultCode == Activity.RESULT_OK) {
Uri contactData = data.getData();
Cursor c = managedQuery(contactData, null, null, null, null);
if (c.moveToFirst()) {
String name = c.getString(c.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME));}}break;}}
④ android 如何查询最新添加的联系人
文是提供了3种方式查询通讯录,获取手机通讯录中所有联系人的ID和联系人姓名。
通过ContentResolver类的query方法,这种方法在主线程中查询数据,query方法返回数据库的游标Cursor,需要自己去手动关闭。
通过Activity类的managedQuery方法,这种方法查询同样是在主线程中,但是不需要手动关闭Cursor,因为Activity类销毁时再onDestroy方法中会去关闭。
通过Android提供的异步查询框架AsyncQueryHandler,这个类是一个抽象类,提供异步查询,当查询完毕后,会调用onQueryComplete方法,该方法参数cursor是查询系统数据库完成后获得的游标。
⑤ android如何通过path得到uri
最近做项目要通过图片的绝对路径找到图片的URI,然后删除图片,小小总结一下获取URI的方法,亲自试验在
android 4.1.3的系统上都是可用的。
1.将所有的图片路径取出,遍历比较找到需要的路径,取出URI,效率较低
其中 MediaStore.MediaColumns.DATA 字段存的就是图片的绝对路径,
最后mImageUri得到的就是图片的URI
1 Uri mUri = Uri.parse("content://media/external/images/media");
2 Uri mImageUri = null;
3 Cursor cursor = managedQuery(
4 MediaStore.Images.Media.EXTERNAL_CONTENT_URI, null, null, null,
5 MediaStore.Images.Media.DEFAULT_SORT_ORDER);
6 cursor.moveToFirst();
7
8 while (!cursor.isAfterLast()) {
9 String data = cursor.getString(cursor
10 .getColumnIndex(MediaStore.MediaColumns.DATA));
11 if (picPath.equals(data)) {
12 int ringtoneID = cursor.getInt(cursor
13 .getColumnIndex(MediaStore.MediaColumns._ID));
14 mImageUri = Uri.withAppendedPath(mUri, "" + ringtoneID);
15 break;
16 }
17 cursor.moveToNext();
18 }2.直接从媒体数据库根据字段取出相应的记录,效率较高
1 //TYLT: add by anyf 20121027 start
2 String type = Utils.ensureNotNull(intent.getType());
3 Log.d(TAG, "uri is " + uri);
4 if (uri.getScheme().equals("file") && (type.contains("image/")))
{
5 String path = uri.getEncodedPath();
6 Log.d(TAG, "path1 is " + path);
7 if (path != null) {
8 path = Uri.decode(path);
9 Log.d(TAG, "path2 is " + path);
10 ContentResolver cr = this.getContentResolver();
11 StringBuffer buff = new StringBuffer();
12 buff.append("(")
13 .append(Images.ImageColumns.DATA)
14 .append("=")
15 .append("'" + path + "'")
16 .append(")");
17 Cursor cur = cr.query(
18 Images.Media.EXTERNAL_CONTENT_URI,
19 new String[] { Images.ImageColumns._ID },
20 buff.toString(), null, null);
21 int index = 0;
22 for (cur.moveToFirst(); !cur.isAfterLast(); cur
23 .moveToNext()) {
24 index = cur.getColumnIndex(Images.ImageColumns._ID);
25 // set _id value
26 index = cur.getInt(index);
27 }
28 if (index == 0) {
29 //do nothing
30 } else {
31 Uri uri_temp = Uri
32 .parse("content://media/external/images/media/"
33 + index);
34 Log.d(TAG, "uri_temp is " + uri_temp);
35 if (uri_temp != null) {
36 uri = uri_temp;
37 }
38 }
39 }
40 }
41 //TYLT: add by anyf 20121027 end3.直接根据路径通过 ContentProvider 的 delete() 方法删除图片,两行代码搞定,效率最高
1 String params[] = new String[]{filepath};
2
ctx.getContentResolver().delete(MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
MediaStore.Images.Media.DATA + " LIKE ?", params);
⑥ 如何扫描出Android系统媒体库中视频文件
android系统启动时会去扫描系统文件,并将系统支持的视频文件(mp4,3gp,wmv)扫描到媒体库(MediaStore)中,下面代码演示如何获得这些文件的信息:
public static List<VideoInfo> sysVideoList = null;// 视频信息集合
sysVideoList = new ArrayList<VideoInfo>();
setVideoList();
private void setVideoList() {
// MediaStore.Video.Thumbnails.DATA:视频缩略图的文件路径
String[] thumbColumns = { MediaStore.Video.Thumbnails.DATA,
MediaStore.Video.Thumbnails.VIDEO_ID };
// MediaStore.Video.Media.DATA:视频文件路径;
// MediaStore.Video.Media.DISPLAY_NAME : 视频文件名,如 testVideo.mp4
// MediaStore.Video.Media.TITLE: 视频标题 : testVideo
String[] mediaColumns = { MediaStore.Video.Media._ID,
MediaStore.Video.Media.DATA, MediaStore.Video.Media.TITLE,
MediaStore.Video.Media.MIME_TYPE,
MediaStore.Video.Media.DISPLAY_NAME };
cursor = managedQuery(MediaStore.Video.Media.EXTERNAL_CONTENT_URI,
mediaColumns, null, null, null);
if(cursor==null){
Toast.makeText(SystemVideoChooseActivity.this, "没有找到可播放视频文件", 1).show();
return;
}
if (cursor.moveToFirst()) {
do {
VideoInfo info = new VideoInfo();
int id = cursor.getInt(cursor
.getColumnIndex(MediaStore.Video.Media._ID));
Cursor thumbCursor = managedQuery(
MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI,
thumbColumns, MediaStore.Video.Thumbnails.VIDEO_ID
+ "=" + id, null, null);
if (thumbCursor.moveToFirst()) {
info.setThumbPath(thumbCursor.getString(thumbCursor
.getColumnIndex(MediaStore.Video.Thumbnails.DATA)));
}
info.setPath(cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DATA)));
info.setTitle(cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.TITLE)));
info.setDisplayName(cursor.getString(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.DISPLAY_NAME)));
LogUtil.log(TAG, "DisplayName:"+info.getDisplayName());
info.setMimeType(cursor
.getString(cursor
.getColumnIndexOrThrow(MediaStore.Video.Media.MIME_TYPE)));
sysVideoList.add(info);
} while (cursor.moveToNext());
}
}
有一点需要注意的是:系统的媒体库并不会在我们添加视频文件后自动更新,我们如何去手动扫描媒体库,或者重启系统才能从媒体库中得到更新的视频文件:
sendBroadcast(new Intent(Intent.ACTION_MEDIA_MOUNTED, Uri.parse("file://"
+ Environment.getExternalStorageDirectory())));