这篇文章,我们将学习如何检索并显示媒体库中的图片以及每张图片的详细信息包括名称,ID,路径,大小等等。

关于游标(cursor)不懂的可以看博文:Android中Cursor类的概念和用法

具体实现:


[java] view plain copy
  1. packagexiaosi.photoLibrary;
  2. importandroid.app.Activity;
  3. importandroid.app.AlertDialog;
  4. importandroid.content.DialogInterface;
  5. importandroid.database.Cursor;
  6. importandroid.graphics.Bitmap;
  7. importandroid.graphics.drawable.Drawable;
  8. importandroid.os.Bundle;
  9. importandroid.provider.MediaStore.Images.Media;
  10. importandroid.view.View;
  11. importandroid.view.View.OnClickListener;
  12. importandroid.widget.Button;
  13. importandroid.widget.ImageView;
  14. importandroid.widget.TextView;
  15. /**
  16. *该类完成图片的检索,显示功能
  17. *
  18. *@authorAdministrator
  19. *
  20. */
  21. publicclassPhotoLibraryActivityextendsActivityimplementsOnClickListener
  22. {
  23. privateImageViewphoto;
  24. privateButtonnext=null;
  25. privateButtonprevious=null;
  26. privateButtonmessage=null;
  27. privateTextViewposition=null;
  28. privateCursorcursor;
  29. privateintphotoIndex;
  30. privateintphotoNameIndex;
  31. privateintphotoIDIndex;
  32. privateintphotoTitleIndex;
  33. privateintphotoSizeIndex;
  34. privateStringMessage=null;
  35. privateinttotalNum=0;
  36. publicvoidonCreate(BundlesavedInstanceState)
  37. {
  38. super.onCreate(savedInstanceState);
  39. setContentView(R.layout.main);
  40. init();
  41. }
  42. privatevoidinit()
  43. {
  44. next=(Button)findViewById(R.id.next);
  45. next.setOnClickListener(this);
  46. previous=(Button)findViewById(R.id.previous);
  47. previous.setOnClickListener(this);
  48. message=(Button)findViewById(R.id.message);
  49. message.setOnClickListener(this);
  50. photo=(ImageView)this.findViewById(R.id.image_view);
  51. position=(TextView)findViewById(R.id.number);
  52. //指定获取的列
  53. Stringcolumns[]=newString[]{Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME,Media.SIZE};
  54. //得到一个游标
  55. cursor=this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI,columns,null,null,null);
  56. //获取指定列的索引
  57. photoIndex=cursor.getColumnIndexOrThrow(Media.DATA);
  58. photoNameIndex=cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
  59. photoIDIndex=cursor.getColumnIndexOrThrow(Media._ID);
  60. photoTitleIndex=cursor.getColumnIndexOrThrow(Media.TITLE);
  61. photoSizeIndex=cursor.getColumnIndexOrThrow(Media.SIZE);
  62. //获取图片总数
  63. totalNum=cursor.getCount();
  64. //跳到第一个图片
  65. if(cursor.moveToFirst())
  66. {
  67. setImage();
  68. position.setText("(1/"+totalNum+")");
  69. }
  70. }
  71. @Override
  72. publicvoidonClick(Viewarg0)
  73. {
  74. switch(arg0.getId())
  75. {
  76. //下一个
  77. caseR.id.next:
  78. if(cursor.moveToNext())
  79. {
  80. setImage();
  81. }
  82. else
  83. {
  84. cursor.moveToLast();
  85. }
  86. break;
  87. //上一个
  88. caseR.id.previous:
  89. if(cursor.moveToPrevious())
  90. {
  91. setImage();
  92. }
  93. else
  94. {
  95. cursor.moveToFirst();
  96. }
  97. break;
  98. caseR.id.message:
  99. //Dialog显示详细信息
  100. AlertDialog.Builderbuilder=newAlertDialog.Builder(PhotoLibraryActivity.this);
  101. builder.setTitle("详细信息");
  102. builder.setMessage(Message);
  103. builder.setPositiveButton("关闭",newandroid.content.DialogInterface.OnClickListener(){
  104. publicvoidonClick(DialogInterfacedialog,intwhich)
  105. {
  106. dialog.dismiss();
  107. }
  108. });
  109. builder.show();
  110. break;
  111. }
  112. }
  113. privatevoidsetImage()
  114. {
  115. //获取图片的Name
  116. Stringname=cursor.getString(photoNameIndex);
  117. //获取图片的ID
  118. Stringnumber=cursor.getString(photoIDIndex);
  119. //获取图片的Title
  120. Stringtitle=cursor.getString(photoTitleIndex);
  121. //获取图片的大小
  122. Stringsize=cursor.getString(photoSizeIndex);
  123. //获取图片存储路径
  124. Stringpath=cursor.getString(photoIndex);
  125. //为TextView:position赋值(现在所在的位置)
  126. position.setText("("+number+"/"+totalNum+")");
  127. Message="Name:"+name+"\n"+"Number:"+number+"\n"+"Title:"+title+"\n"+"Size:"+size+"\n"+"Path:"+path;
  128. //通过路径获取图片
  129. Drawableimage=Drawable.createFromPath(path);
  130. photo.setImageDrawable(image);
  131. }
  132. }

