2014-10-13 3 views
0

안녕하세요, 인터넷 서핑 안드로이드에서 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 당신에게

로그 캣을 정말 고마워요 내 조각입니다 ... 여기 자원이 발견되지 않기 때문에 그 행위에서 난 것이 아니니 않는 것 같다

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) 

답변

1

그것의 멋진

이것을 사용하여 코드의 한 줄로 imageview에서 이미지를로드 할 수 있습니다. 예 :

Picasso.with(context).load("http://i.imgur.com/DvpvklR.png").into(imageView); 
  • 이리스트 뷰/그리드보기의 경우 빠른 영상 취득에서는 필요한 인 & 디스크 캐시 메모리 (Lrucache)의 붙박이 지원한다.
  • 서버 비동기로 이미지를 다운로드 & 이미지 설정 이미지 다운로드 완료시 이미지보기.
+0

슈퍼 라이브러리 .. 나는이 표시하는 유일한 행입니다 그것은 피카소 라이브러리 –

+0

에 대한보고 조작하지 않고 이미지? Picasso.with (this) .load ("http://i.imgur.com/DvpvklR.png") .into ((대상) v.findViewById (R.id.place_image)); –

+0

@Akhil 너무 많은 감사합니다! 피카소 ^^ 놀라운 라이브러리로 해결했습니다 –

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

을 할 수 그리고 바로 그거야!

+0

문제는 응용 프로그램에 bg_main이 없지만 bmp로 서식이 지정된 입력 스트림이 있습니다 ... 여기 있습니다 : –

+0

InputStream is; \t \t \t \t \t \t \t = conn.getInputStream(); \t \t \t \t \t \t \t 비트 맵 bmImg = BitmapFactory.decodeStream (is); –

+0

그렇다면 나는 @ Hunkeone의 대답이 당신이 찾고있는 것이라고 믿는다. –

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); 
나는 또한 업로드되지 않은 경우 이미지에 기본 이미지를 만들기 위해 추천 할 것입니다

(인터넷에 연결되지 않음 등

+0

나는 시도 할 것이고 나는 결과를 주석으로 말할 것이다. :) –

+0

@Hunkeone :/나는 편집에서 logcat을 게시한다. –

+0

즉, 비트 맵이 비어 있음을 의미한다. 비트 맵 bmImg = 비트 맵 팩토리.decodeStream (있는); 다음과 같이 확인하십시오. ImageView imageview = (ImageView) v.findViewById (R.id.place_image); if (bmImg! = null) { imageview.setImageBitmap (비트 맵); imageview.setAdjustViewBounds (true); 드로어 블 드로어 블 = 새로운 BitmapDrawable (getResources(), 비트 맵); imageview.setImageDrawable (drawable); } else { imageview.setImageDrawable (R.Layout.yourPicture) } – Hunkeone

관련 문제