검색결과 리스트
Android Image에 해당되는 글 1건
- 2012.03.08 [Android] Image Read and Download
글
서버상에서 이미지를 읽어와서 이미지 뷰에 보이고 또한 다운로드 받는 예제이다.
다운받은 파일은 DDMS 상에서 확인이 가능하다.
public class ImageViewActivity extends Activity{
ImageView m_imgview;
public void onCreate(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
m_imgview = (ImageView)findViewById(R.id.img);
Button draw_btn = (Button)findViewById(R.id.draw);
draw_btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
String imgurl="파일경로";
{
InputStream is;
try {
is = new URL(imgurl).openStream();
Bitmap bmp = BitmapFactory.decodeStream(is);
m_imgview.setImageBitmap(bmp);
is.close();
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
});
Button down_btn = (Button)findViewById(R.id.down);
down_btn.setOnClickListener(new Button.OnClickListener(){
public void onClick(View v){
String imgurl="파일경로";
int index = imgurl.lastIndexOf('/');
String localimg = imgurl.substring(index+1);
String path = Environment.getDataDirectory().getAbsolutePath();
path += "/data/imageview.test/"+localimg;
if(new File(path).exists()==false){
DownLoadImage(imgurl,localimg);
}
Bitmap bitmap = BitmapFactory.decodeFile(path);
m_imgview.setImageBitmap(bitmap);
}
});
}
boolean DownLoadImage(String url, String filename){
URL imgurl;
int read;
try{
imgurl = new URL(url);
HttpURLConnection connect = (HttpURLConnection)imgurl.openConnection();
int length = connect.getContentLength();
byte[] data = new byte[length];
InputStream is = connect.getInputStream();
FileOutputStream fos = openFileOutput(filename,0);
for(;;){
read = is.read(data);
if(read<=0) break;
fos.write(data,0,read);
}
is.close();
fos.close();
connect.disconnect();
}
catch(Exception ex){return false;}
return true;
}
}
RECENT COMMENT