如何从SD卡读取原始文本文件?

时间:2016-07-07 13:44:36

标签: android

我正在尝试获取应用程序的版本名称,以及保存在名为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();
}

1 个答案:

答案 0 :(得分:0)

这不是完整的解决方案,但你明白了。

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...
}