openFileOutput, FileNotFoundException

by Michael » Thu, 27 Jan 2011 12:08:35 GMT


Sponsored Links
 Som alla andra (tydligen) s fr jag FileNotFound p raden dr jag kr
"openFileOutput". Borde det inte funka i en activityklass? Vad tusan
r fel? Jag har lst trdar till frbannelse men hittar ingen lsning.
/Micke


import java.io.FileOutputStream;
import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class firstActivity extends Activity {
        public firstActivity(){
        }
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maininterface);

        String fil = "test";
        String string = "test";

        FileOutputStream fos;

                fos = openFileOutput(fil, Context.MODE_PRIVATE);

        fos.write(string.getBytes());
        fos.close();

    }
}

-- 



openFileOutput, FileNotFoundException

by Michael » Thu, 27 Jan 2011 12:30:58 GMT


 Hi!
I've read most available post for this exception with openFileOutput,
and it seem to be a common issue. Though, i can't find a solution.
This code throughs a FileNotFoundException on the row "fos =
openFileOutput(fil, Context.MODE_PRIVATE);". Why? I can't figure it
out. Thanks!


import java.io.FileOutputStream;

import android.app.Activity;
import android.content.Context;
import android.os.Bundle;
public class firstActivity extends Activity {
        public firstActivity(){
        }
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.maininterface);


                String fil = "test2";
                String string = "exempeltext";

                FileOutputStream fos;

                fos = openFileOutput(fil, Context.MODE_PRIVATE);

                fos.write(string.getBytes());
                fos.close();
    }
}

-- 


Sponsored Links


Re: openFileOutput, FileNotFoundException

by TreKing » Fri, 28 Jan 2011 01:36:40 GMT


 



You may want to try that again in English.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: openFileOutput, FileNotFoundException

by Jake Basile » Fri, 28 Jan 2011 02:48:46 GMT


 Do you have <uses-permission 
android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> in your manifest?

-- 



Re: openFileOutput, FileNotFoundException

by TreKing » Fri, 28 Jan 2011 02:53:02 GMT


 



At which point does it through the exception? What does the full stack look
like?





You do not need external storage permission to save local data.

-------------------------------------------------------------------------------------------------
TreKing < http://sites.google.com/site/rezmobileapps/treking> ; - Chicago
transit tracking app for Android-powered devices

-- 



Re: openFileOutput, FileNotFoundException

by Michael » Fri, 28 Jan 2011 20:45:26 GMT


 


Any other permissions than can be the issue? I've tried reading some
documentation for the answer, but i can't find anything about internal
storage and permission.

-- 



Re: Re: openFileOutput, FileNotFoundException

by Kostya Vasilyev » Fri, 28 Jan 2011 21:40:46 GMT


 Michael,

Your code (as posted) is correct. Just in verify, I just pasted in 
verbatim into my application and verified that it works (as it should).


Try removing and reinstalling the application, see if that helps.

Other than that - is this a Samsung Galaxy S with 2.2.1 firmware, by any 
chance?


-- Kostya

28.01.2011 15:45, Michael :
You do not need external storage permission to save local data.
Any other permissions than can be the issue? I've tried reading some documentation for the answer, but i can't find anything about internal storage and permission.
-- Kostya Vasilyev -- WiFi Manager + pretty widget -- http://kmansoft.wordpress.com --



Re: openFileOutput, FileNotFoundException

by Michael » Fri, 28 Jan 2011 21:58:30 GMT


 Thanks for the answer and for testing the code. I'm developing on
Eclipse - this is my first try on android developing and my java isn't
that good in the first place. I'm using a virtual andriod phone for
testing, but i can't even compile the code because of the error
message. I will try and reinstall Eclipe or another API and see if it
helps.





-- 



Re: Re: openFileOutput, FileNotFoundException

by Kostya Vasilyev » Fri, 28 Jan 2011 22:13:13 GMT


 h!!! So you have a *compile* error!

Ok, try this:

String fil = "test2";
String string = "exempeltext";

FileOutputStream fos;

try {

fos = openFileOutput(fil, Context.MODE_PRIVATE);

fos.write(string.getBytes());
fos.close();
} catch (IOException x) {
Log.i(TAG, "Cannot open file: " + x.toString());
}

Eclipse (or any other Java compiler) checks exception signatures at
compile time.


The compiler knows that openFileOutput *can* throw a
FileNotFoundException, since it's declared like this:



public FileOutputStream openFileOutput(String name, int mode)
*throws FileNotFoundException*


It also knows that write() and close() *can* throw IOException.

Since onCreate says it does not throw any exceptions, the compiler flags
this as an error, and suggests that you either:


1) catch any exceptions from the above methods, and don't let them
propagate out of onCreate or


2) change the signature of onCreate to indicate that it, too, can throw
exceptions (which is not really an option, because onCreate is an
override of a method of Activity, and you can't add new exceptions when
overriding methods).


Since exception handling is one of the fundamentals of Java, you should
take some time to learn more about them. There are lots of articles and
samples on the net, here is one (from 1998, and things haven't changed
since then):


http://www.javaworld.com/jw-07-1998/jw-07-exceptions.html

-- Kostya

28.01.2011 16:58, Michael :

Thanks for the answer and for testing the code. I'm developing on
Eclipse - this is my first try on android developing and my java isn't
that good in the first place. I'm using a virtual andriod phone for
testing, but i can't even compile the code because of the error
message. I will try and reinstall Eclipe or another API and see if it
helps.

On 28 Jan, 14:40, Kostya Vasilyev<kmans...@gmail.com> wrote:

Michael,

Your code (as posted) is correct. Just in verify, I just pasted in
verbatim into my application and verified that it works (as it should).

Try removing and reinstalling the application, see if that helps.

Other than that - is this a Samsung Galaxy S with 2.2.1 firmware, by any
chance?

-- Kostya

28.01.2011 15:45, Michael :


On 27 Jan, 19:52, TreKing<treking...@gmail.com> wrote:

You do not need external storage permission to save local data.

Any other permissions than can be the issue? I've tried reading some
documentation for the answer, but i can't find anything about internal
storage and permission.

--
Kostya Vasilyev -- WiFi Manager + pretty widget --http://kmansoft.wordpress.com



--
Kostya Vasilyev -- WiFi Manage



Re: openFileOutput, FileNotFoundException

by Michael » Fri, 28 Jan 2011 22:32:35 GMT


 hanks alot for the help Kostya. I'll definitely follow your advice
and learn som more about exceptions and debuggning.
/Micke

On 28 Jan, 15:12, Kostya Vasilyev <kmans...@gmail.com> wrote:

--



Other Threads

1. Can I get the street name for a given Location

thanks.

--~--~---------~--~----~------------~-------~--~----~

2. adding third party applications to the emulator /device

I have a linux C executable native application. is it possible to
execute the application in the emulator or device; if so how to load
it to the emulator.

--~--~---------~--~----~------------~-------~--~----~

3. May I remove those default shortcuts in home screen

4. How to add different elements ?

5. What happens to app when a customer requests a refund?

6. Where is the ADT Eclipse plugin?

7. Coloring Book