mian.xml

[java] view plain copy
  1. <?xmlversion="1.0"encoding="utf-8"?>
  2. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
  3. android:layout_width="fill_parent"
  4. android:layout_height="fill_parent"
  5. android:background="@drawable/background"
  6. android:orientation="vertical">
  7. <LinearLayout
  8. android:layout_width="fill_parent"
  9. android:layout_height="50dip"
  10. android:orientation="horizontal">
  11. <Button
  12. android:id="@+id/next"
  13. android:layout_width="60dip"
  14. android:layout_height="50dip"
  15. android:background="@drawable/next"
  16. android:paddingLeft="30dip"/>
  17. <TextView
  18. android:id="@+id/number"
  19. android:layout_width="100dip"
  20. android:layout_height="50dip"
  21. android:paddingLeft="30dip"
  22. android:textColor="#000000"/>
  23. <Button
  24. android:id="@+id/previous"
  25. android:layout_width="60dip"
  26. android:layout_height="50dip"
  27. android:background="@drawable/previous"
  28. android:paddingLeft="30dip"/>
  29. <Button
  30. android:id="@+id/message"
  31. android:layout_width="80dip"
  32. android:layout_height="40dip"
  33. android:paddingLeft="30dip"
  34. android:text="详情"/>
  35. </LinearLayout>
  36. <ImageView
  37. android:id="@+id/image_view"
  38. android:layout_width="fill_parent"
  39. android:layout_height="fill_parent"/>
  40. </LinearLayout>

更多相关文章

  1. Android——数据存储(四种方式之二)读写SD卡
  2. Content Provider基础(一)
  3. java获取页面输入的值
  4. Android学习之SharedPreferences
  5. android——代码实现在指定位置显示View
  6. Android(安卓)调用系统相机返回data为null
  7. Android数据存储访问——文件存储
  8. Jsp读取数据库返回json数据,Android客户端接收json
  9. Android(安卓)下载图片并显示在ImageView中

随机推荐

  1. Android(安卓)App的国际化-代码里实现
  2. Android(安卓)8.1 【FriendlyARM】读取 B
  3. Android(安卓)打正式包报错:Execution fai
  4. Intent.ACTION_TIME_TICK的正确用法
  5. 在Android(安卓)5.0中使用JobScheduler
  6. Android(安卓)Storage Manager
  7. Android引路蜂地图开发示例:地址反编码
  8. Android(安卓)Studio中AIDL使用方法
  9. Android进程通信之Messenger&AIDL使用详
  10. Android实现BMP和PNG转换为JPEG格式