简体   繁体   中英

How to read a raw text file from SD card?

I am trying to get my app's version name, as well as the version name saved in a .txt file on my SD card named ver.txt. I can easily get my app's version but how do I read the version stored in my .txt file?

// 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();
}

This is not the complete solution, but you get the idea.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM