본문 바로가기

Android

[Thead]자동저장 스레드

반응형

자동저장 스레드

 

import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;

import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.util.Log;
import android.widget.LinearLayout;

/**
 *
 * 이미지 자동 저장 쓰레드
 *
 * @author 

 * @since 

 */
public class AutosaveThread extends Thread {
 private final static String TAG = "EditImageAutoSave(Thread)";

 /** 옵션의 자동저장 체크 상태 */
 private boolean isAutoSave = false;
 
 /** 편집 액티비티의 상태 */
 private boolean isEditMode = false;
 
 /** 자동저장 시간 설정값 */
 private long saveDelay = 5000;

 private Context mContext;
 private LinearLayout mainView;
 public AutosaveThread() {
 }

 public AutosaveThread(Context mContext, boolean autoSave, boolean editMode, long delay) {
  this.isAutoSave = autoSave;
  this.isEditMode = editMode;
  this.saveDelay = delay;
  this.mContext = mContext;
 }

 @Override
 public void run() {
  super.run();
  while (isAutoSave && getEditMode()) {
   try {
    Thread.sleep(saveDelay);
   } catch (InterruptedException e) {
    Log.e(TAG, e.getMessage());
   }

   if (isAutoSave && getEditMode()) {
    // 이미지 자동 저장 처리코드.
    StringBuilder sdcardPath = new StringBuilder();
    sdcardPath.append(android.os.Environment.getExternalStorageDirectory().getAbsolutePath());
    File sdImageMainDirectory = new File(sdcardPath.append("/ImageStickerEditFolder").toString());
    if (!sdImageMainDirectory.isDirectory()) {
     sdImageMainDirectory.mkdirs();
    }
    FileOutputStream outputStream;
    BufferedOutputStream bos;
    mainView = (LinearLayout) ((Activity)mContext).findViewById(R.id.LinearLayout01);
    mainView.setDrawingCacheEnabled(true);

    Bitmap saveBitmap = mainView.getDrawingCache();
    try {
     Log.i(TAG, "Save image file = " + sdImageMainDirectory.toString());
     outputStream = new FileOutputStream(sdImageMainDirectory.toString() + "/.temp.TMP");
     bos = new BufferedOutputStream(outputStream);
     saveBitmap.compress(CompressFormat.JPEG, 100, bos);
     bos.flush();
     bos.close();

    } catch (FileNotFoundException e) {
     Log.i(TAG, "saveEditImage = " + e.toString());
    } catch (IOException e) {
     Log.i(TAG, "saveEditImage = " + e.toString());
    }
    
    Log.i(TAG, "Save Image(auto save mode).");
   }
  }
 }

 public void setEditMode(boolean isEditMode) {
  this.isEditMode = isEditMode;
 }

 public boolean getEditMode() {
  return this.isEditMode;
 }
}


반응형