今天上午完成了業務資料匯入到手機,能顯示漢字了。

來源:互聯網
上載者:User

標籤:end   iat   匯入   com   desire   bar   編號   ati   directory   

今天上午完成了業務資料匯入到手機,能顯示漢字了。

MainActivity.java 檔案內容如下

package com.strongdady.qrscan;

import java.util.Arrays;
import java.util.List;




import java.io.File;

import java.io.InputStream;

import java.io.FileOutputStream;

import android.database.sqlite.SQLiteDatabase;
import android.database.Cursor;

import com.google.zxing.integration.android.IntentIntegrator;
import com.google.zxing.integration.android.IntentResult;

import android.widget.Toast;
import android.content.Intent;



import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.design.widget.BottomNavigationView;
import android.support.v7.app.AppCompatActivity;
import android.view.MenuItem;
import android.widget.TextView;


import static android.os.Environment.getExternalStorageDirectory;


public class MainActivity extends AppCompatActivity {

private final int BUFFER_SIZE = 400000;
public static final String DB_NAME = "device.db"; // 儲存的資料庫檔案名稱
public static final String PACKAGE_NAME = "com.strongdady.qrscan";// 應用的包名
//public static final String DB_PATH = "/data"droid.os.Environment.getDataDirectory().getAbsolutePath()+"/"
public static final String DB_PATH =getExternalStorageDirectory()+"/data";

String device_no = "";
String owner="";
String register_date = "";
String device_name = "";
String sqlString="";
SQLiteDatabase db=null;
Cursor cursor;

private TextView mTextMessage;

private BottomNavigationView.OnNavigationItemSelectedListener mOnNavigationItemSelectedListener
= new BottomNavigationView.OnNavigationItemSelectedListener() {



public void copyDBFile()
{
mTextMessage.append(DB_PATH);
try {
File myDataPath = new File(DB_PATH+"/device_db");
if (!myDataPath.exists())
{
mTextMessage.append("inside copydbfile 20180521=|"+myDataPath.toString()+"|enddbfile");
myDataPath.mkdirs();// 假設沒有這個檔案夾,則建立
}
else
{
mTextMessage.append("inside copydbfile =|"+myDataPath.toString()+"|existed 2018_5-21!");
}
String dbfile=myDataPath+"/"+DB_NAME;
// if (!(new File(dbfile).exists())) {// 推斷資料庫檔案是否存在,若不存在則運行匯入,否則直接開啟資料庫

if (true) {// 推斷資料庫檔案是否存在,若不存在則運行匯入,否則直接開啟資料庫
InputStream is =getResources().openRawResource(R.raw.device); // 欲匯入的資料庫
FileOutputStream fos = new FileOutputStream(dbfile);
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
while ((count = is.read(buffer)) > 0) {
fos.write(buffer, 0, count);
}
fos.close();
is.close();
}
db = SQLiteDatabase.openOrCreateDatabase(dbfile,null);

} catch (Exception e)
{
mTextMessage.append(e.toString());
}
}


@Override
public boolean onNavigationItemSelected(@NonNull MenuItem item) {

switch (item.getItemId()) {
case R.id.navigation_home:
mTextMessage.setText(R.string.title_home);

IntentIntegrator integrator=new IntentIntegrator(MainActivity.this);

//List<String> oDesiredFormats = Arrays.asList("UPC_A,UPC_E,EAN_13,EAN_8,RSS_14,RSS_EXPANDED,CODE_39,CODE_93,CODE_128,ITF,CODABAR,QR_CODE,DATA_MATRIX,PDF_417".split(","));
List<String> oDesiredFormats = Arrays.asList("PDF_417,UPC_A,UPC_E,EAN_13,EAN_8,RSS_14,RSS_EXPANDED,CODE_39,CODE_93,CODE_128,ITF,CODABAR,QR_CODE,DATA_MATRIX".split(","));

integrator.setPrompt("scanning .....");
integrator.initiateScan(oDesiredFormats);

return true;


case R.id.navigation_dashboard:
mTextMessage.setText(R.string.title_dashboard);

copyDBFile();

mTextMessage.append(" after open database ");
if(db!=null)
mTextMessage.append(" db not null");
else
mTextMessage.append(" db is null!!");


try {
sqlString="SELECT * FROM device where device_no=‘1010-2012-000582‘";
cursor = db.rawQuery(sqlString, null);
}catch (Exception e)
{
mTextMessage.append("exceipion e"+e.toString());
}
owner="init001";


while (cursor.moveToNext()) {
mTextMessage.append("!!!!!!!!!!!!");
device_name = cursor.getString(cursor.getColumnIndex("device_name"));
owner = cursor.getString(cursor.getColumnIndex("owner"));
register_date=cursor.getString(cursor.getColumnIndex("register_date"));
mTextMessage.append("+here+");
}

mTextMessage.append("after cursor!!!"+"sqlstring:"+sqlString);
mTextMessage.append("owner:"+owner);
return true;

case R.id.navigation_notifications:
mTextMessage.setText(R.string.title_notifications);
return true;
}
return false;
}
};

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mTextMessage = (TextView) findViewById(R.id.message);
BottomNavigationView navigation = (BottomNavigationView) findViewById(R.id.navigation);
navigation.setOnNavigationItemSelectedListener(mOnNavigationItemSelectedListener);

}

protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);

IntentResult scanResult = IntentIntegrator.parseActivityResult(requestCode, resultCode, data);
if (scanResult != null) {


mTextMessage.setText("裝置編號 "+device_no);

Toast.makeText(this, device_no, Toast.LENGTH_LONG).show();
//String dbOpenString="/data/data/com.strongdady.qrscan/device.db";
//String dbOpenString=android.os.Environment.getDataDirectory().getAbsolutePath()+"device.db";

if (db==null)
mTextMessage.append("db is null");

else
mTextMessage.append("db is ok!");

cursor = db.rawQuery("SELECT * FROM device where device_no = ‘1010-2012-000582‘", null);


while (cursor.moveToNext()) {
device_name = cursor.getString(cursor.getColumnIndex("device_name"));
owner = cursor.getString(cursor.getColumnIndex("owner"));
register_date=cursor.getString(cursor.getColumnIndex("register_date"));
}

db.close();
mTextMessage.append("裝置名稱:"+device_name+owner+register_date);

}
else
{
//super.onActivityResult(requestCode, resultCode, data);
return;
}

}
}

今天上午完成了業務資料匯入到手機,能顯示漢字了。

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.