마스터Q&A 안드로이드는 안드로이드 개발자들의 질문과 답변을 위한 지식 커뮤니티 사이트입니다. 안드로이드펍에서 운영하고 있습니다. [사용법, 운영진]

인터넷에서 이미지를 얻고 싶습니다 도와주세요

0 추천

인터넷 URL형식을 가져와서 이미지를 이미지뷰에 가져오려고 하는데 되지를 않네요..ㅜㅜ

소스코드는 인터넷에서 본걸로 했는데

뭐가 문제인지 잘 되지 않네요 .ㅜㅜㅜ

<액티비티>

                  
       findViewById(R.id.btn02).setOnClickListener(new OnClickListener() {
         
        @Override
        public void onClick(View v) {
            //정보가저오기
            Cursor cursor = handler.select();
           
            startManagingCursor(cursor);
            while (cursor.moveToNext()) {
                int _id =cursor.getInt(cursor.getColumnIndex("_id"));
                String name =cursor.getString(cursor.getColumnIndex("name"));
                String age =cursor.getString(cursor.getColumnIndex("age"));
                String phone =cursor.getString(cursor.getColumnIndex("phone"));
                String addr =cursor.getString(cursor.getColumnIndex("addr"));
                String img=cursor.getString(cursor.getColumnIndex("image"));
                
                tv01.setText("seq="+_id+"//"+"이름="+name +"//"+"나이="+age+"//"+"폰="+phone+"//"+"주소="+addr+"//"+"이미지="+img);
                
                Bitmap bitmap=null;
                

           try {
            URL url=new URL(img);
            //웹사이트에 접속
            HttpURLConnection conn = (HttpURLConnection) url.openConnection();
            
            //데이터 가져옴
            int imageSize=conn.getContentLength();
            //스트림에 넣어줌
            BufferedInputStream bis=new BufferedInputStream(conn.getInputStream(),imageSize);
            //비트맵에 넣어줌
            bitmap=BitmapFactory.decodeStream(bis);
            bis.close();
     } catch (MalformedURLException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     } catch (IOException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
           
           img01.setAdjustViewBounds(true);
           img01.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
           img01.setScaleType(ImageView.ScaleType.FIT_XY);
           img01.setImageBitmap(bitmap);
\            }      
        }
    });    
    }  
}

 

 

<메인.xml>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context="${packageName}.${activityClass}" >

    <ImageView
        android:id="@+id/imageView1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignLeft="@+id/tv01"
        android:layout_below="@+id/btn01"
        android:layout_marginTop="99dp"
        android:src="@drawable/abc_list_selector_disabled_holo_light" />

</RelativeLayout>

 

 

개굴이 (220 포인트) 님이 2014년 6월 9일 질문
개굴이님이 2014년 6월 9일 수정

2개의 답변

0 추천
ImageView에는 어디서 세팅하나요??
YeonMin (17,860 포인트) 님이 2014년 6월 9일 답변
ImageVIew 를 레이아웃에  xml파일에 넣어놨기만 하였습니다.
그럼 ImageView 를 findViewById를 이용해서 뷰 연결하고 setImageBitmap하시면 됩니다. 위 소스를 보니 img01이 있는데 뷰 연결이 제대로 되지 않았을 수도 있습니다.
0 추천
네트워크 관련 작업은 별도의 쓰레드로 분리하여 다시 시도해보시길 바랍니다.
인연 (31,880 포인트) 님이 2014년 6월 10일 답변
...