-1

I can't create a file in a location /tmp of device. i think it is some permission issues. can you please help me..? in my android manifest, i included following permissions:-

<uses-permission android:name="android.permission.INTERNET" />

    <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
    <uses-permission android:name="android.permission.READ_PHONE_STATE" />

    <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.WRITE_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_INTERNAL_STORAGE"/>
    <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>

My activity code is as follows:

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

        // Example of a call to a native method

        try{

            write();

            TextView tv = (TextView) findViewById(R.id.sample_text);
            tv.setText(tv.getText()+"  "+Register());
        //System.out.println("The value of retval is"+nativeinterface());
        }catch (Exception e){e.printStackTrace();}
    }

    public Boolean write(){
        try {

            String fcontent = "Hello world!!";
            String fpath = "/tmp/abc.txt";


            File file = new File(fpath);



            // If file does not exists, then create it
            if (!file.exists()) {
                file.createNewFile();
            }

            FileWriter fw = new FileWriter(file.getAbsoluteFile());
            BufferedWriter bw = new BufferedWriter(fw);
            bw.write(fcontent);
            bw.close();

            Log.d("Suceess","Sucess");
            return true;

        } catch (IOException e) {
            System.out.println("The File Not Created");
            e.printStackTrace();
            return false;
        }

    }
}

My log is as follows:

W/System.err: java.io.IOException: Permission denied
W/System.err:     at java.io.UnixFileSystem.createFileExclusively0(Native Method)
W/System.err:     at java.io.UnixFileSystem.createFileExclusively(UnixFileSystem.java:280)
W/System.err:     at java.io.File.createNewFile(File.java:948)

please help me to solve this...

Anjali
  • 59
  • 2
  • 9

1 Answers1

0

Per the documentation, you should use the Context object's getCacheDir() method to get the proper location to save temporary files, which "Returns the absolute path to the application specific cache directory on the filesystem." Note that this is for temporary files, so if you want to persist the files "permanently" (in quotes because you can't really assume that a file will be permanent in a mobile device), you'll want to use a different method. Again from the documentation,

The system will automatically delete files in this directory as disk space is needed elsewhere on the device. The system will always delete older files first...

Take a look at this question as well for a similar problem.