2015-11-09 189 views
0

我想在ImageView中显示缩略图视频。视频由用户上传并存储在服务器上。目前,我还没有设置任何存储和上传视频的机制,因此我正在使用使用http协议访问的示例视频文件进行测试。但是,this后说,如果URI方案的形式为内容的不用http显示视频的缩略图

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(); 
    } 
    } 
} 

回答

1

有你的代码的问题。

您的getRealPathFromURI()目前在大约6亿台Android设备上(运行Android 4.4或更高版本的所有设备)一般都处于打破状态,对于任何Uri值都是如此,更不用说您正在尝试使用的设备了。 A Uri is not a file。无论你从哪里得到Uri,它都可能不会指向MediaStore。即使它是来自MediaStore的东西,也不会从MediaStore通过DATA获得文件路径。即使可以获取文件路径,也可能无法访问该文件(例如,它存储在removable storage中)。

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

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

+0

我喜欢这样的答案。尤其是最后一段。事实上,我盲目复制和粘贴代码(主要原因是我不知道这种方法是否正确)。你确实回答了我的主要问题。 – mok