【问题标题】:Does ImageView.setImageURI(Uri uri) work with remote files?ImageView.setImageURI(Uri uri) 是否适用于远程文件?
【发布时间】:2010-06-19 17:11:30
【问题描述】:

是否可以使用 ImageView.setImageURI(Uri uri) 从远程服务器加载图像?

【问题讨论】:

  • 你还没试过?我一直在使用this 方法。编辑:Here 的另一个很酷的例子。
  • 顺便说一下,是ImageView.setImageURI(Uri uri) (URI != Uri) -- 差别很大。

标签: android


【解决方案1】:

简短的回答是否定的!不能。

例如,如果 uri 包含对本地文件的引用,则可以使用 ImageView.setImageURI(Uri uri)。 例如:file:///sdcard/images/thumb.png

【讨论】:

    【解决方案2】:

    要从目录加载图像,应首先将其转换为Drawable。这是一段可以提供帮助的代码:

    File file = new File ("/sdcard/1.jpg");
    
    ImageView imageView = (ImageView) findViewById(R.id.icon);
    
    imageView.setImageDrawable(Drawable.createFromPath(file.getAbsolutePath()));
    

    请注意,ImageView 的另一种方法称为setImageURI(URI uri)。该方法用于加载外部文件;它不适用于File 类型。 例如,此代码不起作用:

    File file = new File ("/sdcard/1.jpg");
    
    ImageView imageView = (ImageView) findViewById(R.id.icon);
    
    imageView.setImageURI(Uri.fromFile(file));
    

    感谢 Martin Wibbels this post

    【讨论】:

    • 最后一个代码示例确实对我有用(API 级别 15 左右)。也许这是一个新事物。但还要注意setImageURI() 的参数是Uri,而不是URI
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-06-21
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-02-07
    相关资源
    最近更新 更多