Unable to open stack trace file '/data/anr/traces.txt': Permission denied while the permission is granted
<p>I'm writing an Android application with Eclipse, everything works properly if I write the application as follows:</p>

<pre><code>import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

public class HTableActivity extends Activity {

    /** Called when the activity is first created. */
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
    }

    public boolean onCreateOptionsMenu(Menu menu){
        super.onCreateOptionsMenu(menu);
        MenuInflater inflater = getMenuInflater();
        inflater.inflate(R.menu.mymenu, menu);
        return true;
    }

    public boolean onOptionsItemSelected(MenuItem item){
        if (item.getItemId() == R.id.save_item){
            save("testo.txt");
            return true;
        } else if (item.getItemId() == (R.id.load_item)) {
            load("testo.txt");
            return true;
        } else
            return false;
    }

    private void save(String filename){

        StringBuffer aux = new StringBuffer();
        Writer writer = null; //scrittore

        EditText[] textAreas = new EditText[3];
        textAreas[0] = (EditText) findViewById(R.id.editText1);
        textAreas[1] = (EditText) findViewById(R.id.editText2);
        textAreas[2] = (EditText) findViewById(R.id.editText3); 

        for(EditText et : textAreas){
            aux.append( et.getText().toString() + "\n");
        }

        try{
            writer = new OutputStreamWriter(this.openFileOutput(filename, MODE_PRIVATE));
            writer.write(aux.toString());
        }catch (IOException e){
            Log.e("Hash Table", "Impossibile salvare il file", e);
        }
        finally{
            if (writer != null){
                try { 
                    writer.close(); 
                } catch (Throwable t) {
                    Log.e("Hash Table", "Impossibile chiudere il file", t);
                }
            }
        }
    }

    private void load(String filename){

        StringBuffer aux = new StringBuffer();
        Reader reader = null;
        String[] splits;
        int i=0;

        EditText[] textAreas = new EditText[3];
        textAreas[0] = (EditText) findViewById(R.id.editText1);
        textAreas[1] = (EditText) findViewById(R.id.editText2);
        textAreas[2] = (EditText) findViewById(R.id.editText3); 

        try{
            reader = new InputStreamReader(openFileInput(filename));
            char[] buf = new char[1024];

            int len;

            while ((len = reader.read(buf)) != -1) {
                aux.append(buf, 0, len);
            }
            splits = aux.toString().split("\\n");
            for(EditText et : textAreas){
                et.setText(splits[i++]);
            }

        } catch (FileNotFoundException e) {
            Log.e("Hash Table", "File non trovato", e);
        } catch (IOException e) {
            Log.e("Hash Table", "Impossibile aprire il file", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close(); 
                } catch (Throwable t) {
                    Log.e("Hash Table", "Impossibile chiudere il file", t);
                }
            }
        }
    }
}
</code></pre>

<p>But given that the following variables:</p>

<pre><code>    EditText[] textAreas = new EditText[3];
    textAreas[0] = (EditText) findViewById(R.id.editText1);
    textAreas[1] = (EditText) findViewById(R.id.editText2);
    textAreas[2] = (EditText) findViewById(R.id.editText3); 
</code></pre>

<p>Appear both in the load method than in the save, I wanted to make them "global" as follows:</p>

