android error : java.io.filenotfoundexception /proc/net/xt_qtaguid/stats + open failed :ENOENT(no such file or directory )

StackOverflow https://stackoverflow.com/questions/20783594

Question

i am trying to create android application that allow user to save the file after he enter the file name but the problem is that when user try to save the file an error is displayed that say :

 Caused by: java.io.FileNotFoundException: /proc/net/xt_qtaguid/stats: open failed: ENOENT (No such file or directory)

and then the file it saved as null.txt

how to fix this error ???

SignSactivity.java

package com.devleb.idapp;

import java.io.File;
import java.io.FileOutputStream;
import java.io.OutputStreamWriter;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import android.app.Activity;
import android.app.AlertDialog;
import android.app.DatePickerDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.os.Environment;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

public class SignSoldgerActivity extends Activity {

    EditText edit_txt_note;
    final Context context = this;
    // attribute for the date picker

    public String fileName;
    String userinputResult;

    Button btndatePicker, btn_save_soldger;
    TextView txtatePicker;
    int year, monthofyear, dayofmonth;
    Calendar cal = Calendar.getInstance();
    DateFormat dt = DateFormat.getInstance();

    DatePickerDialog.OnDateSetListener dpd;

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

        edit_txt_note = (EditText) findViewById(R.id.editTxtNote);

        btndatePicker = (Button) findViewById(R.id.btnDateTimePicker);
        btndatePicker.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                // TODO Auto-generated method stub
                new DatePickerDialog(SignSoldgerActivity.this, dpd, cal
                        .get(Calendar.YEAR), cal.get(Calendar.MONTH), cal
                        .get(Calendar.DAY_OF_MONTH)).show();

            }
        });

        txtatePicker = (TextView) findViewById(R.id.txtDate);

        dpd = new DatePickerDialog.OnDateSetListener() {

            @Override
            public void onDateSet(DatePicker view, int year, int monthOfYear,
                    int dayOfMonth) {
                // TODO Auto-generated method stub
                cal.set(Calendar.YEAR, year);
                cal.set(Calendar.MONTH, monthOfYear);
                cal.set(Calendar.DAY_OF_MONTH, dayOfMonth);

                txtatePicker.setText(new StringBuilder().append(year + "/")
                        .append(monthOfYear + "/").append(dayOfMonth));
            }
        };

        btn_save_soldger = (Button) findViewById(R.id.btnSaveSoldger);
        btn_save_soldger.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View arg0) {

                // / for creating a dialog
                LayoutInflater li = LayoutInflater.from(context);
                View promptsView = li.inflate(R.layout.prompts, null);

                AlertDialog.Builder alertDialogBuilder = new AlertDialog.Builder(
                        context);

                // set prompts.xml to alertdialog builder
                alertDialogBuilder.setView(promptsView);

                final EditText userInput = (EditText) promptsView
                        .findViewById(R.id.editTextDialogUserInput);

                // set dialog message
                alertDialogBuilder
                        .setCancelable(false)
                        .setPositiveButton("OK",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        // get user input and set it to result
                                        // edit text
                                        userinputResult = userInput.getText()
                                                .toString();

                                        SimpleDateFormat formatter = new SimpleDateFormat(
                                                "yyyy/MM/dd\\HH:mm:ss");
                                        Date now = new Date();
                                        fileName = formatter.format(now) + "/"
                                                + userinputResult;

                                        saveFile(fileName);
                                        txtatePicker.setText(fileName);
                                    }
                                })
                        .setNegativeButton("Cancel",
                                new DialogInterface.OnClickListener() {
                                    public void onClick(DialogInterface dialog,
                                            int id) {
                                        dialog.cancel();
                                    }
                                });

                // create alert dialog
                AlertDialog alertDialog = alertDialogBuilder.create();

                // show it
                alertDialog.show();

            }

        });

    }

    // / for saving the file on the SD

    public void saveFile(String fileName) {
        try {
            String sdPath = Environment.getExternalStorageDirectory()
                    .getAbsolutePath() + "/" + fileName + ".txt";

            File myFile = new File(sdPath);
            myFile.createNewFile();

            Toast.makeText(getBaseContext(), "the second step in saving file",
                    Toast.LENGTH_SHORT).show();

            FileOutputStream fOut = new FileOutputStream(myFile);
            OutputStreamWriter myOutWriter = new OutputStreamWriter(fOut);

            // append or write
            myOutWriter.append(edit_txt_note.getText());
            myOutWriter.close();
            fOut.close();
            edit_txt_note.setText("");
            Toast.makeText(getBaseContext(), "Done Writing SD" + fileName,
                    Toast.LENGTH_SHORT).show();

        } catch (Exception e) {

            Toast.makeText(getBaseContext(), e.getMessage(), Toast.LENGTH_SHORT)
                    .show();
        }
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.sign_soldger, menu);
        return true;
    }

}
Was it helpful?

Solution

It's throwing an error because your're trying save the file in a directory that does not exist. I ran your code and using a breakpoint I check the value in the String fileName.

From the screen shot below you can see that it is an invalid directory and there's where the problem is. You might want to change the directory.

enter image description here

[EDIT]

Change the way you're setting the name of the file.

SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd-HH-mm-ss");
Date now = new Date();
fileName = formatter.format(now) + "-" + userinputResult;
saveFile(fileName);

Now, fileName will have current date + the user entered text.

For instance, the file name will be 2013-12-26-18-51-26-hello.txt

Also, make sure you've added the permission <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top