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. Invalidating a row of a ListView?

Hi,

I have a ListView, its rows each have an image downloaded from the net
in a separate thread.

When the image is done downloading, I notify my main thread, and need
to invalidate the corresponding row if it's on screen. I'm doing this:

 // run on the main thread.
 public void onThumbnailDownloaded(String url, Bitmap bmp) {
     for (int i = 0; i < mListView.getChildCount(); i++) {
         PanelMyRow panel = (PanelMyRow)mListView.getChildAt(i);
         if (panel.getUrl().equals(url)) {
             panel.getImageView().setImageBitmap(bmp);
             break;
         }
     }
 }

this seems to work ok. Ideally though, I think it'd be better if I
could somehow trigger my adapter to call its getView() method for this
'dirty' row. I can't find a method within ListView or BaseAdapter to
do this. Calling any of the invalidate methods has no effect, and any
of these:

  ListView.invalidateViews()
  BaseAdapter.notifyDatasetInvalidated();
  BaseAdapter.notifyDatasetChanged();

seem pretty heavyweight and my ListView scrolling will be very choppy
if the images are still downloading and one of the above three is
called during the callback. Is there some way I can simply mark the
child view as dirty which notifies the ListView that it should call
getView() on the adapter for that row again? Seems safer.

Thanks

-- 

2. Problem with drop down views on orientation change (bug?)

This may be a bug, but not if there is a way to attach the view to the
window manager.

I have a class extending AlertDialog. The dialog has a drop down menu
that allows user to choose item.  When the orientation is changed with
the drop down menu displayed, I get the exception listed below.  I set
up the drop down view/resources in onStart() of the Dialog, as shown:

    protected void onStart() {
        super.onStart();

        mySpinner = (Spinner) findViewById(R.id.spinner);
        mySpinnerAdapter = new ArrayAdapter<Tee>(context,
android.R.layout.simple_spinner_item, myList);

        
mySpinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
        mySpinner.setAdapter(mySpinnerAdapter);

    }

This error is occuring because, though this Dialog is managed by the
Activity (using onCreateDialog, onPrepareDialog, etc.), the View
generated by the SpinnerAdapter is not.  I see there is a way to grab
individual items from the drop down view, but not get the view itself
(to attach it).

How can the SpinnerAdapter not cause problems on any orientation change?

E/AndroidRuntime( 5329): java.lang.IllegalArgumentException: View not
attached to window m
anager
E/AndroidRuntime( 5329):        at
android.view.WindowManagerImpl.findViewLocked(WindowMan
agerImpl.java:355)
E/AndroidRuntime( 5329):        at
android.view.WindowManagerImpl.removeView(WindowManager
Impl.java:200)
E/AndroidRuntime( 5329):        at
android.view.Window$LocalWindowManager.removeView(Windo
w.java:417)
E/AndroidRuntime( 5329):        at
android.app.Dialog.dismissDialog(Dialog.java:279)
E/AndroidRuntime( 5329):        at android.app.Dialog.access$000(Dialog.java:72)
E/AndroidRuntime( 5329):        at android.app.Dialog$1.run(Dialog.java:108)
E/AndroidRuntime( 5329):        at android.app.Dialog.dismiss(Dialog.java:263)
E/AndroidRuntime( 5329):        at
android.widget.Spinner.onDetachedFromWindow(Spinner.jav
a:86)
E/AndroidRuntime( 5329):        at
android.view.View.dispatchDetachedFromWindow(View.java:
5584)
E/AndroidRuntime( 5329):        at
android.view.ViewGroup.dispatchDetachedFromWindow(ViewG
roup.java:1074)
E/AndroidRuntime( 5329):        at
android.view.ViewGroup.dispatchDetachedFromWindow(ViewG


Thanks

-- 

3. eugene pindah dari XDA

4. verizon 3g

5. AdSense for Mobile Content New high-end devices!

6. Good EULA idea

7. How to change application theme at runtime