我需要使用 Picasso 库在列表视图中显示每个视频的缩略图以更快地运行,因此我需要获取缩略图路径以供使用。
这是我获取缩略图路径的代码(我在 Google 上找到了它,我更改了一些内容以适应我的应用程序):
String getThumbnailPathForLocalFile(Uri uri)
{
Cursor thumbCursor = null;
try
{
thumbCursor = c.getContentResolver().
query(uri
, null
, null , null, null);
if(thumbCursor.moveToFirst())
{
// the path is stored in the DATA column
int dataIndex = thumbCursor.getColumnIndexOrThrow( MediaStore.MediaColumns.DATA );
String thumbnailPath = thumbCursor.getString(dataIndex);
return thumbnailPath;
}
}
finally
{
if(thumbCursor != null)
{
thumbCursor.close();
}
}
return null;
}
我的 getView 功能:
@Override
public View getView(int position, View convertView, ViewGroup parent) {
View v = convertView;
if (v == null) {
LayoutInflater vi = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
v = vi.inflate(R.layout.listview_item, null);
}
/* create a new view of my layout and inflate it in the row */
// convertView = ( RelativeLayout ) inflater.inflate( resource, null );
final Item item = items.get(position);
if (item != null) {
TextView t1 = (TextView) v.findViewById(R.id.txt_fileName);
imageCity = (ImageView) v.findViewById(R.id.img_icon);
switch (item.getType()) {
case "video":
String uri2 = item.getPath();
Uri videoUri = MediaStore.Video.Thumbnails
.getContentUri(uri2);
String VideoThumbnailPath =getThumbnailPathForLocalFile(videoUri);
Picasso.with(c).load(new File(VideoThumbnailPath))
.resize(64, 64).into(imageCity);
break;
case "image":
String uri4 = item.getPath();
Picasso.with(c).load(new File(uri4)).resize(64, 64).into(imageCity);
break;
default:
break;
}
if (t1 != null)
t1.setText(item.getName());
}
return v;
}
我检查了 logcat 和调试,所以我发现 thumbCursor 为空:
12-10 17:52:38.400: E/AndroidRuntime(8659): java.lang.NullPointerException: Attempt to invoke interface method 'boolean android.database.Cursor.moveToFirst()' on a null object reference
12-10 17:52:38.400: E/AndroidRuntime(8659): at com.example.knock.FileArrayAdapter.getThumbnailPathForLocalFile(FileArrayAdapter.java:105)
12-10 17:52:38.400: E/AndroidRuntime(8659): at com.example.knock.FileArrayAdapter.getView(FileArrayAdapter.java:73)
任何人都可以帮助我吗?非常感谢你