正文

图像存储和元数据(13)

Android多媒体开发高级编程 作者:(美)艾佛瑞


以下代码指定想要返回的列,它必须是字符串数组的形式。在下面的代码中将这个数组传递给managedQuery方法。

String[] columns = { Media.DATA, Media._ID, Media.TITLE, 

Media.DISPLAY_NAME };

cursor = managedQuery(Media.EXTERNAL_CONTENT_URI, columns, null, null, 

null);

我们将需要知道从Cursor对象获取数据的每个列的索引。在此示例中,从Media.DATA切换到MediaStore.Images.Media.DATA。这仅仅是为了说明它们是相同的。Media.DATA仅仅是可以使用的简写形式,因为有一条包含它的import语句:android.provider.MediaStore. Images.Media。

fileColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);

titleColumn = cursor.getColumnIndexOrThrow(MediaStore.Images.

Media.TITLE);

displayColumn= cursor.getColumnIndexOrThrow(MediaStore.Images.Media.

DISPLAY_NAME);

运行查询并获得一个结果Cursor对象之后,调用该对象上的moveToFirst方法,以确保它包含结果。

if (cursor.moveToFirst()) {

//titleTextView.setText(cursor.getString(titleColumn));

titleTextView.setText(cursor.getString(displayColumn));

imageFilePath = cursor.getString(fileColumn);

bmp = getBitmap(imageFilePath);

// 显示图像

imageButton.setImageBitmap(bmp);

}

然后,为imageButton指定一个新的onClickListener,其调用Cursor对象上的moveToNext方法。它将遍历结果集,获取并显示返回的每幅图像。

imageButton.setOnClickListener( 

new OnClickListener() {

public void onClick(View v) {

if (cursor.moveToNext()) 


上一章目录下一章

Copyright © 读书网 www.dushu.com 2005-2020, All Rights Reserved.
鄂ICP备15019699号 鄂公网安备 42010302001612号