Androidプロジェクト開発経験まとめ


1、画像byteデータ転送Bitmap
Bitmap bitmap=BitmapFactory.decodeFile(file.getAbsolutePath()を使用すると、メモリがオーバーフローしやすくなります.
public static Bitmap decodeImg(byte[] imgByte) {
        Bitmap bitmap = null;

        InputStream input = null;
        try {
            BitmapFactory.Options options = new BitmapFactory.Options();
            options.inSampleSize = 8;
            input = new ByteArrayInputStream(imgByte);
            SoftReference softRef = new SoftReference(BitmapFactory.decodeStream(input, null, options));
            bitmap = (Bitmap) softRef.get();

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if (imgByte != null) {
                imgByte = null;
            }

            if (input != null) {
                try {
                    input.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        }
        return bitmap;
    }

2、Timer+TimerTask+Handlerでタイマーを実現する
public class HanderDemoActivity extends Activity {  
    TextView tvShow;  
    private int i = 0;  
    private int TIME = 1000;  
  
    @Override  
    public void onCreate(Bundle savedInstanceState) {  
        super.onCreate(savedInstanceState);  
        setContentView(R.layout.main);  
        tvShow = (TextView) findViewById(R.id.tv_show);  
        timer.schedule(task, 1000, 1000); // 1s   task,  1s      
    }  
  
    Handler handler = new Handler() {  
        public void handleMessage(Message msg) {  
            if (msg.what == 1) {  
                tvShow.setText(Integer.toString(i++));  
            }  
            super.handleMessage(msg);  
        };  
    };  
    Timer timer = new Timer();  
    TimerTask task = new TimerTask() {  
  
        @Override  
        public void run() {  
            //      :      
            Message message = new Message();  
            message.what = 1;  
            handler.sendMessage(message);  
        }  
    };  
}