无法写入文件

时间:2015-08-04 14:50:29

标签: android file android-logcat

我的应用程序是关于读取logcat的,当缓冲区有5000个字符写入文件时,我的应用程序没有抛出异常但不写文件。

public class MainActivity extends ActionBarActivity {

    private TextView text;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        text = (TextView) findViewById(R.id.TextView_mensajesAlUsuario);
        text.setText(String.valueOf(3));
         new HiloEnSegundoPlano().execute("", "", " ");

    }

    private class HiloEnSegundoPlano extends AsyncTask<String ,String,String> {

        public HiloEnSegundoPlano(){
            //TODO código del constructor
        }


        @Override
        protected String doInBackground(String... params) {

            InputStream in = null;
            Process process = null;
            String cadena = "";
            char caracter;
            FileUtilities file = new FileUtilities(MainActivity.this);

            try {
                process = Runtime.getRuntime().exec("logcat");
            } catch (IOException e) {
                Log.v("ALERTA",e.getMessage());
                e.printStackTrace();
            }
            in = process.getInputStream();

            while(true){
                Log.v("CONTROL", "EN EL HILO");
                try {
                    caracter = (char) in.read();
                    cadena += caracter;
                    if(cadena.length() > 5000) {
                        writeFile("LogCat", cadena);
                        break;
                    }

                        Log.v("ALERTA", String.valueOf(cadena.length()));

                } catch (IOException e) {
                    e.printStackTrace();
                }


            }
            return "";
        }

        @Override
        protected void onProgressUpdate(String... t) {
            text.setText(t[0]);
        }

        protected void onProgressUpdate(String t) {

        }
        public void writeFile(String nombre, String cadena){

            try
            {
                File ruta_sd = Environment.getExternalStorageDirectory();

                File f = new File(ruta_sd.getAbsolutePath()+"/Download", nombre+".txt");

                Log.v("WRITE", ruta_sd.getAbsolutePath()+"/Download");

                OutputStreamWriter fout =
                        new OutputStreamWriter(
                                new FileOutputStream(f));

                fout.write(cadena);
                fout.close();
            }
            catch (Exception ex)
            {
                Log.e("Ficheros", "Error al escribir fichero a tarjeta SD Ruta: "+ Environment.getExternalStorageDirectory());
            }

        }

    }

这是代码中最重要的部分。

0 个答案:

没有答案