[英]Url image to imageview
ImageView imgview = (ImageView)findViewById(R.id.image);
Drawable drawable = LoadImage("http://192.168.172.1/myproject/images/st1.jpg");
imgview.setImageDrawable(drawable);
...
...
public Drawable LoadImage(String url)
{
try
{
InputStream is = (InputStream) new URL(url).getContent();
Drawable b = Drawable.createFromStream(is, url);
return b;
}catch(Exception e){
System.out.println(e);
return null;
}
}
.java 的.java
...
...
<ImageView
android:id="@+id/image"
android:layout_height="wrap_content"
android:layout_width="wrap_content"/>
.xml .XML
getting the image from wamp server as a Url and display it onto my xml but image doesn't shown 从网址服务器获取图像并将其显示在我的xml上,但未显示图像
I use the following code
to get an image
from url
and add it to an ImageView
: 我使用以下
code
从url
获取image
并将其添加到ImageView
:
ImageRequest imgRequest = new ImageRequest(url,
new Response.Listener<Bitmap>() {
@Override
public void onResponse(Bitmap response) {
mImageView.setImageBitmap(response);
Toast.makeText(MainActivity.this, "Succes", Toast.LENGTH_SHORT).show();
}
}, 0, 0, ImageView.ScaleType.FIT_XY, Bitmap.Config.ARGB_8888, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
Toast.makeText(MainActivity.this, error.getMessage(), Toast.LENGTH_SHORT).show();
mImageView.setBackgroundColor(Color.parseColor("#ff0000"));
error.printStackTrace();
}
});
Volley.newRequestQueue(this).add(imgRequest);
All you need to do is to use Volley Library
made by google developers
(trusted source). 您需要做的就是使用
google developers
制作的Volley Library
(受信任的来源)。 More info here 更多信息在这里
Hope it helps! 希望能帮助到你!
Use http://square.github.io/picasso/ it's really easy to implement, follow the following steps:- 使用http://square.github.io/picasso/确实很容易实现,请执行以下步骤:
1- If you haven't done it already. 1-如果尚未完成。 If you are using eclipse as your development IDE, then just copy the downloaded picasso-2.5.2.jar file into your application lib folder.
如果您使用eclipse作为开发IDE,则只需将下载的picasso-2.5.2.jar文件复制到您的应用程序lib文件夹中。 If you are using Android Studio IDE, then you have to add below dependency in build.gradle file.
如果使用的是Android Studio IDE,则必须在build.gradle文件中添加以下依赖项。
dependencies {
...
compile "com.squareup.picasso:picasso:2.5.2"
...
}
2- Now let us download the image and display on imageView:- 2-现在让我们下载图像并显示在imageView上:
//Loading image from below url into imageView
Picasso.with(this)
.load("YOUR IMAGE URL HERE")
.into(imageView);//Your image view
That's all, Hope this will help you !! 仅此而已,希望对您有所帮助!!
使用毕加索效果很好,易于使用。
Are you given internet permission on manifest 您是否获得清单上的互联网许可?
and running this code in background such as asyn task 并在后台(例如asyn任务)中运行此代码
class LoadImageLoader extends AsyncTask<Void, Void, Void>{
@Override
protected Void doInBackground(Void... params) {
// TODO Auto-generated method stub
drawable = LoadImage("http://i.imgur.com/DvpvklR.png");
return null;
}
@Override
protected void onPostExecute(Void result) {
// TODO Auto-generated method stub
super.onPostExecute(result);
imgview.setImageDrawable(drawable);
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.