當前位置:首頁 » 安卓系統 » androidmanagedquery

androidmanagedquery

發布時間: 2023-11-08 10:56:49

① 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())));

熱點內容
起床的戰爭玩什麼伺服器 發布:2025-01-23 21:03:06 瀏覽:140
企業級安卓手機防毒軟體哪個好 發布:2025-01-23 20:59:28 瀏覽:242
資料庫精美 發布:2025-01-23 20:37:05 瀏覽:234
mysql怎麼編譯驅動 發布:2025-01-23 20:35:15 瀏覽:466
修改資料庫的語句是 發布:2025-01-23 20:26:17 瀏覽:761
linuxping域名 發布:2025-01-23 20:24:34 瀏覽:478
神經網路演算法應用 發布:2025-01-23 20:18:36 瀏覽:218
冒險島按鍵精靈腳本下載 發布:2025-01-23 19:46:50 瀏覽:751
安卓訪問共享需要開通什麼服務 發布:2025-01-23 19:43:01 瀏覽:518
vs2015c語言調試 發布:2025-01-23 19:42:47 瀏覽:142