I have an internal SQLite DB under "Assets" Folder in which i have stored 100 usernames & corresponding Passwords, How do i access it via phoneGap.? I have read
http://docs.phonegap.com/en/1.6.1/cordova_storage_storage.md.html#Storage
connecting to sqlite database from phonegap html file for android
http://wiki.phonegap.com/w/page/16494756/Adding-SQL-Database-support-to-your-iPhone-App
Am still not able to figure out How to connect to my internal DB which is already Created, dbname = userauthTablename = regparameters = usrnm , psw Any Answer is Highly EncouragedAm using this here.
/**
* Creates / Opens a connection to DB
*/
DB.openDB = function() {
try {
if (!window.openDatabase) {
//alert('Cannot open database!');
} else {
var shortName = 'db_name';
var version = '1.0';
var displayName = 'DBNAME';
var maxSize = (DEVICE_TYPE == DEVICE_ANDROID || DEVICE_TYPE == DEVICE_ANDROID_TAB) ? 5242880 : 1000000; ////819200; //65536; // in bytes // increased to support Android//163840; //
this.vocabDB = window.openDatabase(shortName, version, displayName, maxSize, this.DBCreated);
this.DBSupported = true;
}
} catch(e) {
//console.log("DB Error handling code goes here.");
//console.log(e);
return;
}
}
Well, you can do a window.openDatabase() to a database in the assets folder. You'll need to copy it to the right place so that the WebView will load it. Check out this:
http://gauravstomar.blogspot.ca/2011/08/prepopulate-sqlite-in-phonegap.html?utm_source=feedburner&utm_medium=feed&utm_campaign=Feed:+GauravSTomarBootstrappingIntelligence+(Gaurav+S+Tomar+:+Bootstrapping+Intelligence)
post as Gaurav gives you code to do this on both Android and iOS.
Related
I'm trying get a list of all documents in a device without breaking the illusion of being in my app like file picker does. I test with my physical device and it was working before but since I bought a better device using Android 11, I've been having issues.
This is what I did back then that worked. After getting permission errors for trying to access /storage/emulated/0/Android, I think there has been a permission update so I am now temporarily avoiding that folder manually but still I couldn't see any files until I added .mp4 to the allowed extensions and then it showed only my videos.
List<File> bookList = [];
List<List<String>> bookPropertyList = [];
fetchBooks() async {
var rootPath = await ExternalPath.getExternalStorageDirectories();
var docPath = Directory(rootPath[0]);
var docassets = docPath.listSync(recursive: true, followLinks: true);
for (FileSystemEntity asset in docassets) {
if (asset is File) {
String name = basename(asset.path);
if (name.endsWith('.pdf') ||
name.endsWith('.PDF') ||
name.endsWith('.txt') ||
name.endsWith('.TXT') ||
name.endsWith('.doc') ||
name.endsWith('.DOC') ||
name.endsWith('.docx') ||
name.endsWith('.DOCX')) {
String size = await getSize(asset.statSync().size, 1);
int day = asset.statSync().modified.day;
int month = asset.statSync().modified.month;
int year = asset.statSync().modified.year;
String date = '$day/$month/$year';
List<String> docProperty = [name, size, date];
bookPropertyList.add(docProperty);
File? file = asset.absolute;
bookList.add(file);
}
}
}
}
Is there a reason why it wouldn't show my files?
And is there a better way to get the files without leaving the app? Perhaps a light package I could use.
Using Xamarin.Forms, has the way to write to file changed since Android 8.0?
I updated an existing project of mine, which includes a very simple function to write a text file to local storage, but after running the app and testing the write to file part, it just freezes and crashes out. There are no errors in the console or even evidence of it doing anything.
My function is as such:
public void Submit_Clicked(object sender, EventArgs e)
{
{
string text = NameEntry.Text;
string path = Environment.GetFolderPath(Environment.SpecialFolder.Personal);
string filename = Path.Combine(path, "DeviceProfile.txt");
File.WriteAllText(filename, text);
}
catch(Exception ex)
{
DisplayAlert("Error:", ex.Message.ToString(), "Ok");
}
}
I cant see why this shouldn't work. Does someone know any reason why this wouldn't?
Like Jason said, the code below and the code you provided both work to write a text file to local storage.
string text = NameEntry.Text;
var path = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DeviceProfile.txt");
using (var writer = File.CreateText(backingFile))
{
await writer.WriteLineAsync(text);
}
You could read the text from the file to check.
public async Task<string> ReadCountAsync()
{
var backingFile = Path.Combine(System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal), "DeviceProfile.txt");
if (backingFile == null || !File.Exists(backingFile))
{
return null;
}
var text = string.Empty;
using (var reader = new StreamReader(backingFile, true))
{
var str = await reader.ReadToEndAsync();
text = str;
}
return text;
}
The path Environment.SpecialFolder.Personal you used to save the file is internal storage.
In Internal Storage, you couldn't see the files without root permission. If you want to view it, you could use adb tool. Please check the way in link.
How to write the username in a local txt file when login success and check on file for next login?
I am trying to create an Android application created using Adobe Flash Actionscript 3. I wanted each user of the app to input their name in the beginning of the application, then they have the capability to save their progress in current frame (and it will be saved into a save slot or similar). However, the problem arises when another user is going to use the app, he/she must enter a distinct username, and he/she can save anytime (and load his/her distinct load progress, different from the previous user.). And it goes on.
I am a newbie in programming and I hope you could help me. Any suggestions will be appreciated. Thanks!
This my code for creating a username and saving it:
function handleClick(Event:MouseEvent):void
{
var myFirstVariable = boxOne.text;
boxTwo.text = myFirstVariable;
gotoAndStop("opening_scene")
}
myButton2.addEventListener(MouseEvent.MOUSE_UP, handleClick);
UPDATED EDIT 2: Here is my code for saving and loading. Still not working:
var so:SharedObject = SharedObject.getLocal("Test");
var userName:String = nameField.text;
if (so.data.users == null)
so.data.users = new Object();
btnSave.addEventListener(MouseEvent.CLICK, onClick);
function onClick(e:MouseEvent):void
{
if (so.data.users[userName] == null)
so.data.users[userName] = new Object();
so.data.users[userName].lastframe = currentFrame;
so.flush();
trace(userName);
}
btnLoad.addEventListener(MouseEvent.CLICK, reloadBtnClick);
function reloadBtnClick(e:MouseEvent):void
{
if (so.data.users[userName] == null)return;
if (so.data.users[userName].lastFrame == null) return;
gotoAndStop(so.data.users[userName].lastFrame);
trace(userName);
}
I recommend that you use a SQLite Data Base to save your data can find all the documentation here Manipulating SQL database data Well is a little bit more complicate but works like have tables in Excel.
As Vesper suggested, you need to have a storage with all the users you store. For example:
// Put user name here.
var userName:String;
var SO:SharedObject = SharedObject.getLocal("save");
// Create storage for users.
if (SO.data.users == null) SO.data.users = new Object();
// ...
function saveCurrentFrame(event:MouseEvent):void
{
// Create storage for current user by their name.
if (SO.data.users[userName] == null) SO.data.users[userName] = new Object();
SO.data.users[userName].lastframe = currentFrame;
SO.flush();
}
function getLastFrame(event:MouseEvent):void
{
trace(1);
// Skip this if there's no record for
if (SO.data.users[userName] == null) return;
trace(2);
if (SO.data.users[userName].lastFrame == null) return;
trace(3);
gotoAndStop(SO.data.users[userName].lastFrame);
trace(4);
}
As the title states, my user's on iOS and Android are reporting their save data has been deleted when I push an update to the respected storefronts. The game saves and restores data properly on application load and exit. I have not changed any save information locations or package names... Just mere bug fixes, build, push.
Any clarification would be helpful, or proposed redundant data backup scheme's I should pursue to ensure the end-user experience is from henceforth not affected negatively. I would also like to understand better how I can test this as a release package is not allowed to update a market installed application.
var so:SharedObject = SharedObject.getLocal("storage");
var u:User;
if(so.data != null){
u = so.data.user;
}
trace("Loading application...");
if(u != null){
trace("LOADING SAVE DATA");
user = u;
}else{
trace("NO SAVE DATA EXISTS");
user = new User();
}
I have discovered the cause of this issue and it seems that Adobe has identified and fixed the issue as well (as of AIR 3.5). https://bugbase.adobe.com/index.cfm?event=bug&id=3347676
SharedObjects are stored within a directory named according to the SWF used by your application, so if your application is running off "myApp.swf" the SharedObject will be stored in a directory "myApp". If you change the name of this SWF (i.e. the corresponding XML build sheet configuration file for your AIR project) any subsequent builds will store their SharedObjects in a new location.
The issue described in this bug was specifically denoted for iOS, wherein the application was not storing the SharedObject in the corresponding SWF location as described above but in a separate location denoted by the "filename" attribute in your project's XML build sheet.
I have also discovered that Adobe does indeed condone the use of a SharedObject for persistent storage on a mobile platform.
I have developed a simple backup for future version save redundancy in case future updates fail to persist the SharedObject.
/** the last timestamp a deep save was completed */
private var mLastSave:Number = -1;
/**
* save the state of the globals object and all of its
* sub objects.
* #warning
* all objects must implement variables in a "public" state.
* Private variables are not saved within the persistance manager
*/
public function save():void{
var so:SharedObject = SharedObject.getLocal("storage");
so.data.user = user;
so.flush();
saveDeep();
}
/**
* Save the SharedObject to denoted mobile applicationStorageDirectory.
*/
public function saveDeep():void{
// save on first application save or after
// every 5 minutes
if(mLastSave == -1 || mLastSave < getTime() + 300000){
mLastSave = getTime();
var file:File = File.applicationStorageDirectory.resolvePath("userSave");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.WRITE);
var ba:ByteArray = new ByteArray();
ba.writeObject(user);
fileStream.writeBytes(ba);
fileStream.close();
}
}
/**
* Load the application from the SharedObject be default
* if the SharedObject DNE attempt to load from the
* applicationStorageDirectory, if neither exist
* create new User object
*/
public function load():void{
registerClassAlias("user", User);
// Create/read a shared-object named "userData"
var so:SharedObject = SharedObject.getLocal("storage");
var u:User;
if(so.data != null){
u = so.data.user;
}
trace("Loading application...");
if(u != null){
trace("LOADING SAVE DATA");
user = u;
}else{
trace("NO SAVE DATA EXISTS");
var file:File = File.applicationStorageDirectory.resolvePath("userSave");
if(file.exists){
trace("Found userSave backup -attempting to load");
var fileStream:FileStream = new FileStream();
fileStream.open(file, FileMode.READ);
var ba:ByteArray = new ByteArray();
fileStream.readBytes(ba);
fileStream.close();
try{
user = (ba.readObject() as User);
}catch( e : EOFError ){
trace("SharedObject did not exist, attempted userSave load, failed");
}
}else{
user = new User();
trace("created New user...");
}
}
}
I have two apps, one is a trial version the other the full version of a game, both made with adobe air. While saving data via the sharedobjects solution is no problem, I would like to use "one" savegame for both appsm, so users can keep their progress when upgrading to the full version. I tried around a little. But code like e.g. ...:
SharedObject.getLocal("myApp","/");
... doesnt work. So the question is, is there a way to have two Air apps using the same shared object? Or maybe if not using, at least "read" the shared object of another Air app?
Thanks in advance,
ANB_Seth
The answer is yes, I actually made a game transfer system for iOS and Android via network connection and 6 digit hash the user has to enter in the newly installed app to fetch the SO from the server. You could do this with a simple file stored locally on the SD card or other local storage device.
/**
* send this user's current save data to the server
*/
public function send():void{
var ba:ByteArray = new ByteArray();
// Main.sv.user - is the registerClassAlias object we write/read locally via SharedObject
ba.writeObject(Main.sv.user);
var name:String = Crypto.hash("Random Secrect Salt - typically user score, name, etc.");
// create 6 digit hash
var key:String = Crypto.hash(name).slice(0, 6).toUpperCase();
var request:URLRequest = new URLRequest ( 'https://sharedobject.com/transfer/save/name/'+name+'/key/'+key );
var loader: URLLoader = new URLLoader();
request.contentType = 'application/octet-stream';
request.method = URLRequestMethod.POST;
request.data = ba;
loader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:Event) {
trace("error - network");
onSaveRestoreEvent(1);
});
loader.addEventListener(Event.COMPLETE, function (evt:Event) {
addChild(new BaseDialog("Save Sent To Server", "Your data has been sent to the server. To get this data back from the server " +
"you will need your secret key. Please write this six digit key down:\n"+name));
});
loader.load( request );
}
/**
* create a GET SO dialog
*/
public function receive():void{
var text:Sprite = new Sprite();
var textInput:TextInput = new TextInput();
textInput.width = Constants.SCREEN_WIDTH-100;
textInput.y = -50;
text.addChild(textInput);
var dialog:BaseDialog = new BaseDialog("Enter Secret Save Key", "Please enter your six digit secret save key in the field below, then press \"Get\".\n\n",
"Get", function():void{
text.removeChildren();
var url:String = "https://sharedobject.com/transfer/get/name/"+textInput.text; //servlet url
var request:URLRequest = new URLRequest(url);
//get rid of the cache issue:
var urlVariables:URLVariables = new URLVariables();
urlVariables.nocache = new Date().getTime();
request.data = urlVariables;
request.method = URLRequestMethod.GET;
var loader:URLLoader = new URLLoader();
loader.dataFormat = URLLoaderDataFormat.BINARY;
loader.addEventListener(Event.COMPLETE, function (evt:Event) {
var loader:URLLoader = URLLoader(evt.target);
var bytes:ByteArray = loader.data as ByteArray;
bytes.position = 0;
if(bytes.length <= 10 || !(bytes.readObject() is User)){
onSaveRestoreEvent(2);
}else{
try{
bytes.position = 0;
Main.sv.user = (bytes.readObject() as User);
Main.sv.save();
onSaveRestoreEvent(0);
}
catch( e : EOFError ){
onSaveRestoreEvent(3);
}
}
});
loader.addEventListener(IOErrorEvent.IO_ERROR, function (evt:Event) {
trace("error - network");
onSaveRestoreEvent(1);
});
loader.load(request);
},
"Close", function():void{text.removeChildren();}, null, null, text);
dispatchEvent(new CreateBaseDialogEvent(dialog));
}
/**
* called after the restore save system is done
* #param prompt int [0 = complete][1 = error network][2 = error key][3 = error EOF]
*/
private function onSaveRestoreEvent(prompt:int):void{
var dialog:BaseDialog;
if(prompt == 0){
dialog = new BaseDialog("Restore Complete!", "All save data has been restored.");
}else if(prompt == 1){
dialog = new BaseDialog("Network Error!", "Please seek an internet connection and try again.");
}else if(prompt == 2){
dialog = new BaseDialog("Invalid Secret Key!", "The key you've entered seems to be invalid, or the save data has expired on the server. " +
"Data only lasts on the server for 24 hours.");
}else{
dialog = new BaseDialog("Error!", "There was an issue getting the file from the server. Please try the transfer again.");
}
dispatchEvent(new CreateBaseDialogEvent(dialog));
}