[android-beginners] Saving image to SD Card
I'm using Android SDK-1.0_rc2 on windows, I've emulated the sd card and created on my D:\ drive as sdcard1.iso. My code for saving to the SD card is as follows: try { Log.e(filename,filename); String filepath=Environment.getExternalStorageDirectory ().getAbsolutePath(); Log.e(FilePath,filepath); FileOutputStream fos = openFileOutput(filepath + filename, MODE_APPEND); BufferedOutputStream bos = new BufferedOutputStream (fos); b.compress(CompressFormat.JPEG, 100, fos); bos.flush(); bos.close(); } When I view the Logcat I see the following error: java.lang.IllegalArgumentException: File /sdcardobama.jpg contains a path separator So I changed the code to: FileOutputStream fos = openFileOutput(filepath + / + filename, MODE_APPEND); The error is still occuring, so I tried to push the image using the command: adb push obama.jpg /sdcard/obama.jpg I noticed 0 bytes are being pushed, any idea where I'm going wrong? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: Saving image to SD Card
I changed my code to : FileOutputStream fos = new FileOutputStream(filepath + / + filename); This allowed me to save the image, I loaded the device using DDMS and noticed the image had been saved. I've tried using the MediaScannerConnection to refresh the sd card so that I can view the image, I need some help with that.. On Apr 17, 2:17 pm, jwesonga crazylun...@gmail.com wrote: I'm using Android SDK-1.0_rc2 on windows, I've emulated the sd card and created on my D:\ drive as sdcard1.iso. My code for saving to the SD card is as follows: try { Log.e(filename,filename); String filepath=Environment.getExternalStorageDirectory ().getAbsolutePath(); Log.e(FilePath,filepath); FileOutputStream fos = openFileOutput(filepath + filename, MODE_APPEND); BufferedOutputStream bos = new BufferedOutputStream (fos); b.compress(CompressFormat.JPEG, 100, fos); bos.flush(); bos.close(); } When I view the Logcat I see the following error: java.lang.IllegalArgumentException: File /sdcardobama.jpg contains a path separator So I changed the code to: FileOutputStream fos = openFileOutput(filepath + / + filename, MODE_APPEND); The error is still occuring, so I tried to push the image using the command: adb push obama.jpg /sdcard/obama.jpg I noticed 0 bytes are being pushed, any idea where I'm going wrong? --~--~-~--~~~---~--~~ You received this message because you are subscribed to the Google Groups Android Beginners group. To post to this group, send email to android-beginners@googlegroups.com To unsubscribe from this group, send email to android-beginners-unsubscr...@googlegroups.com For more options, visit this group at http://groups.google.com/group/android-beginners?hl=en -~--~~~~--~~--~--~---
[android-beginners] Re: Records not loading from database in listview
Any chance I could get some help on this please? On Feb 6, 6:50 pm, jwesonga crazylun...@gmail.com wrote: I've been working on a simple database app on android, I can't seem to get it to work, all I get when I run the emulator is Users list empty, when I check the sqlite database from the adb shell its populated,my code is as follows: DBDroid.java package com.android.dbdroid; import android.content.Context; import android.database.sqlite.*; import android.util.Log; import android.database.Cursor; import android.database.SQLException; import java.util.ArrayList; import java.util.List; public class DBDroid { private static final String DATABASE_NAME=mydroiddatabase.db; private static final String USER_TABLE_NAME=Users; private static final String CREATE_TABLE_USERS=CREATE TABLE IF NOT EXISTS + USER_TABLE_NAME + (LastName VARCHAR, FirstName VARCHAR,Country VARCHAR, Age INT(3));; private static final int DATABASE_VERSION = 1; private SQLiteDatabase myDb=null; public DBDroid(Context ctx){ myDb = ctx.openOrCreateDatabase(DATABASE_NAME, 0, null); myDb.execSQL(CREATE_TABLE_USERS); this.createUsers(); } public void createUsers(){ myDb.execSQL(INSERT INTO + USER_TABLE_NAME + (LastName, FirstName, Country, Age) + VALUES ('Gramlich', 'Nicolas', 'Germany', 20);); myDb.execSQL(INSERT INTO + USER_TABLE_NAME + (LastName, FirstName, Country, Age) + VALUES ('Doe', 'John', 'US', 34);); } public ListUser getUsers() { ArrayListUser users = new ArrayListUser(); try{ Cursor c = myDb.query(USER_TABLE_NAME, new String[] { LastName, FirstName, Country,Age }, null, null, null, null, null); int numRows = c.getCount(); c.isFirst(); if(c!=null){ for (int i = 0; i numRows; ++i) { User user = new User(); user.lastName=c.getString(0); user.firstName=c.getString(1); user.country=c.getString(2); user.age=c.getInt(3); users.add(user); c.moveToNext(); } } }catch (SQLException e){ Log.e(DBDroid, e.toString()); } return users; } } UserManager.java: package com.android.dbdroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class UserManager extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.users_row); Intent i = new Intent(getApplicationContext(), UsersList.class); startActivity(i); } } UserList.java package com.android.dbdroid; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.ListView; public class UsersList extends ListActivity{ private DBDroid droidDB; private ListUser users; public void onCreate(Bundle savedInstanceState) { try{ super.onCreate(savedInstanceState); setContentView(R.layout.users_list); droidDB = new DBDroid(this); fillData(); }catch(Throwable e){ Log.e(DBDroid,e.toString()); } } private void fillData(){ ListString items = new ArrayListString(); users=droidDB.getUsers(); for(User user:users){ items.add(user.lastName); //items.add(user.lastName); //items.add(user.country); } ArrayAdapterString adapter = new ArrayAdapterString(this, R.layout.users_row, items); setListAdapter(adapter); } } users_list.xml: ?xml version=1.0 encoding=utf-8? LinearLayout android:id=@+id/widget28 android:layout_width=fill_parent android:layout_height=fill_parent android:orientation=vertical xmlns:android=http
[android-beginners] Records not loading from database in listview
I've been working on a simple database app on android, I can't seem to get it to work, all I get when I run the emulator is Users list empty, when I check the sqlite database from the adb shell its populated,my code is as follows: DBDroid.java package com.android.dbdroid; import android.content.Context; import android.database.sqlite.*; import android.util.Log; import android.database.Cursor; import android.database.SQLException; import java.util.ArrayList; import java.util.List; public class DBDroid { private static final String DATABASE_NAME=mydroiddatabase.db; private static final String USER_TABLE_NAME=Users; private static final String CREATE_TABLE_USERS=CREATE TABLE IF NOT EXISTS + USER_TABLE_NAME + (LastName VARCHAR, FirstName VARCHAR,Country VARCHAR, Age INT(3));; private static final int DATABASE_VERSION = 1; private SQLiteDatabase myDb=null; public DBDroid(Context ctx){ myDb = ctx.openOrCreateDatabase(DATABASE_NAME, 0, null); myDb.execSQL(CREATE_TABLE_USERS); this.createUsers(); } public void createUsers(){ myDb.execSQL(INSERT INTO + USER_TABLE_NAME + (LastName, FirstName, Country, Age) + VALUES ('Gramlich', 'Nicolas', 'Germany', 20);); myDb.execSQL(INSERT INTO + USER_TABLE_NAME + (LastName, FirstName, Country, Age) + VALUES ('Doe', 'John', 'US', 34);); } public ListUser getUsers() { ArrayListUser users = new ArrayListUser(); try{ Cursor c = myDb.query(USER_TABLE_NAME, new String[] { LastName, FirstName, Country,Age }, null, null, null, null, null); int numRows = c.getCount(); c.isFirst(); if(c!=null){ for (int i = 0; i numRows; ++i) { User user = new User(); user.lastName=c.getString(0); user.firstName=c.getString(1); user.country=c.getString(2); user.age=c.getInt(3); users.add(user); c.moveToNext(); } } }catch (SQLException e){ Log.e(DBDroid, e.toString()); } return users; } } UserManager.java: package com.android.dbdroid; import android.app.Activity; import android.content.Intent; import android.os.Bundle; public class UserManager extends Activity { @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.users_row); Intent i = new Intent(getApplicationContext(), UsersList.class); startActivity(i); } } UserList.java package com.android.dbdroid; import java.util.ArrayList; import java.util.List; import android.app.ListActivity; import android.os.Bundle; import android.util.Log; import android.widget.ArrayAdapter; import android.widget.ListView; public class UsersList extends ListActivity{ private DBDroid droidDB; private ListUser users; public void onCreate(Bundle savedInstanceState) { try{ super.onCreate(savedInstanceState); setContentView(R.layout.users_list); droidDB = new DBDroid(this); fillData(); }catch(Throwable e){ Log.e(DBDroid,e.toString()); } } private void fillData(){ ListString items = new ArrayListString(); users=droidDB.getUsers(); for(User user:users){ items.add(user.lastName); //items.add(user.lastName); //items.add(user.country); } ArrayAdapterString adapter = new ArrayAdapterString(this, R.layout.users_row, items); setListAdapter(adapter); } } users_list.xml: ?xml version=1.0 encoding=utf-8? LinearLayout android:id=@+id/widget28 android:layout_width=fill_parent android:layout_height=fill_parent android:orientation=vertical xmlns:android=http://schemas.android.com/apk/res/android; ListView android:id=@android:id/android:list android:layout_width=wrap_content android:layout_height=wrap_content /ListView TextView android:id=@+id/text1 android:layout_width=wrap_content