AndroidManifest.xml
<uses-permission android:name="android.permission.INTERNET" />
class MainActivity
package com.example.choi.mystudy29_3;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
final static String TAG="MainActivity";
String urlAddr="http://braverokmc.dothome.co.kr/include/images/beautiful/49.jpg";
String urlAddr2="http://braverokmc.dothome.co.kr/include/images/beautiful/30.jpg";
Button threadButton, asyncTaskButton;
ImageView ivNet;
MyThread thread;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
threadButton=(Button)findViewById(R.id.threadButton);
asyncTaskButton=(Button)findViewById(R.id.asyncTaskButton);
ivNet=(ImageView)findViewById(R.id.iv_network_img);
threadButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(thread!=null)thread.interrupt(); thread=null;
thread=new MyThread();
thread.setDaemon(true);
thread.start();
}
});
asyncTaskButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if(thread!=null)thread.interrupt(); thread=null;
NetWorkAsyncTask netWorkAsyncTask=new NetWorkAsyncTask(MainActivity.this, ivNet, urlAddr);
netWorkAsyncTask.execute(100);
}
});
}
class MyThread extends Thread{
ProgressDialog dialog;
String devicePath;
@Override
public void run() {
try {
//다이얼로그 호출
runOnUiThread(new Runnable() {
@Override
public void run() {
dialog();
}
});
//안드로이드 이미지 다운로드
imageDownLoad();
//안드로이드 에 내부저장소에서 저장된 이미지 불러오기
runOnUiThread(new Runnable() {
@Override
public void run() {
imageShow();
}
});
//다이얼로그 종료
if(dialog!=null)dialog.dismiss();
}catch (Exception e){
e.printStackTrace();
}
}
public void dialog(){
dialog=new ProgressDialog(MainActivity.this);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("스레드 Dialog");
dialog.setMessage("이미지 다운로드");
dialog.show();
}
public void imageDownLoad(){
InputStream is =null;
FileOutputStream fos=null;
int index=urlAddr2.lastIndexOf("/");
String imageName=urlAddr2.substring(index+1);
devicePath= Environment.getDataDirectory().getAbsolutePath()+"/data/com.example.choi.mystudy29_3/files/"+imageName;
try {
URL url=new URL(urlAddr2);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setConnectTimeout(10000);
int len= 1024; //connection.getContentLength();
byte[] bs =new byte[len];
if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
is=connection.getInputStream();
fos=MainActivity.this.openFileOutput(imageName, 0);
while (true){
int i=is.read(bs);
if(i<0)break;
fos.write(bs, 0, i);
}
}
//1초지연
try {
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(fos!=null)fos.close();
if(is!=null)is.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
}
public void imageShow(){
Bitmap bitmap= BitmapFactory.decodeFile(devicePath);
ivNet.setImageBitmap(bitmap);
}
}
}
R.layout.activity_main
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.choi.mystudy29_3.MainActivity">
<LinearLayout
android:id="@+id/line1"
android:orientation="horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true">
<Button
android:text="스레드"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/threadButton"
android:layout_weight="1"
android:background="@color/colorAccent"
android:textColor="@android:color/background_light" />
<Button
android:text="AsyncTask"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:id="@+id/asyncTaskButton"
android:layout_weight="1"
android:textAllCaps="false"
android:background="@android:color/holo_blue_dark"
android:textColor="@android:color/background_light" />
</LinearLayout>
<ImageView
android:layout_width="match_parent"
android:layout_height="match_parent"
android:id="@+id/iv_network_img"
android:layout_alignParentTop="true"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true" />
</RelativeLayout>
class NetWorkAsyncTask
package com.example.choi.mystudy29_3;
import android.app.ProgressDialog;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Environment;
import android.util.Log;
import android.widget.ImageView;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import static android.content.Context.MODE_PRIVATE;
/**
* Created by choi on 2017-03-18.
*/
public class NetWorkAsyncTask extends AsyncTask<Integer, String, Integer > {
final static String TAG ="NetWorkAsyncTask";
Context mContext;
String mAddr;
ProgressDialog dialog=null;
String devicePath;
ImageView ivNet;
public NetWorkAsyncTask(Context context, ImageView iv, String url) {
this.mContext=context;
this.ivNet=iv;
this.mAddr=url;
}
@Override
protected void onPreExecute() {
Log.i(TAG, "onPreExecute()");
dialog=new ProgressDialog(mContext);
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
dialog.setTitle("Async Dialog");
dialog.setMessage("down......");
dialog.show();
}
@Override
protected Integer doInBackground(Integer... params) {
Log.i(TAG, "doInBackground()");
Log.i(TAG, " params : "+params[0]);
int index=mAddr.lastIndexOf("/");
String imgName=mAddr.substring(index+1);
//이미지 안드로이드 저장 위치
devicePath= Environment.getDataDirectory().getAbsolutePath()+"/data/com.example.choi.mystudy29_3/files/"+imgName;
InputStream is =null;
FileOutputStream fos=null;
try {
URL url=new URL(mAddr);
HttpURLConnection connection=(HttpURLConnection)url.openConnection();
connection.setConnectTimeout(10000);
int len=connection.getContentLength(); //1024
byte[] bs=new byte[len];
if(connection.getResponseCode()==HttpURLConnection.HTTP_OK){
is=connection.getInputStream();
fos=mContext.openFileOutput(imgName, MODE_PRIVATE);
while (true){
int i=is.read(bs);
if(i<0)break;
fos.write(bs, 0, i);
}
//1초지연
try {
Thread.sleep(1000);
}catch (Exception e){
e.printStackTrace();
}
}
}catch (Exception e){
e.printStackTrace();
}finally {
try {
if(fos!=null)fos.close();
if(is!=null)is.close();
}catch (Exception e2){
e2.printStackTrace();
}
}
return null;
}
@Override
protected void onProgressUpdate(String... values) {
Log.i(TAG, "onProgressUpdate()");
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Integer integer) {
Log.i(TAG, "onPreExecute()");
//안드로이드 내부 저장소에 저장된 이미지 불러오기
Bitmap bitmap= BitmapFactory.decodeFile(devicePath);
ivNet.setImageBitmap(bitmap);
dialog.dismiss();
}
@Override
protected void onCancelled(Integer integer) {
Log.i(TAG, "onCancelled(Integer integer)");
super.onCancelled(integer);
}
@Override
protected void onCancelled() {
Log.i(TAG, "onCancelled()");
super.onCancelled();
}
}
댓글 ( 4)
댓글 남기기