如何获取图库选中视频的缩略图?及视频上传服务器URL选择咨询
Hey there! Let's break down your two issues and get you sorted out.
1. Displaying Video Thumbnail in ImageView
If your ImageView is showing nothing or unexpected results, using Android's built-in MediaMetadataRetriever is the most reliable way to extract and display video thumbnails. Here's how to implement it, plus common fixes for issues you might be hitting:
Example Code (Kotlin)
First, ensure you have the right permissions:
- For Android 12 and below: Request
READ_EXTERNAL_STORAGE(handle runtime permissions for Android 6.0+) - For Android 13+: Request
READ_MEDIA_VIDEOinstead
import android.media.MediaMetadataRetriever import android.widget.ImageView import android.net.Uri import android.graphics.Bitmap fun loadVideoThumbnail(videoUri: Uri, targetImageView: ImageView) { val retriever = MediaMetadataRetriever() try { // Attach the video URI to the retriever retriever.setDataSource(targetImageView.context, videoUri) // Extract a frame at 1 second (adjust time in microseconds as needed) val thumbnailBitmap = retriever.getFrameAtTime( 1000000, MediaMetadataRetriever.OPTION_CLOSEST_SYNC ) thumbnailBitmap?.let { targetImageView.setImageBitmap(it) } ?: run { // Fallback to a default image if thumbnail extraction fails targetImageView.setImageResource(R.drawable.default_video_thumbnail) } } catch (e: Exception) { e.printStackTrace() // Handle errors (e.g., corrupted video, invalid URI) targetImageView.setImageResource(R.drawable.default_video_thumbnail) } finally { retriever.release() // Always release resources to avoid leaks } }
Common Fixes for Failed Thumbnails
- Permission Issues: Double-check that you've requested and been granted the required storage/video permissions. Runtime permissions are mandatory for Android 6.0+.
- Invalid URI: The gallery might return a
content://URI (not a direct file path) – make sure you're passing this URI correctly to the retriever, not trying to use afile://path that doesn't exist. - Corrupted/Unsupported Video: If the video file is damaged or uses an unsupported codec, the retriever can't extract a frame. Add a fallback image to handle this case.
2. Which Video URL/Path to Use for Server Upload?
The right choice depends on where your video is stored:
- Local Gallery Video: When you pick a video from the gallery, you'll get a
content://URI. For uploads, avoid usingfile://paths (they trigger exceptions on Android 7.0+). Instead:- Upload via InputStream: Use
getContentResolver().openInputStream(videoUri)to get a stream of the video file, then pass this stream to your upload client (like OkHttp or Retrofit). This is the most reliable method. - Convert to File Path: If your server requires a file path, use
ContentResolverto resolve thecontent://URI to a local file path (note: this doesn't work for all gallery URIs, like those from cloud storage apps).
- Upload via InputStream: Use
- Remote Video: If the video is already hosted online (e.g., your own server, CDN), use the direct HTTP/HTTPS URL of the video file.
Quick Upload Snippet (InputStream)
val inputStream = context.contentResolver.openInputStream(videoUri) // Pass this input stream to your upload library // Example with OkHttp: val requestBody = inputStream?.let { RequestBody.create(MediaType.parse("video/*"), it.readBytes()) } // Then build your upload request
内容的提问来源于stack exchange,提问作者podgradle
相关产品推荐
相关产品推荐

