gpt4 book ai didi

android - 如何让图像从 URL 加载? (关于 SO 的所有答案均无效/已弃用)

转载 作者:搜寻专家 更新时间:2023-11-01 08:03:04 25 4
gpt4 key购买 nike

我正在尝试构建一个我想在其中显示一些图像的应用。

在真正开始之前,我已经遇到了一个问题:我无法让 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" />

我检查了链接,更改了好几次,检查了我的互联网连接……但我只有一个空白屏幕。没有任何反应。

我想我缺少一些非常基本的东西,我需要任何其他许可吗?

你能看出什么不对吗?

编辑

使用 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);

-.- ....另一方面,Tasomaniac 的回答效果很好:刚刚添加了 Picasso jar 并且:

 ImageView imgView =(ImageView)findViewById(R.id.imageView1);

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

不需要 Asynctask 或复杂的废话。

谢谢!

最佳答案

使用 Square Inc 的 Picasso 库。它需要以下 1 行代码,仅此而已。

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

https://github.com/square/picasso/

关于android - 如何让图像从 URL 加载? (关于 SO 的所有答案均无效/已弃用),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18302377/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com