1 public class MainActivity extends Activity {

2 private ImageView image;
3 private Button btn;
4 private int index;
5 private int totalCount;
6 private ArrayList<String> imageSrcs = new ArrayList<String>();
7 @Override
8 public void onCreate(Bundle savedInstanceState) {
9 super.onCreate(savedInstanceState);
10 setContentView(R.layout.main);
11
12 image = (ImageView)findViewById(R.id.image);
13 btn = (Button)findViewById(R.id.btn);
14
15 //獲取上下文
16 CoNtext ctx = MainActivity.this;
17 //獲取ContentResolver物件
18 ContentResolver resolver = ctx.getContentResolver();
19 //獲得外部儲存卡上的圖片縮略圖
20 Cursor c = resolver.query(MediaStore.Images.Thumbnails.EXTERNAL_CONTENT_URI, null, null, null, null);
21 //為了for迴圈性能優化,用一變數存儲資料條數
22 totalCount = c.getCount();
23
24 //將Cursor移動到第一位
25 c.moveToFirst();
26 //將縮略圖資料添加到ArrayList中
27 for(int i=0; i<totalCount; i++){
28 int index = c.getColumnIndexOrThrow(MediaStore.Images.Thumbnails.DATA);
29 String src = c.getString(index);
30 imageSrcs.add(src);
31 index = i;
32 c.moveToNext();
33 }
34 //關閉游標
35 c.close();
36
37 //點擊按鈕,切換圖片
38 btn.setOnClickListener(new OnClickListener(){
39 @Override
40 public void onClick(View v) {
41 String src = imageSrcs.get(index);
42 image.setImageURI(Uri.parse(src));
43 index ++;
44 if(index == totalCount){
45 index = 0;
46 }
47 }
48 });
49 }
50 }
 
 
來源:
http://www.cnblogs.com/wisekingokok/archive/2011/09/20/2182272.html
arrow
arrow
    全站熱搜

    戮克 發表在 痞客邦 留言(0) 人氣()