0

Code first:

AssetManager mgr = DeviceListActivity.this.getApplicationContext().getAssets();

try {
    Log.e("Glenn:", address);
    FileOutputStream fout = mgr.openFd("device/device_address.txt").createOutputStream();
    PrintWriter _fout = new PrintWriter(fout);
    _fout.println(address);
    Log.e("Glenn", address);

    _fout.close();
    fout.close();

    InputStream fin =  mgr.open("device/device_address.txt");
    BufferedReader br = new BufferedReader(new InputStreamReader(fin));
    address = br.readLine(); 

    try {
        Log.e("Glenn:", address);
    } catch (NullPointerException e) {
        e.printStackTrace();
    }
} catch (IOException e) {
    e.printStackTrace();
    Log.e("Glenn", "error with OutputStream");
}

The value of address printed by the first two Log.e() calls is the right value, which actually is a device MAC address. However, when I was trying to test the value of address read from the file, which had just been written, NullPointerException has been caught within the Log.e() call. This means the value read from the file is NULL. Can anyone point out what's wrong with the code?

Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
user1437534
  • 129
  • 3
  • 8
  • it's possible to write data or change any existing Assets/raw file? i think no not possible because it is packed(.apk) and not expandable in size – ρяσѕρєя K Jul 03 '12 at 13:03

1 Answers1

5

You cannot write in your app's asset file. You have only read but not write permissions. AssetManager only provides methods to read the files from your app's asset folder.

AggelosK
  • 4,313
  • 2
  • 32
  • 37
  • So how can I save some data for using when re-launch the app? in this case, I wanna save the device's MAC address. – user1437534 Jul 03 '12 at 13:24
  • 1
    @user1437534 there are multiple options for persistent storage. See the Docs: http://developer.android.com/guide/topics/data/data-storage.html – Lukas Knuth Jul 03 '12 at 13:29