【问题标题】:Show the thumbnail of a video with http使用 http 显示视频的缩略图
【发布时间】:2015-11-09 21:12:26
【问题描述】:

我想在ImageView 中显示缩略图视频。视频由用户上传并存储在服务器上。目前我还没有设置任何机制来存储和上传视频,所以我正在使用一个使用 http 协议访问的示例视频文件进行测试。但是,this 帖子说

如果 uri 方案的格式不是 content://,则 ContentResolver.query 返回 null

我的方法错了吗?是否可以将这种方法与 http 一起使用?

这是我的测试代码:

protected void onCreate(Bundle savedInstanceState) 
{
    super.onCreate(savedInstanceState);
    setContentView(R.layout.testlayout);
    Uri uri = Uri.parse("http://download.wavetlan.com/SVV/Media/HTTP/BlackBerry.3gp");
    Log.i("m",getRealPathFromURI(this, uri));
}
public String getRealPathFromURI(Context context, Uri contentUri) {
  Cursor cursor = null;
  try { 
    String[] proj = { MediaStore.Images.Media.DATA };
    cursor = context.getContentResolver().query(contentUri,  proj, null, null, null);
    if (cursor == null)
    {
        Log.i("m","null");
        return "";
    }
    int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
    cursor.moveToFirst();
    return cursor.getString(column_index);
  } finally {
    if (cursor != null) {
      cursor.close();
    }
  }
}

【问题讨论】:

    标签: android video


    【解决方案1】:

    你的代码有问题。

    目前,您的getRealPathFromURI() 在大约 6 亿台 Android 设备(运行 Android 4.4 或更高版本的所有设备)上普遍损坏,对于任何Uri 值,更不用说您正在尝试的那个了。 A Uri is not a file。无论您从哪里获得Uri,它都可能不指向来自MediaStore 的内容。即使它来自MediaStore,您也不会通过DATA 获得来自MediaStore 的文件路径。即使可以获取文件路径,您也可能无法访问该文件(例如,它存储在removable storage)。

    MediaStore 是本地内容的索引。因此,在您的特定情况下,除非您的 Android 设备运行托管在 download.wavetlan.com 的 Web 服务器,否则您的 URL 上的内容不是本地的,因此 MediaStore 将对此一无所知。

    请让您的服务器生成缩略图,然后您可以使用image loading library (如Picasso)来获取该缩略图。

    【讨论】:

    • 我喜欢这样的答案。尤其是最后一段。确实,我盲目地复制粘贴了代码(主要原因是我根本不知道这种方法是否正确)。你确实回答了我的主要问题。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2011-07-14
    • 1970-01-01
    • 1970-01-01
    • 2011-03-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多