Basic file input / output tutorial

by superjet » Thu, 12 Mar 2009 10:22:23 GMT


Sponsored Links
 I'm new to Java/Android, can anyone recommend a tutorial on android
that introduces reading and writing flat files?

sj

ps, if this is a double post, sorry, but I posted several hours ago
and it seems it never went through.

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



Basic file input / output tutorial

by superjet » Thu, 12 Mar 2009 22:16:11 GMT


 Sorry, this is a duplicate:


Sponsored Links


Basic file input / output tutorial

by Joseph Arceneaux » Fri, 13 Mar 2009 01:57:57 GMT


 I'm not aware of a tutorial, but it's pretty easy - here's some code that
reads in a file.  Check out the File and File*Stream classes.
Joe

        String path = "/data/data/com.company.app/files/foo.jpg";
        File f = new File (path);
        if (f.exists() && f.canRead ())
        {
            try
            {
                FileInputStream is = new FileInputStream (f);
                int size = is.available();
                contents = new byte[size];
                int got = is.read (contents);
                if (got != size)
                {
                    Log.e ("File", f.getName () + ": expected " + size + ",
got " + got + " bytes.");
                    bm = null;
                }
                else
                    bm = BitmapFactory.decodeByteArray (contents, 0, size);
            }
            catch (Exception e)
            {
                Log.e ("FileSave", e.toString());
            }






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



Basic file input / output tutorial

by superjet » Sun, 15 Mar 2009 13:13:21 GMT


 Thanks man! I'll give this a shot tomorrow night.





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



Other Threads

1. new app

can someone develop an mlb at bat app like the iphone has  please

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

2. Very Weird Spinner Issue

Hello,

I am trying (as many are doing) to populate a 2nd spinner out of the
first spinner selected item like this:

//populate first spinner
            final Spinner s = (Spinner) findViewById(R.id.spinner);
            ArrayAdapter adapter = ArrayAdapter.createFromResource(
                    this, R.array.universities,
android.R.layout.simple_spinner_item);
            adapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
            s.setAdapter(adapter);

// Initialization of the 2nd spinner
                   String[] array = {"Select Building Here"};
                   final Spinner s2 = (Spinner) findViewById(R.id.spinner2);
                   ArrayAdapter<String> cadapter = new ArrayAdapter(this,
android.R.layout.simple_spinner_item, array);
                   cadapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
                   s2.setAdapter(cadapter);

                // When user selects a university
                    s2.setOnItemSelectedListener(new OnItemSelectedListener() {

                                @Override
                                public void onItemSelected(AdapterView<?> arg0, 
View arg1,
                                                int arg2, long arg3) {

                                                 //Get the selected building
                                           String selectedItemString = (String) 
s2.getSelectedItem();
                                           System.out.println
("Building: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+selectedItemString);



                                }

                                @Override
                                public void onNothingSelected(AdapterView<?> 
arg0) {
                                        // TODO Auto-generated method stub
                                }

               });



            // When user selects a university
            s.setOnItemSelectedListener(new OnItemSelectedListener() {

                        @Override
                        public void onItemSelected(AdapterView<?> arg0, View 
arg1,
                                        int arg2, long arg3) {
                                  //Get the selected university
                                   String selectedItemString = (String) 
s.getSelectedItem();
                                   System.out.println
("Uni: !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!"+selectedItemString);

                          if(selectedItemString .equals(""))
                          {
                                  // In case of an error so it won't crash
                                  nospinner();
                          }

                          else
                          {
                                  // Find the table of the selected University 
dynamically
                                  finder(selectedItemString);
                          }

                        }

                        @Override
                        public void onNothingSelected(AdapterView<?> arg0) {
                                // TODO Auto-generated method stub
                        }

       });


where finder() is
protected void finder(String selectedItemString) {

               final Dbadapter db = new Dbadapter(this);  // This creates an
instance of the database in this class
           final Spinner s2 = (Spinner) findViewById(R.id.spinner2);
                   // Access Database
                   db.open();

                   // Fix selected item to match the db
                   selectedItemString = (selectedItemString).toLowerCase();
                   selectedItemString = selectedItemString.replaceAll(" ","");

                //   System.out.println("Uni: !!!!!!!!!!!!

                   // Call a function to return the rows of that university
                   final Cursor cur = db.getAllTitles(selectedItemString);

           // Get Columns
           String[] columns = new String[]{"building"};
                   SimpleCursorAdapter cadapter = new SimpleCursorAdapter(this,
android.R.layout.simple_spinner_item, cur,
                       columns, new int[] {android.R.id.text1});

                   // Spinner Layout and population
                   cadapter.setDropDownViewResource
(android.R.layout.simple_spinner_dropdown_item);
                   s2.setAdapter(cadapter);
                   db.close();

}

whenever the user changes the selection of the first spinner it's
fine, whenever the changes are related to the 2nd spinner I get a
handler exception on this line:   String selectedItemString = (String)
s2.getSelectedItem();

I could get an object on selected item but not the actual String
value.

anyone could help?

thanks a lot
--~--~---------~--~----~------------~-------~--~----~

3. Handler Exception when trying to get Spinner selected Item

4. Android on Galileo

5. Alarm Manager

6. Is there a way to access the serial device on G1

7. Progress Dialog When Application Starts