【问题标题】:How can I download and set an Image on a ImageView on Android app? [duplicate]如何在 Android 应用程序的 ImageView 上下载和设置图像? [复制]
【发布时间】:2014-12-08 03:43:45
【问题描述】:

大家好,上网的朋友们,我找到了一些可以下载的 sn-p,并在 Android 上的 ImageView 上设置图像。但这似乎不起作用,因为它找不到资源...这是我的sn-p

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)

【问题讨论】:

    标签: android imageview android-imageview


    【解决方案1】:

    使用方形的Picasso.. 太棒了

    使用它,您可以使用单行代码在 imageview 中加载图像。例如

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

    【讨论】:

    • 超级图书馆..我关注它
    • 使用 Picasso 库,这是唯一无需操作即可显示图像的行? Picasso.with(this).load("i.imgur.com/DvpvklR.png").into((Target)v.findViewById(R.id.place_image));
    • @Akhil 非常感谢!!我用毕加索解决了^^惊人的图书馆!
    • 太简单了!
    【解决方案2】:

    您提供给 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);
    

    就是这样!

    【讨论】:

    • 问题是我的应用程序中没有 bg_main,但我有一个格式为 bmp 的输入流......你在这里:
    • 输入流是; is = conn.getInputStream();位图 bmImg = BitmapFactory.decodeStream(is);
    • 那我相信@Hunkeone 的答案就是你要找的
    【解决方案3】:

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

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

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

    【讨论】:

    • 我会尝试,我会评论结果:)
    • 不起作用@Hunkeone:/我在编辑日志中发布了日志
    • 这意味着你的位图是空的 Bitmap bmImg = BitmapFactory.decodeStream(is);像这样检查: ImageView imageview = (ImageView)v.findViewById(R.id.place_image); if(bmImg!=null){ imageview.setImageBitmap(bitmap); imageview.setAdjustViewBounds(true); Drawable drawable= new BitmapDrawable(getResources(),bitmap); imageview.setImageDrawable(drawable); }else{ imageview.setImageDrawable(R.Layout.yourPicture) }
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-09-13
    • 2013-04-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-01-24
    相关资源
    最近更新 更多