티스토리 뷰
[Android] calling ui thread from worker thread
Method compress must be called from the worker thread, currently inferred thread is UI thread less... (Ctrl+F1)
Inspection info:Ensures that a method which expects to be called on a specific thread, is actually called from that thread. For example, calls on methods in widgets should always be made on the UI thread
위와 같이 View와 같은 Class내에서 Bitmap Compress 함수 사용시
Activity의 context 의 runOnUiThread 에서 수행해주도록 아래와 같이 구현한다.
public static Bitmap croppedBitmap;
public static File croppedFile;
File sdCard = Environment.getExternalStorageDirectory();
File dir = new File(sdCard.getAbsolutePath() + "/" + Data.PIC_FOLDERNAME + "/" + Data.PIC_FILENAME);
if (dir.exists() == false) {
dir.mkdir();
}
String path = sdCard.getAbsolutePath() + "/" + Data.PIC_FOLDERNAME + "/" + Data.PIC_FILENAME + "/" + Data.CROP_FILENAME;
croppedFile = new File(path);
((CropActivity) getContext()).runOnUiThread(new Runnable() {
public void run() {
// things need to work on ui thread
Bitmap croppedBitmap = CropImageView.croppedBitmap;
FileOutputStream out;
try {
out = new FileOutputStream(croppedFile);
croppedBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
}
}
});
아래와 같이 UI Thread 이용하며 CropActivity는 위의 코드가 동작하는 View와 같은 Class가 호출되어 Activity 로 동작하는 Class 이다.
((CropActivity) getContext()).runOnUiThread(new Runnable() {
public void run() {
// things need to work on ui thread
}
});
'Computer > Android' 카테고리의 다른 글
[Android] Context removed error when activity destroyed (0) | 2019.06.16 |
---|---|
[Android] 다른 Activity의 ListView새로고침하기 (notifyDataSetChanged) (0) | 2019.05.15 |
Google Android Sample Code Git (0) | 2019.02.09 |
구글계정 지급보류 문제해결 (0) | 2019.02.04 |
Android 파일명 변경시 (0) | 2019.01.28 |