gpt4 book ai didi

android - 显示视频缩略图

转载 作者:行者123 更新时间:2023-11-29 17:44:49 29 4
gpt4 key购买 nike

我需要使用 Picasso 库在 ListView 中显示每个视频的缩略图以加快运行速度,因此我需要获取缩略图路径以供使用。

这是我获取缩略图路径的代码(我在 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)

谁能帮帮我?非常感谢你

最佳答案

您的 URI 不正确。 getContentUri(String volumeName) 需要魔法词 "external" 而不是路径。你可能还没有缩略图。

您可以使用这段代码加载缩略图

private static final String SELECTION = MediaColumns.DATA + "=?";
private static final String[] PROJECTION = { BaseColumns._ID };
public static Bitmap loadVideoThumbnail(String videoFilePath, ContentResolver cr) {
Bitmap result = null;
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] selectionArgs = { videoFilePath };
Cursor cursor = cr.query(uri, PROJECTION, SELECTION, selectionArgs, null);
if (cursor.moveToFirst()) {
// it's the only & first thing in projection, so it is 0
long videoId = cursor.getLong(0);
result = MediaStore.Video.Thumbnails.getThumbnail(cr, videoId,
Thumbnails.MICRO_KIND, null);
}
cursor.close();
return result;
}

它的作用是:

  1. 通过查询Video.Media来查找文件的视频Id
  2. 将该 videoId 传递给 getThumbnail,它会阻塞(在您的情况下是 ui 线程..),直到制作并解码缩略图。

最大的缺点是您不能在此处使用 Picasso 的路径。 (自定义加载有效,https://github.com/square/picasso/blob/master/picasso/src/main/java/com/squareup/picasso/MediaStoreRequestHandler.java 似乎是一个实现,这里有一些关于它的描述:http://blog.jpardogo.com/requesthandler-api-for-picasso-library/)

您可以获得缩略图路径,但是如果您查看缩略图表的内容,例如通过这段代码

    StringBuilder sb = new StringBuilder();
Cursor query = getContentResolver().query(
Video.Thumbnails.EXTERNAL_CONTENT_URI, null, null, null, null);
DatabaseUtils.dumpCursor(query, sb);
query.close();
Log.d("XXX", sb.toString());

您会发现并非每个视频都有缩略图。

但是那些有的可以通过

找到
public static String loadVideoThumbnailPath(String videoFilePath,
ContentResolver cr) {
String result = null;
Uri uri = MediaStore.Video.Media.EXTERNAL_CONTENT_URI;
String[] selectionArgs = { videoFilePath };
Cursor cursor = cr.query(uri, PROJECTION, SELECTION, selectionArgs,
null);
long videoId = -1;
if (cursor.moveToFirst()) {
videoId = cursor.getLong(0);
}
cursor.close();
if (videoId > 0) {
Uri uri2 = MediaStore.Video.Thumbnails.EXTERNAL_CONTENT_URI;
String[] projection2 = { MediaStore.Video.Thumbnails.DATA };
String selection2 = MediaStore.Video.Thumbnails.VIDEO_ID + "=?";
String[] selectionArgs2 = { String.valueOf(videoId) };
Cursor cursor2 = cr.query(uri2, projection2, selection2, selectionArgs2, null);
if (cursor2.moveToFirst()) {
result = cursor2.getString(0);
}
cursor2.close();
}
return result;
}

(仍然是上面的 PROJECTIONSELECTION 常量)

关于android - 显示视频缩略图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27399293/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com