2015-11-03 87 views
0

我试图从url加载图像到我的android ImageView。但它没有给我的网址形象。但是当我打电话另一个样本网址加载在ImageView的URL图像将不会设置为ImageView Android

我的URL这给空

https://192.168.100.15/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102 

此示例网址为我的作品

https://upload.wikimedia.org/wikipedia/commons/4/47/PNG_transparency_demonstration_1.png 

这是我的代码我正致力于

public class GetImage extends Activity{ 
ImageView postpic1; 
Bitmap b; 
@Override 
public void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.all_posts); 
    postpic1 = (ImageView) findViewById(R.id.postpic1); 
    information info = new information(); 
    info.execute(""); 
} 
public class information extends AsyncTask<String, String, String> 
{ 
    @Override 
    protected String doInBackground(String... arg0) { 
     try 
     { 
      URL url = new URL("https://localhost/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102"); 
      InputStream is = new BufferedInputStream(url.openStream()); 
      b = BitmapFactory.decodeStream(is); 
     } catch(Exception e){} 
     return null; 
    } 
    @Override 
    protected void onPostExecute(String result) { 
     postpic1.setImageBitmap(b); 

    } 
} 
} 

回答

1

只需使用图像加载和缓存库。例如Picasso

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 

替代解决方案是Glide;它有一个类似的工作principleIt也有类似的工作原理是:

Glide.with(this).load("http://goo.gl/gEgYUd").into(imageView); 
1

你有一个本地网络服务器运行支持HTTPS?因为那是你试图从中加载图像的地方。

另外,如果你已经运行,当你在你的浏览器中调用你想要的URL时,你会得到图像吗?

+0

这是URL '的https:// 192.168.100.15/HeyVoteWeb /主页/的getImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102' – Naz141

+0

是的,我看到了。这是对本地HTTPS网络服务器的调用,那么你有一个运行?如果没有,这个URL永远不会给你一张图片。这将调用您正在运行此操作的计算机/设备,而不是**网络上的随机图像(仅供说明)。 – LilaQ

+0

yes在您的浏览器中运行该网址 – Naz141

5

图片的URL是localhost。本地主机(127.0.0.1)是指与请求源相同的机器。所以,你的手机向自己发送请求。而是指定服务器正在运行的个人电脑的IP地址。

PS:确保您的PC和手机都连接到同一网络。

+0

@ Naz141:如果这个答案有帮助,你能否标记为已接受? – Srinivas

+0

我正在使用IP,您可以在我编辑的问题中看到 – Naz141

+0

@ Naz141:您是否在服务器上设置了SSL? – Srinivas

2

我认为你的问题是在你的URL中,用你的IP地址替换你的本地主机,希望它能解决你的问题。

0

您是否尝试过使用Picasso库非常简单而有效:

  1. 转到您的应用程序目录内的build.gradle并添加到依赖性:

    compile 'com.squareup.picasso:picasso:2.5.2'

  2. 然后用毕加索的lib :

    String url = "https://localhost/HeyVoteWeb/Home/GetImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102"; 
    

    Picasso.with(context) //The context of your activity .load(url) .into(postpic1);

+0

当我把我的下面的url它不加载imageview是空的。但对于所有其他图像的URL工作 '的https:// 192.168.100.15/HeyVoteWeb /主页/的getImage/d9cbd32c-47fc-4644-ab97-1f525c96e9ed/100000102' – Naz141

+0

我的图片URL我的工作我的浏览器。当我在浏览器中运行网址 – Naz141

+0

@ Naz141时,图像会自动下载,那么这就是你应该直接使用链接到img的问题,而不是链接下载img,它们是2种不同的链接,带有2种不同的功能 – commonSenseCode

相关问题