MaggotSauceYumYum MaggotSauceYumYum - 1 year ago 101
Android Question

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

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

Answer Source

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);
}catch (Exception e) {
    //Handle errors!

if (info.versionCode  < updateVersionCode) {
    //The local version is minor, so updates are avaliable...
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download