crychair
crychair

Reputation: 347

Internal Storage for Android; creating files

Im trying to just create basic files internally for another app. So i wrote a basic app to work out the kinks then was going to add it to the other app. Here is the log from the cat

10-09 17:56:14.579: D/dalvikvm(11092): Not late-enabling CheckJNI (already on)
10-09 17:56:15.599: E/Trace(11092): error opening trace file: No such file or directory (2)
10-09 17:56:16.549: D/gralloc_goldfish(11092): Emulator without GPU emulation detected.
10-09 17:57:20.158: D/dalvikvm(11092): Debugger has detached; object registry had 1 entries
10-09 17:57:30.078: E/Trace(11662): error opening trace file: No such file or directory (2)
10-09 17:57:30.759: D/gralloc_goldfish(11662): Emulator without GPU emulation detected.

The file is created. But when im implemented the code to create a file off the onClick it did not. Or when i put the file creating in a class other than the main class, it did not create the file.

here is my basic code: `package com.newapp;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
import android.view.Menu;

public class MainActivity extends Activity {

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

}
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
    public String timeStamp(){
        Date myDate = new Date();
        return (DateFormat.getDateInstance().format(myDate) + " " +                         DateFormat.getTimeInstance().format(myDate));
    }

    public void newFile (){
         String FILENAME = timeStamp();
         String string = "hello world!";

         FileOutputStream fos = null;
            try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
            } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
                e.printStackTrace();
        }
         try {
                fos.write(string.getBytes());
            } catch (IOException e) {
                // TODO Auto-generated catch block
        e.printStackTrace();
        }
         try {
                    fos.close();
            } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}`

I got rid of the button and other classes for now. This is all the code other than the normal hello worlds screen they give you and an unmodified manifest.

EDIT The code above works i and a file is created in /data/data/com.newapp/files Just ran it again to make sure a file is created and got this from the cat.

10-09 17:57:29.482: D/dalvikvm(11662): Not late-enabling CheckJNI (already on) 10-09 17:57:30.078: E/Trace(11662): error opening trace file: No such file or directory (2) 10-09 17:57:30.759: D/gralloc_goldfish(11662): Emulator without GPU emulation detected. 10-09 18:53:35.238: D/dalvikvm(11662): Debugger has detached; object registry had 1 entries 10-09 18:53:53.389: E/Trace(14975): error opening trace file: No such file or directory (2) 10-09 18:53:54.658: I/Choreographer(14975): Skipped 36 frames! The application may be doing too much work on its main thread. 10-09 18:53:54.668: D/gralloc_goldfish(14975): Emulator without GPU emulation detected.

I am going to move it to its own class now and try it.

Here is the file creation in its own class: package com.newapp;

import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;    
import java.util.Date;

import android.app.Activity;
import android.content.Context;

public class NewFile extends Activity {

public String timeStamp(){
    Date myDate = new Date();
    return (DateFormat.getDateInstance().format(myDate) + " " +             DateFormat.getTimeInstance().format(myDate));
}

public NewFile (){
     String FILENAME = timeStamp();
     String string = "hello world!";

     FileOutputStream fos = null;
    try {
        fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try {
        fos.write(string.getBytes());
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
     try {
        fos.close();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }
    }
 }

And here is the Main activity:

package com.newapp;



import android.app.Activity;
import android.os.Bundle;
import android.view.Menu;


public class MainActivity extends Activity {

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
    NewFile firstone = new NewFile();
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
    getMenuInflater().inflate(R.menu.activity_main, menu);
    return true;
}   
}

Finally I tried it a few different ways with having the NewFile(); as a constructor as just a regular method and calling firstone.newFile(); Having a lot of trouble it will not even run on the virtual device or tablet right now.

Upvotes: 1

Views: 6541

Answers (1)

crychair
crychair

Reputation: 347

I figured out the problem with the help of a friend. You cannot "extend activity" in the new class. You need to use context to reference the class. The final code for the NewFile class is:

package com.newapp;

import java.io.FileOutputStream;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;
import android.content.Context;

public class NewFile{

public String timeStamp(){
    Date myDate = new Date();
    return (DateFormat.getDateInstance().format(myDate) + " " + DateFormat.getTimeInstance().format(myDate));
}

public void createFile(Context c) throws IOException{
     String FILENAME = timeStamp();
     String string = "hello world!";

     FileOutputStream fos = c.openFileOutput(FILENAME, Context.MODE_PRIVATE);
    fos.write(string.getBytes());
    fos.close();


}
}

and in the main class you just needed to call:

NewFile firstfile = new NewFile();
firstfile.createFile(getBaseContext());

In the onCreate method.

Thanks

Upvotes: 1

Related Questions