Showing posts with label File. Show all posts
Showing posts with label File. Show all posts

Reference a File on your Android Device

How to Reference a File on your Android Device

File was added in API Level 1

An "abstract" representation of a file system entity identified by a pathname. The pathname may be absolute (relative to the root directory of the file system) or relative to the current directory in which the program is running. The actual file referenced by a File may or may not exist. It may also, despite the name File, be a directory or other non-regular file.

1. Create an Android project, if you don't already have one.

2. In the MainActivity.java file, add the below line to the imports section.

import java.io.File;

3. In the onCreate method, add the line below. Replace com.akp.mypackagename is your package name. Replace mySQLiteDatabase.db with your database name, or filename.

File myFile = new File("/data/data/com.akp.mypackagename/mySQLiteDatabase.db"); 

4. Compile and run!

Related Articles:

Resources:
http://developer.android.com/reference/java/io/File.html
http://mobile-development-tutorial.blogspot.com/2012/12/android-external-sqlite-database.html

Web hosting

Check if a File Exists

How to Check if a File Exists

exists() was added in API Level 1
exists() - Returns a boolean indicating whether this file can be found on the underlying file system.

1. Create a Reference to the File named myFile.

2. In the MainActivity.java file, add the below code to your onCreate method.
  1. 
    if (myFile.exists()) {
        //File exists.
    } else {
        //File does not exist. 
    }
    
  2. 3. Alternately, you can test for it to NOT to exist. 

  3. if (!myFile.exists()) {
  4.     //File does not exist.
    } else {
        //File exists. 
    }
  5. 3. Compile and test!

  1. Resources:
    http://developer.android.com/reference/java/io/File.html#exists()
    http://mobile-development-tutorial.blogspot.com/2012/12/android-external-sqlite-database.html
    http://stackoverflow.com/questions/8007993/in-android-check-if-sqlite-database-exists-fails-from-time-to-time