我正在尝试构建一个应用程序,以便在其中显示一些图像。
在真正开始之前,我已经有一个问题:我无法获得从URL加载图像的imageview。。。
我知道这是这个主题中关于SO最常见的问题之一,下面我有一些代码:
https://stackoverflow.com/a/12173580
下面是我的全部主要活动:
public class MainActivity extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
Bitmap bitmap = DownloadImage("http://4.bp.blogspot.com/-w7q2YocdYpY/UCeb6SCfGoI/AAAAAAAAAWY/nsXfzrGQjyA/s1600/Yahoo+Messenger.png");
ImageView img = (ImageView) findViewById(R.id.imageView1);
img.setImageBitmap(bitmap);
}
private InputStream OpenHttpConnection(String urlString) throws IOException {
InputStream in = null;
int response = -1;
URL url = new URL(urlString);
URLConnection conn = url.openConnection();
if (!(conn instanceof HttpURLConnection))
throw new IOException("Not an HTTP connection");
try {
HttpURLConnection httpConn = (HttpURLConnection) conn;
httpConn.setAllowUserInteraction(false);
httpConn.setInstanceFollowRedirects(true);
httpConn.setRequestMethod("GET");
httpConn.connect();
response = httpConn.getResponseCode();
if (response == HttpURLConnection.HTTP_OK) {
in = httpConn.getInputStream();
}
} catch (Exception ex) {
throw new IOException("Error connecting");
}
return in;
}
private Bitmap DownloadImage(String URL) {
Bitmap bitmap = null;
InputStream in = null;
try {
in = OpenHttpConnection(URL);
bitmap = BitmapFactory.decodeStream(in);
in.close();
} catch (IOException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
return bitmap;
}
}
我有:
<uses-permission android:name="android.permission.INTERNET" />
我检查了链接,更改了好几次,检查了我的互联网连接。。。我只有一个空白屏幕。没有任何反应。
我想我错过了一些基本的东西,我需要其他许可吗?
你能看出什么不对吗?
编辑
使用Picasso库:
小代码:
ImageView imgView =(ImageView)findViewById(R.id.imageView1);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(imgView);
大型Logcat错误,应用程序在启动时崩溃。
08-19 00:29:56.970: E/AndroidRuntime(4731): FATAL EXCEPTION: main
08-19 00:29:56.970: E/AndroidRuntime(4731): java.lang.RuntimeException: Unable to start activity ComponentInfo{com.exampl.aaa/com.exampl.aaa.MainActivity}: java.lang.IllegalArgumentException: Target must not be null.
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2077)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2104)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.app.ActivityThread.access$600(ActivityThread.java:134)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1247)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.os.Handler.dispatchMessage(Handler.java:99)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.os.Looper.loop(Looper.java:154)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.app.ActivityThread.main(ActivityThread.java:4624)
08-19 00:29:56.970: E/AndroidRuntime(4731): at java.lang.reflect.Method.invokeNative(Native Method)
08-19 00:29:56.970: E/AndroidRuntime(4731): at java.lang.reflect.Method.invoke(Method.java:511)
08-19 00:29:56.970: E/AndroidRuntime(4731): at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:809)
08-19 00:29:56.970: E/AndroidRuntime(4731): at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:576)
08-19 00:29:56.970: E/AndroidRuntime(4731): at dalvik.system.NativeStart.main(Native Method)
08-19 00:29:56.970: E/AndroidRuntime(4731): Caused by: java.lang.IllegalArgumentException: Target must not be null.
08-19 00:29:56.970: E/AndroidRuntime(4731): at com.squareup.picasso.RequestBuilder.into(RequestBuilder.java:317)
08-19 00:29:56.970: E/AndroidRuntime(4731): at com.exampl.aaa.MainActivity.onCreate(MainActivity.java:33)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.app.Activity.performCreate(Activity.java:4479)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1050)
08-19 00:29:56.970: E/AndroidRuntime(4731): at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2041)
08-19 00:29:56.970: E/AndroidRuntime(4731): ... 11 more
什么目标不能为空?。。。请帮助:)
编辑2忘记以前的编辑。我没有setContentView(R.layout.main);
-.-。。。。另一方面,塔索马尼亚克的回答很好:只添加了毕加索的罐子和:
ImageView imgView =(ImageView)findViewById(R.id.imageView1);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(imgView);
不需要异步任务或复杂的垃圾。
谢谢!
使用Square股份有限公司的Picasso库。它需要以下1行代码,仅此而已。
Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
https://github.com/square/picasso/
我建议使用以下库,它负责图像加载/缓存等,只会让你的生活更轻松;-)
https://code.google.com/p/android-query/#Image_Loading
//fetch and set the image from internet, cache with file and memory
aq.id(R.id.image1).image("http://www.vikispot.com/z/images/vikispot/android-w.png");
特点:
- 简单
- 内存&文件缓存
- 向下采样
- 可缩放(WebView)
- 回退图像
- 预加载
- 动画
- 动态纵横比
- 避免重复的同时获取
- 自定义回调
我也有这个权限,不确定它是否必要,但值得一试:
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
我不确定你的问题出在哪里,但这是我的工作代码。我得去看看那些图书馆。
private Bitmap getBitmapFromURL(String str)
{
Bitmap bmp = null;
HttpURLConnection connection = null;
try
{
URL url = new URL(str);
connection = openConnectionWithTimeout(url);
BitmapFactory.Options opt = new Options();
// opt.inPurgeable=true;
opt.inSampleSize = 6;
bmp = BitmapFactory.decodeStream(connection.getInputStream(), null, opt);
}
catch (IOException e)
{
e.printStackTrace();
}
finally
{
if (connection != null)
connection.disconnect();
}
return bmp;
}
public static HttpURLConnection openConnectionWithTimeout(URL url) throws IOException
{
HttpURLConnection connection = (HttpURLConnection) url.openConnection();
connection.setConnectTimeout(MAX_TIME);
connection.setReadTimeout(MAX_TIME);
return connection;
}
在不使用第三方库的情况下,您可以执行以下操作:
private Bitmap getBitmap(String url) {
try{
HttpURLConnection con = (HttpURLConnection)new URL(url).openConnection();
con.setDoInput(true);
con.connect();
InputStream input = con.getInputStream();
Bitmap bmp = BitmapFactory.decodeStream(input);
input.close();
return bmp;
}catch(Exception e){
Log.v("SHOW", e.getMessage());
return null;
}
}