1. Code
(1) sdcard 是否存在
android.os.Environment.getExternalStorageState().equals(android.os.Environment.MEDIA_MOUNTED);
return true; //存在
return false; //不存在
(2) 內部儲存空間
File path = android.os.Environment.getDataDirectory();
StatFs sf = new StatFs(path.getPath());
long bs = sf.getBlockSize();
long ab = stat.getAvailableBlocks();
long bc = stat.getBlockCount();
retrun bs*ab; //剩餘儲存空間
retrun bs*bc; //總儲存空間
(3) sdcard儲存空間
File path = android.os.Environment.getExternalStorageDirectory();
StatFs sf = new StatFs(path.getPath());
long bs = sf.getBlockSize();
long ab = stat.getAvailableBlocks();
long bc = stat.getBlockCount();
retrun bs*ab; //剩餘儲存空間
retrun bs*bc; //總儲存空間
2. 說明
bs(block size),在fat32檔案系統, block size是 4096 byte,其他可以用getBlockSize()來找出block size 大小。
2013年1月3日 星期四
2012年12月12日 星期三
(Android Note) 畫面横屏OR值屏
1. code
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);//畫面直向
- setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);//畫面橫向
以上畫面只要轉方向時,activity 會重新執行。
- 如果不要 activity 重新執行,需要在 AndroidManifest.xml 檔案中,在 activity 加入 android:configChanges="orientation|keyboardHidden" 就會禁止重新執行。
2012年11月22日 星期四
(Android Note) webview 字體設定
1. code
(1) 卷軸設定
WebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
WebView.setHorizontalScrollBarEnabled(false); //橫軸
WebView.setVerticalScrollBarEnabled(false); //縱軸
(2)字體
WebView.getSettings().setDefaultFontSize(value); //value 數值
OR
WebView.getSettings().setTextSize(WebSettings.TextSize.Normal); //TextSize只有5種設定方式(Smallest、Smaller、Normal、Larger、Largest)
(1) 卷軸設定
WebView.setScrollBarStyle(SCROLLBARS_OUTSIDE_OVERLAY);
WebView.setHorizontalScrollBarEnabled(false); //橫軸
WebView.setVerticalScrollBarEnabled(false); //縱軸
(2)字體
WebView.getSettings().setDefaultFontSize(value); //value 數值
OR
WebView.getSettings().setTextSize(WebSettings.TextSize.Normal); //TextSize只有5種設定方式(Smallest、Smaller、Normal、Larger、Largest)
2012年11月15日 星期四
(Android Note) 圖片依照螢幕節析度自動調整
1. Introduction
(1) 有時用bitmap會發覺內存溢出導致error,主要是圖片太大才會發生此情況,以下code 可以解決內存溢出問題。
(2)目前android mobile device太多不同的節析度,此code 為了讓圖片可以依照節析度自行縮放。
2. Code
changePictureWH(讀取圖檔, 元件);//getResources().openRawResource(R.drawable.X), 元件
public void changePictureWH(InputStream resources, ImageButton id) {
Bitmap bmp;
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inJustDecodeBounds = true; //不分配空間
bmp = BitmapFactory.decodeStream(resources,null,bfo);
int picWidth = bfo.outWidth; //圖片寬度
int picHeight = bfo.outHeight; //圖片長度
int screenWidth = Define_Variable.scalWidth; //螢幕寬度
int screenHeight = Define_Variable.scalHeight; //螢幕長度
bfo.inSampleSize = 1; //isSampleSize 指圖片縮放程度 = 100/isSampleSize, 值越大圖越小
//根據螢幕的大小和圖片大小計算比例
if(picWidth > picHeight){
if(picWidth > screenWidth) bfo.inSampleSize = picWidth/screenWidth;
}else{
if(picHeight > screenHeight) bfo.inSampleSize = picHeight/screenHeight;
}
//產生經過縮放有像素的bitmap
bfo.inJustDecodeBounds = false; //從新分配空間
bmp = BitmapFactory.decodeStream(resources,null,bfo);
id.setImageBitmap(bmp);
}
(1) 有時用bitmap會發覺內存溢出導致error,主要是圖片太大才會發生此情況,以下code 可以解決內存溢出問題。
(2)目前android mobile device太多不同的節析度,此code 為了讓圖片可以依照節析度自行縮放。
2. Code
changePictureWH(讀取圖檔, 元件);//getResources().openRawResource(R.drawable.X), 元件
public void changePictureWH(InputStream resources, ImageButton id) {
Bitmap bmp;
BitmapFactory.Options bfo = new BitmapFactory.Options();
bfo.inJustDecodeBounds = true; //不分配空間
bmp = BitmapFactory.decodeStream(resources,null,bfo);
int picWidth = bfo.outWidth; //圖片寬度
int picHeight = bfo.outHeight; //圖片長度
int screenWidth = Define_Variable.scalWidth; //螢幕寬度
int screenHeight = Define_Variable.scalHeight; //螢幕長度
bfo.inSampleSize = 1; //isSampleSize 指圖片縮放程度 = 100/isSampleSize, 值越大圖越小
//根據螢幕的大小和圖片大小計算比例
if(picWidth > picHeight){
if(picWidth > screenWidth) bfo.inSampleSize = picWidth/screenWidth;
}else{
if(picHeight > screenHeight) bfo.inSampleSize = picHeight/screenHeight;
}
//產生經過縮放有像素的bitmap
bfo.inJustDecodeBounds = false; //從新分配空間
bmp = BitmapFactory.decodeStream(resources,null,bfo);
id.setImageBitmap(bmp);
}
2012年10月31日 星期三
(Android Note) listview 移置最後一行
1. Code
listview.postDelayed(new Runnable() {
@Override
public void run() {
listview.setSelection(data.size()); //設定移到第幾個item
}
}, 0);
listview.postDelayed(new Runnable() {
@Override
public void run() {
listview.setSelection(data.size()); //設定移到第幾個item
}
}, 0);
2012年10月25日 星期四
(Android Note) Timer 使用方法
1. Code
(1) schedule 用法
schedule(TimerTask task, long delay) // 只執行一次,long delay 延遲時間(單位毫秒)
schedule(TimerTask task, long delay, long period) //重複執行 ,long period 週期多久執行(單位毫秒)
(2)
public class TimerState extends Activity{
Timer mTimer;
public int num;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.timerstate);
num = 0;
mTimer = new Timer();
mTimer.schedule( new startTimer() , 0, 1000 ); //第二個參數 0 代表無延遲時間, 第三個參數 單位毫秒
//mTimer.cancel();//暫停
}
//使用schedule方法,必須要有物件繼承imerTask,方可使用
public class startTimer extends TimerTask{
@Override
public void run() {
// TODO Auto-generated method stub
num++;
Log.e("num", ""+num);
}
}
}
訂閱:
文章 (Atom)