如何在Android应用上的ImageView上下载和设置图像?

时间:2014-10-13 14:24:29

标签: android imageview android-imageview

大家好,网上冲浪我找到了一些代码片段,在Android上的ImageView上设置了一个图片。但它似乎不起作用,因为它找不到资源......这是我的代码片段

String user_image_url = "http://www.nuoto.it/foto_news/papera";
URL url;
try {
    url = new URL(user_image_url);
    HttpURLConnection conn;
    try {
        conn = (HttpURLConnection) url.openConnection();
        conn.setDoInput(true); 
        conn.connect();
        InputStream is;
        is = conn.getInputStream();
        Bitmap bmImg = BitmapFactory.decodeStream(is);
    } catch (IOException e1) {
    // TODO Auto-generated catch block
    e1.printStackTrace();
}   


String uri = "@drawable-hdpi/bg_main.png";
int imageResource = getResources().getIdentifier(uri, null, getPackageName()); //Here it give me the error (Resource not found)

ImageView imageview = (ImageView)v.findViewById(R.id.place_image);
Drawable res = getResources().getDrawable(imageResource);
imageview.setImageDrawable(res);
} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

非常感谢

@Hunkeone的Logcat:

10-13 16:55:57.926: E/AndroidRuntime(4380): FATAL EXCEPTION: main
10-13 16:55:57.926: E/AndroidRuntime(4380): Process: com.example.findmyclients, PID: 4380
10-13 16:55:57.926: E/AndroidRuntime(4380): java.lang.NullPointerException
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.content.ContextWrapper.getResources(ContextWrapper.java:94)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.example.findmyclients.BuildInfoMatrix.Read_Matrix(BuildInfoMatrix.java:172)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.example.findmyclients.WindowFeature.show(WindowFeature.java:51)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.example.findmyclients.MainActivity$7.onInfoWindowClick(MainActivity.java:679)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.android.gms.maps.GoogleMap$9.e(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.android.gms.maps.internal.f$a.onTransact(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.os.Binder.transact(Binder.java:361)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.android.gms.maps.internal.ai.a(SourceFile:82)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.c.ae.h(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.c.g.b(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.o.bo.aK_(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.o.bo.a(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.o.ca.c(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.o.am.onSingleTapConfirmed(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.i.g.onSingleTapConfirmed(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.google.maps.api.android.lib6.gmm6.i.i.handleMessage(Unknown Source)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.os.Handler.dispatchMessage(Handler.java:102)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.os.Looper.loop(Looper.java:146)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at android.app.ActivityThread.main(ActivityThread.java:5602)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at java.lang.reflect.Method.invokeNative(Native Method)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at java.lang.reflect.Method.invoke(Method.java:515)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1283)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1099)
10-13 16:55:57.926: E/AndroidRuntime(4380):     at dalvik.system.NativeStart.main(Native Method)

3 个答案:

答案 0 :(得分:2)

使用方形的Picasso ..真棒

使用此功能,您可以使用单行代码在imageview中加载图像。 e.g。

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView);
  • 内置支持磁盘&内存缓存(Lrucache),这是 在Listview / Grid视图的情况下,需要更快的图像检索。
  • 从服务器异步下载&设置图像 ImageView完成图像下载。

答案 1 :(得分:1)

您为String uri提供的路径应该只是@drawable/bg_main,但有一些简单的方法可以将图像分配给ImageView。而不是做:

String uri = "@drawable-hdpi/bg_main.png";
int imageResource = getResources().getIdentifier(uri, null, getPackageName()); 
ImageView imageview = (ImageView)v.findViewById(R.id.place_image);
Drawable res = getResources().getDrawable(imageResource);
imageview.setImageDrawable(res);

你可以做到

ImageView imageview = (ImageView)v.findViewById(R.id.place_image);
imageview.setImageDrawable(R.drawable.bg_main);

就是这样!

答案 2 :(得分:1)

收到位图图像后,使用可绘制位图将其设置为布局。

ImageView imageview = (ImageView)v.findViewById(R.id.place_image);
imageview.setImageBitmap(bitmap);
imageview.setAdjustViewBounds(true);
Drawable drawable= new BitmapDrawable(getResources(),bitmap);
imageview.setImageDrawable(drawable);

我还建议制作默认图像,以防图像未上传(没有互联网连接等。