728x90
반응형
1. Async 클래스 사용
- AsynTask 클래스
- 작업 스레드와 관련된 복잡한 부분을 쉽게 처리해주는 클래스
- onPreExecute() : UI 스레드에서 실행되며, 작업을 설정하는데 사용됨
- doInBackground() : 작업 스레드에서 수행할 작업을 기술하는 곳임
- onProgressUpdate() : UI 스레드에서 호출되고, 작업 진행률 업데이트에 사용됨
- onPostExecute() : 작업 완료되면 UI에 결과를 업데이트하는데 사용됨
- AsyncTask<Params, Progress, Result>
- Params : 실행 시에 작업에 전달되는 값의 타입임
- Progress : 작업 진행 정도를 나타내는 값의 타입
- Result : 작업 결과 값을 나타내는 값의 타입
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="kr.co.imagedownload">
<uses-permission android:name="android.permission.INTERNET"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/Theme.ImageDownload">
<activity
android:name=".MainActivity"
android:exported="true">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity">
<EditText
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:id="@+id/editText"
android:inputType="textUri"
android:text="https://www.apple.com/v/imac/n/images/overview/imac27__evbw33241ouq_small_2x.jpg"/>
<Button
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="Download"
android:id="@+id/button"/>
<ImageView
android:layout_width="fill_parent"
android:layout_height="223dp"
android:id="@+id/imageview"/>
</LinearLayout>
package kr.co.imagedownload;
import androidx.appcompat.app.AppCompatActivity;
import android.app.ProgressDialog;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;
public class MainActivity extends AppCompatActivity {
public String URL = "";
EditText editText;
ImageView imageView;
Button button;
ProgressDialog mProgressDialog;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getSupportActionBar().setDisplayShowHomeEnabled(true);
getSupportActionBar().setIcon(R.drawable.ic_launcher);
editText = findViewById(R.id.editText);
button = findViewById(R.id.button);
imageView = findViewById(R.id.imageview);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
URL = editText.getText().toString();
new DownloadImage().execute(URL);
}
});
}
//AsyncTask 클래스 이용하여 스레드를 간접적으로 사용함
private class DownloadImage extends AsyncTask<String, Void, Bitmap>{
@Override
protected void onPreExecute() {
super.onPreExecute();
mProgressDialog = new ProgressDialog(MainActivity.this);
mProgressDialog.setTitle("이미지 다운로드 - 네트워크");
mProgressDialog.setMessage("이미지 다운로드 중입니다.");
mProgressDialog.setIndeterminate(false);
mProgressDialog.show();
}
@Override
protected Bitmap doInBackground(String... url) {
String imageURL = url[0];
Bitmap bitmap = null;
try {
//네트워크 이미지
InputStream input = new URL(imageURL).openStream();
bitmap = BitmapFactory.decodeStream(input);
} catch (IOException e) {
e.printStackTrace();
}
return bitmap;
}
//다운로드된 이미지를 화면에 표시
@Override
protected void onPostExecute(Bitmap bitmap) {
super.onPostExecute(bitmap);
imageView.setImageBitmap(bitmap);
mProgressDialog.dismiss();
}
}
}
[결과 출력]
728x90
반응형
'App > Android Java' 카테고리의 다른 글
Android_Java(34) - ServiceLifeCycle (0) | 2022.02.23 |
---|---|
Android_Java(33) - MusicService (0) | 2022.02.23 |
Android_Java(31) - AsyncTask (0) | 2022.02.15 |
Android_Java(30) - ThreadBasic(2) (0) | 2022.02.15 |
Android_Java(29) - ThreadBasic (1) | 2022.02.15 |
댓글