繁体   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