我正在尝试构建一个要在其中显示一些图像的应用程序。
在真正开始之前,我已经遇到了一个问题:我无法让 imageview 从 URL 加载图像......
我知道这是本主题中关于 SO 的最常见问题之一,下面我有一些我从中摘录的代码:
https://stackoverflow.com/a/12173580
所以,这是我的整个 MainActivity:
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" />
我检查了链接,更改了几次,检查了我的互联网连接......我只有一个空白屏幕。一点反应都没有。
我想我错过了一些非常基本的东西,我需要任何其他许可吗?
你能看出什么不对吗?
编辑
使用毕加索库:
小代码:
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
什么目标不能为空?...请帮助:)
EDIT2 忘记之前的编辑。我没有 setContentView(R.layout.main);
-.- .... 另一方面,Tasomaniac 的回答效果很好:刚刚添加了毕加索罐子和:
ImageView imgView =(ImageView)findViewById(R.id.imageView1);
Picasso.with(this).load("http://i.imgur.com/DvpvklR.png").into(imgView);
不需要 Asynctask 或复杂的废话。
谢谢你 !