簡體   English   中英

如何從SD卡讀取原始文本文件?

[英]How to read a raw text file from SD card?

我正在嘗試獲取應用程序的版本名稱,以及保存在名為ver.txt的SD卡上的.txt文件中的版本名稱。 我可以輕松獲取應用程序的版本,但是如何讀取存儲在.txt文件中的版本?

// get local apk version code
PackageManager manager = context.getPackageManager();
PackageInfo info = null;
try {
    info = manager.getPackageInfo(context.getPackageName(), 0);
} catch (NameNotFoundException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

// get version code from downloaded .txt file
//

// compareVersions
if (info.versionCode >= 1) {
    Toast.makeText(context, "up to date!", Toast.LENGTH_LONG).show();
} else {
    Toast.makeText(context, "updates are available", Toast.LENGTH_LONG).show();
}

這不是完整的解決方案,但是您可以理解。

int updateVersionCode = info.versionCode;

try {
    File sdcard = Environment.getExternalStorageDirectory();
    File file = new File(sdcard, "version.txt");

    BufferedReader br = new BufferedReader(new FileReader(file));
    String line = null;

    if((line = br.readLine()) != null){
        updateVersionCode = Integer.parseInt(line);
    }
    br.close();
}catch (Exception e) {
    //Handle errors!
    e.printStackTrace();
}


if (info.versionCode  < updateVersionCode) {
    //The local version is minor, so updates are avaliable...
}

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM