检索Android图片库并显示详细信息
16lz
2021-01-26
这篇文章,我们将学习如何检索并显示媒体库中的图片以及每张图片的详细信息包括名称,ID,路径,大小等等。
关于游标(cursor)不懂的可以看博文:Android中Cursor类的概念和用法
具体实现:
- packagexiaosi.photoLibrary;
- importandroid.app.Activity;
- importandroid.app.AlertDialog;
- importandroid.content.DialogInterface;
- importandroid.database.Cursor;
- importandroid.graphics.Bitmap;
- importandroid.graphics.drawable.Drawable;
- importandroid.os.Bundle;
- importandroid.provider.MediaStore.Images.Media;
- importandroid.view.View;
- importandroid.view.View.OnClickListener;
- importandroid.widget.Button;
- importandroid.widget.ImageView;
- importandroid.widget.TextView;
- /**
- *该类完成图片的检索,显示功能
- *
- *@authorAdministrator
- *
- */
- publicclassPhotoLibraryActivityextendsActivityimplementsOnClickListener
- {
- privateImageViewphoto;
- privateButtonnext=null;
- privateButtonprevious=null;
- privateButtonmessage=null;
- privateTextViewposition=null;
- privateCursorcursor;
- privateintphotoIndex;
- privateintphotoNameIndex;
- privateintphotoIDIndex;
- privateintphotoTitleIndex;
- privateintphotoSizeIndex;
- privateStringMessage=null;
- privateinttotalNum=0;
- publicvoidonCreate(BundlesavedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.main);
- init();
- }
- privatevoidinit()
- {
- next=(Button)findViewById(R.id.next);
- next.setOnClickListener(this);
- previous=(Button)findViewById(R.id.previous);
- previous.setOnClickListener(this);
- message=(Button)findViewById(R.id.message);
- message.setOnClickListener(this);
- photo=(ImageView)this.findViewById(R.id.image_view);
- position=(TextView)findViewById(R.id.number);
- //指定获取的列
- Stringcolumns[]=newString[]{Media.DATA,Media._ID,Media.TITLE,Media.DISPLAY_NAME,Media.SIZE};
- //得到一个游标
- cursor=this.getContentResolver().query(Media.EXTERNAL_CONTENT_URI,columns,null,null,null);
- //获取指定列的索引
- photoIndex=cursor.getColumnIndexOrThrow(Media.DATA);
- photoNameIndex=cursor.getColumnIndexOrThrow(Media.DISPLAY_NAME);
- photoIDIndex=cursor.getColumnIndexOrThrow(Media._ID);
- photoTitleIndex=cursor.getColumnIndexOrThrow(Media.TITLE);
- photoSizeIndex=cursor.getColumnIndexOrThrow(Media.SIZE);
- //获取图片总数
- totalNum=cursor.getCount();
- //跳到第一个图片
- if(cursor.moveToFirst())
- {
- setImage();
- position.setText("(1/"+totalNum+")");
- }
- }
- @Override
- publicvoidonClick(Viewarg0)
- {
- switch(arg0.getId())
- {
- //下一个
- caseR.id.next:
- if(cursor.moveToNext())
- {
- setImage();
- }
- else
- {
- cursor.moveToLast();
- }
- break;
- //上一个
- caseR.id.previous:
- if(cursor.moveToPrevious())
- {
- setImage();
- }
- else
- {
- cursor.moveToFirst();
- }
- break;
- caseR.id.message:
- //Dialog显示详细信息
- AlertDialog.Builderbuilder=newAlertDialog.Builder(PhotoLibraryActivity.this);
- builder.setTitle("详细信息");
- builder.setMessage(Message);
- builder.setPositiveButton("关闭",newandroid.content.DialogInterface.OnClickListener(){
- publicvoidonClick(DialogInterfacedialog,intwhich)
- {
- dialog.dismiss();
- }
- });
- builder.show();
- break;
- }
- }
- privatevoidsetImage()
- {
- //获取图片的Name
- Stringname=cursor.getString(photoNameIndex);
- //获取图片的ID
- Stringnumber=cursor.getString(photoIDIndex);
- //获取图片的Title
- Stringtitle=cursor.getString(photoTitleIndex);
- //获取图片的大小
- Stringsize=cursor.getString(photoSizeIndex);
- //获取图片存储路径
- Stringpath=cursor.getString(photoIndex);
- //为TextView:position赋值(现在所在的位置)
- position.setText("("+number+"/"+totalNum+")");
- Message="Name:"+name+"\n"+"Number:"+number+"\n"+"Title:"+title+"\n"+"Size:"+size+"\n"+"Path:"+path;
- //通过路径获取图片
- Drawableimage=Drawable.createFromPath(path);
- photo.setImageDrawable(image);
- }
- }
mian.xml [java] view plain copy
- <?xmlversion="1.0"encoding="utf-8"?>
- <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:background="@drawable/background"
- android:orientation="vertical">
- <LinearLayout
- android:layout_width="fill_parent"
- android:layout_height="50dip"
- android:orientation="horizontal">
- <Button
- android:id="@+id/next"
- android:layout_width="60dip"
- android:layout_height="50dip"
- android:background="@drawable/next"
- android:paddingLeft="30dip"/>
- <TextView
- android:id="@+id/number"
- android:layout_width="100dip"
- android:layout_height="50dip"
- android:paddingLeft="30dip"
- android:textColor="#000000"/>
- <Button
- android:id="@+id/previous"
- android:layout_width="60dip"
- android:layout_height="50dip"
- android:background="@drawable/previous"
- android:paddingLeft="30dip"/>
- <Button
- android:id="@+id/message"
- android:layout_width="80dip"
- android:layout_height="40dip"
- android:paddingLeft="30dip"
- android:text="详情"/>
- </LinearLayout>
- <ImageView
- android:id="@+id/image_view"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"/>
- </LinearLayout>
更多相关文章
- Android——数据存储(四种方式之二)读写SD卡
- Content Provider基础(一)
- java获取页面输入的值
- Android学习之SharedPreferences
- android——代码实现在指定位置显示View
- Android(安卓)调用系统相机返回data为null
- Android数据存储访问——文件存储
- Jsp读取数据库返回json数据,Android客户端接收json
- Android(安卓)下载图片并显示在ImageView中