try {
File sd = Environment.getExternalStorageDirectory(); //複製到哪裡
File data = Environment.getDataDirectory(); //原始位置
if (sd.canWrite()) {
String currentDBPath = "//data//com.httc.pipeline//xxx.db"; //原始位置檔名
String backupDBPath = "xxx.db"; //複製位置檔名
File currentDB = new File(data, currentDBPath);
File backupDB = new File(sd, backupDBPath);
Log.i("currentDB", currentDB+"");
Log.i("backupDB", backupDB+"");
if (currentDB.exists()) { //原始檔案存在
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size()); //開始複製
Log.i("dst", dst+"");
src.close();
dst.close();
}
}
} catch (Exception e) {
Log.i("eDB", e+"");
}
參考來源: http://stackoverflow.com/questions/1995320/how-to-backup-database-file-to-sdcard-on-android
2015年2月25日 星期三
Android db file copy backup 資料庫 檔案 複製 備份
備份資料庫或檔案到其他位置,加入下列程式碼
2015年2月12日 星期四
Android TableLayout Center 置中
TableLayout排版文字置中問題
方法1:
TableLayout加入
android:shrinkColumns="*"
android:stretchColumns="*"
方法1:
TableLayout加入
android:shrinkColumns="*"
android:stretchColumns="*"
方法二:text使用比重
參考來源: http://mrbool.com/how-to-deal-with-table-layout-in-android/28008
標籤:
置中
,
Android
,
center
,
java
,
TableLayout
2015年2月10日 星期二
Android NFC Activity disable 關閉 近距離無線通訊
前篇有用到NFC使用方法,在可以使用NFC APP上,我們可能想要在一個頁面(Activity1)可以使用NFC,在下一個頁面(Activity2)不需要用到NFC,但是若沒有設定,仍會感應到tag資料,
所以需加上下列程式碼
所以需加上下列程式碼
/*此頁面關閉NFC感應*/
@Override
protected void onResume() {
super.onResume();
// creating pending intent:
PendingIntent pendingIntent = PendingIntent.getActivity(this, 0,
new Intent(this, getClass())
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP), 0);
// enabling foreground dispatch for getting intent from NFC event:
NfcAdapter nfcAdapter = NfcAdapter.getDefaultAdapter(this);
try {
// nfcAdapter.enableForegroundDispatch(this, pendingIntent,
// new IntentFilter[] { filter }, this.techList); 開啟nfc
nfcAdapter.enableForegroundDispatch(this, pendingIntent,
new IntentFilter[] {}, null); // 關閉nfc IntentFilter[] {}內設為空值, techList改為null空值
} catch (NullPointerException e) {
}
}
參考資料: http://stackoverflow.com/questions/9748513/reading-nfc-tags-only-from-a-particuar-activity
2015年2月2日 星期一
Android camera flashlight 相機閃光燈
簡易手電筒app,開啟相機的閃光燈
1.AndroidManifest.xml 加入
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
2.main.xml
1.AndroidManifest.xml 加入
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
2.main.xml
2.MainActivity.java
package com.prgguru.com;
import android.app.Activity;
import android.content.Context;
import android.content.pm.PackageManager;
import android.hardware.Camera;
import android.hardware.Camera.Parameters;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.Toast;
public class MainActivity extends Activity {
private boolean isFlashOn = false; //預設開關-關閉
private Camera camera;
private Button button;
@Override
protected void onStop() {
super.onStop();
if (camera != null) {
camera.release();
}
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
button = (Button) findViewById(R.id.buttonFlashlight);
Context context = this;
PackageManager pm = context.getPackageManager();
if (!pm.hasSystemFeature(PackageManager.FEATURE_CAMERA)) { //判斷有無相機
Toast.makeText(getApplicationContext(),
"抱歉,您的手機無相機功能",
Toast.LENGTH_SHORT).show();
return;
}
camera = Camera.open();
final Parameters p = camera.getParameters();
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
if (isFlashOn) { //false 關閉
p.setFlashMode(Parameters.FLASH_MODE_OFF); //相機閃光燈模式
camera.setParameters(p);
isFlashOn = false;
button.setText("打開");
} else { // true 開啟
Log.i("info", "torch is turned on!");
p.setFlashMode(Parameters.FLASH_MODE_TORCH); //相機閃光燈模式
camera.setParameters(p);
isFlashOn = true;
button.setText("關閉");
}
}
});
}
}
參考來源: http://programmerguru.com/android-tutorial/android-flashlight-example/
標籤:
閃光燈
,
Android
,
flashlight
,
java
訂閱:
意見
(
Atom
)
