博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Android进阶2之检索Android的图片库并显示图片详细信息
阅读量:4104 次
发布时间:2019-05-25

本文共 3731 字,大约阅读时间需要 12 分钟。

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

关于游标(cursor)不懂的可以看博文:

具体实现:

package xiaosi.photoLibrary;import android.app.Activity;import android.app.AlertDialog;import android.content.DialogInterface;import android.database.Cursor;import android.graphics.Bitmap;import android.graphics.drawable.Drawable;import android.os.Bundle;import android.provider.MediaStore.Images.Media;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.ImageView;import android.widget.TextView;/** * 该类完成图片的检索,显示功能 *  * @author Administrator *  */public class PhotoLibraryActivity extends Activity implements OnClickListener{	private ImageView	photo;	private Button		next		= null;	private Button		previous	= null;	private Button		message		= null;	private TextView	position	= null;	private Cursor		cursor;	private int			photoIndex;	private int			photoNameIndex;	private int			photoIDIndex;	private int			photoTitleIndex;	private int			photoSizeIndex;	private String		Message		= null;	private int			totalNum	= 0;	public void onCreate(Bundle savedInstanceState)	{		super.onCreate(savedInstanceState);		setContentView(R.layout.main);		init();	}	private void init()	{		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);		// 指定获取的列		String columns[] = new String[] { 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	public void onClick(View arg0)	{		switch (arg0.getId())		{		// 下一个		case R.id.next:			if (cursor.moveToNext())			{				setImage();			}			else			{				cursor.moveToLast();			}			break;		// 上一个		case R.id.previous:			if (cursor.moveToPrevious())			{				setImage();			}			else			{				cursor.moveToFirst();			}			break;		case R.id.message:			// Dialog显示详细信息			AlertDialog.Builder builder = new AlertDialog.Builder(PhotoLibraryActivity.this);			builder.setTitle("详细信息");			builder.setMessage(Message);			builder.setPositiveButton("关闭", new android.content.DialogInterface.OnClickListener() {				public void onClick(DialogInterface dialog, int which)				{					dialog.dismiss();				}			});			builder.show();			break;		}	}	private void setImage()	{		// 获取图片的Name		String name = cursor.getString(photoNameIndex);		// 获取图片的ID		String number = cursor.getString(photoIDIndex);		// 获取图片的Title		String title = cursor.getString(photoTitleIndex);		// 获取图片的大小		String size = cursor.getString(photoSizeIndex);		// 获取图片存储路径		String path = cursor.getString(photoIndex);		// 为TextView:position赋值(现在所在的位置)		position.setText("(" + number + "/" + totalNum + ")");		Message = "Name:" + name + "\n" + "Number:" + number + "\n" + "Title:" + title + "\n" + "Size:" + size + "\n" + "Path:" + path;		// 通过路径获取图片		Drawable image = Drawable.createFromPath(path);		photo.setImageDrawable(image);	}}
mian.xml

一起见证菜鸟的起飞。。。。。。

转载地址:http://jodsi.baihongyu.com/

你可能感兴趣的文章
阿里面试总结--JAVA
查看>>
Servlet的生命周期
查看>>
JAVA八大经典书籍,你看过几本?
查看>>
《读书笔记》—–书单推荐
查看>>
【设计模式】—-(2)工厂方法模式(创建型)
查看>>
有return的情况下try catch finally的执行顺序(最有说服力的总结)
查看>>
String s1 = new String("abc"); String s2 = ("abc");
查看>>
JAVA数据类型
查看>>
Xshell 4 入门
查看>>
SoapUI-入门
查看>>
Oracle -常用命令
查看>>
JAVA技术简称
查看>>
ORACLE模糊查询优化浅谈
查看>>
2016——个人年度总结
查看>>
2017——新的开始,加油!
查看>>
【Python】学习笔记——-6.2、使用第三方模块
查看>>
【Python】学习笔记——-7.0、面向对象编程
查看>>
【Python】学习笔记——-7.1、类和实例
查看>>
【Python】学习笔记——-7.2、访问限制
查看>>
【Python】学习笔记——-7.3、继承和多态
查看>>