<pre><code>import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.io.Reader;
import java.io.Writer;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.EditText;

    public class HTableActivity extends Activity {

       public EditText textAreas;

       HTableActivity (){
            textAreas = new EditText[3];
            textAreas[0] = (EditText) findViewById(R.id.editText1);
            textAreas[1] = (EditText) findViewById(R.id.editText2);
            textAreas[2] = (EditText) findViewById(R.id.editText3); 
        }

        /** Called when the activity is first created. */
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.main);
        }

        public boolean onCreateOptionsMenu(Menu menu){
            super.onCreateOptionsMenu(menu);
            MenuInflater inflater = getMenuInflater();
            inflater.inflate(R.menu.mymenu, menu);
            return true;
        }

        public boolean onOptionsItemSelected(MenuItem item){
            if (item.getItemId() == R.id.save_item){
                save("testo.txt");
                return true;
            } else if (item.getItemId() == (R.id.load_item)) {
                load("testo.txt");
                return true;
            } else
                return false;
        }

        private void save(String filename){

            StringBuffer aux = new StringBuffer();
            Writer writer = null;

            for(EditText et : textAreas){
                aux.append( et.getText().toString() + "\n");
            }

            try{
                writer = new OutputStreamWriter(this.openFileOutput(filename, MODE_PRIVATE));
                writer.write(aux.toString());
            }catch (IOException e){
                Log.e("Hash Table", "Impossibile salvare il file", e);
            }
            finally{
                if (writer != null){
                    try { 
                        writer.close(); 
                    } catch (Throwable t) {
                        Log.e("Hash Table", "Impossibile chiudere il file", t);
                    }
                }
            }
        }

        private void load(String filename){

            StringBuffer aux = new StringBuffer();
            Reader reader = null;
            String[] splits;
            int i=0;

            try{
                reader = new InputStreamReader(openFileInput(filename));
                char[] buf = new char[1024];

                int len;

                while ((len = reader.read(buf)) != -1) {
                    aux.append(buf, 0, len);
                }
                splits = aux.toString().split("\\n");
                for(EditText et : textAreas){
                    et.setText(splits[i++]);
                }

            } catch (FileNotFoundException e) {
                Log.e("Hash Table", "File non trovato", e);
            } catch (IOException e) {
                Log.e("Hash Table", "Impossibile aprire il file", e);
            } finally {
                if (reader != null) {
                    try {
                        reader.close(); 
                    } catch (Throwable t) {
                        Log.e("Hash Table", "Impossibile chiudere il file", t);
                    }
                }
            }
        }
    }
</code></pre>

<p>I also added the permissions:</p>

<pre><code>&lt;uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /&gt;
</code></pre>

<p>in the Android manifest, but despite this, when I start the application I get the following error:</p>

<pre><code>E/dalvikvm(384): Unable to open stack trace file '/data/anr/traces.txt': Permission denied
</code></pre>

<p>Can someone explain why this change of code causes this error?</p>

<p>Full StackTrace:</p>

<pre><code>[2012-05-27 13:11:00 - HTable] ------------------------------
[2012-05-27 13:11:00 - HTable] Android Launch!
[2012-05-27 13:11:00 - HTable] adb is running normally.
[2012-05-27 13:11:00 - HTable] Performing corso.terminali.HTableActivity activity launch
[2012-05-27 13:11:00 - HTable] Automatic Target Mode: using existing emulator 'emulator-5554' running compatible AVD 'MyDevice1.6'
[2012-05-27 13:11:00 - HTable] Uploading HTable.apk onto device 'emulator-5554'
[2012-05-27 13:11:00 - HTable] Installing HTable.apk...
[2012-05-27 13:11:03 - HTable] Success!
[2012-05-27 13:11:03 - HTable] Starting activity corso.terminali.HTableActivity on device emulator-5554
[2012-05-27 13:11:05 - HTable] ActivityManager: Starting: Intent { act=android.intent.action.MAIN cat=[android.intent.category.LAUNCHER] cmp=corso.terminali/.HTableActivity }
</code></pre>

<p>I have also implemented a virtual SD Card 512 MB​​, in my Android emulator (version 1.6) integrated with eclipse. </p>

<p><strong>I kept trying, and I came to the conclusion that you can declare and initialize, globally, the array textareas (EditText textareas EditText > = new [n]), but you can not carry out a global assignment of IDs, either in the constructor, that in the method "public void onCreate > ()", which would cause an error when the methods "save" and "load" are > called. Therefore, although I'm not understanding the reasons, I think > the best solution is to declare the array "textareas" globally, and > then proceed to the allocation of IDs "textareas [n] = (EditText) > findViewById (R.Id.editTextn) "in both forms 'save' and 'load'. If > anyone knows why this happens, please let me know.</strong></p>

<hr>

<p>I kept trying, and I came to the conclusion that you can declare and initialize, globally, the array textareas (EditText textareas EditText > = new [n]), but you can not carry out a global assignment of IDs, either in the constructor, that in the method "public void onCreate > ()", which would cause an error when the methods "save" and "load" are > called. Therefore, although I'm not understanding the reasons, I think > the best solution is to declare the array "textareas" globally, and > then proceed to the allocation of IDs "textareas [n] = (EditText) > findViewById (R.Id.editTextn) "in both forms 'save' and 'load'. If > anyone knows why this happens, please let me know.</p>
