为什么图像不加载? [英] Why image not loading?
问题描述
我的工作演示为我的项目。
I am working a demo for my project.
XML文件是如下:
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="${relativePackage}.${activityClass}" >
<ImageView
android:id="@+id/imageview"
android:layout_height="match_parent"
android:layout_width="match_parent"
android:visibility="visible"
android:layout_gravity="center"
android:contentDescription="@string/content_description"
/>
</LinearLayout>
和我的活动
是继到图像加载到的ImageView
。
实施的AsyncTask
的内部类。
And my Activity
is following to load the image into ImageView
.
Implemented AsyncTask
as inner class.
public class LoadImageActivity extends Activity {
ImageView image ;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_image);
image = (ImageView)findViewById(R.id.imageview);
String url = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";
LoadImageAsync loadImageAsync = new LoadImageAsync(url, image);
loadImageAsync.execute(url);
}
在活动
的的onCreate
方法我称之为的AsyncTask
加载图片
In Activity
's onCreate
method I called the AsyncTask
to load image
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.load_image);
image = (ImageView)findViewById(R.id.imageview);
String url = "http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png";
LoadImageAsync loadImageAsync = new LoadImageAsync(url, image);
loadImageAsync.execute(url);
}
在的AsyncTask
类,我有以下code这是我遵循的与此相关的堆栈溢出的链接。
In AsyncTask
class I have the following code which is I followed the Stack Overflow link related to this.
该AsyncTask的类下面是我实现的内部类Activity类。
The AsyncTask class is below which I implemented as inner class to the Activity class.
public class LoadImageAsync extends AsyncTask<String, Void, Bitmap>{
private String url;
private ImageView imageView;
public LoadImageAsync(String url, ImageView imageView) {
this.url = url;
this.imageView = imageView;
}
@Override
protected Bitmap doInBackground(String... params) {
try {
URL urlConnection = (URL) new URL(params[0]);
HttpURLConnection connection = (HttpURLConnection) urlConnection
.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
Bitmap myBitmap = BitmapFactory.decodeStream(input);
return myBitmap;
} catch (Exception e) {
e.printStackTrace();
}
return null;
}
@Override
protected void onPostExecute(Bitmap result) {
imageView.setImageBitmap(result);
super.onPostExecute(result);
}
}
什么错了,我做了?或任何错失?
What wrong I did? or anything missed?
什么是所有需要的图像时考虑的?
What are all need to consider when working on images?
和在此之后将转移到加载图像与 GridView控件
。
And after this will move to loading images with GridView
.
推荐答案
我会建议使用毕加索
Picasso.with(上下文).load(http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png\").into(image);
I would recommend to use Picasso Picasso.with(context).load("http://java.sogeti.nl/JavaBlog/wp-content/uploads/2009/04/android_icon_256.png").into(image);
所以你不必担心OutMemoryException这很可能会发生时,你会实现这个GridView中的
So you don't need to worry about OutMemoryException that will most probably happen when you will implement this in the GridView
这篇关于为什么图像不加载?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!