Create a new file in Android emulator path /data/ - android

I want to create a file in emulator android storage path /data/...
but it seems I can't create a new file by programs,
I should upload an empty file to /data/... and then write the file,
Can anyone help here?
openFileOutput("file.txt", MODE_PRIVATE) seems can create a new file in /data/data/Package/files/...
but it can't create in path /data/...
Thank you so much !

You cannot write directly to /data/ folder unless you have a rooted device. If you want to use /data/ directory then I would suggest using
/data/local/tmp/

This code can write a new file to the /data/data/Package/files/ folder. This may be the same with your code:
public void setparameter(int ilevel){
byte[] buffer = new byte[8];
buffer[0] = (byte)ilevel;
OutputStream output = null;
try{
output = openFileOutput("option.txt", MODE_WORLD_READABLE);
output.write(buffer);
output.flush();
output.close();
}catch (IOException e) { }
}

Related

Android work with db file from SD card

I wrote an app which logged data and saved it via SQL into a .db File. I had a method copying it from internal memory to SD card.
Now i wrote a second app, which needs to work with this particular .db file. As i think, that apps can't get access to package files from other apps
(in this case
/data/data/app1_package/databases/my_database.db
)
i need somehow to work with my DB on the SD Card. How do i do that?
Can i use this path in my SQLiteHelper class? Should i copy it from SD to my package, is that even possible (access rights etc.)?
I'm a beginner in databases, some help would be nice.
You can open any readable file path as a database:
File dbFile = new File( Environment.getExternalStorageDirectory(), "myfile.db" );
SQLiteDatabase db = SQLiteDatabase.openOrCreateDatabase(dbFile,null,null);
Note: check if sd-card is mounted before using this code.
yes place the DB file in your assets folder and get it this way :
DB_PATH="/data/data/app1_package/databases/my_database.db"
in your create :
is = getAssets().open("Meaniningss.db");
write(is);
the method :
public void write(InputStream is) {
try {
OutputStream out = new FileOutputStream(new File(DB_PATH));
int read = 0;
byte[] bytes = new byte[1024];
while ((read = is.read(bytes)) != -1) {
out.write(bytes, 0, read);
}
is.close();
out.flush();
out.close();
System.err.println(out + "\n");
} catch (IOException e) {
e.printStackTrace();
}
}

Where should I store a file in Android?

I have an application that creates a configuration file and a log file. I stored these in the external storage, but when I try it in my android emulator it doesn't work because the external storage isn't writable. If this happens, where should I store the files?
This is my code:
private void createConfigurationFile(){
File ssConfigDirectory =
new File(Environment.getExternalStorageDirectory()+"/MyApp/config/");
File file = new File(ssConfigDirectory, mUsername+".cfg");
if(!file.exists()){
try{
String state = Environment.getExternalStorageState();
if (!Environment.MEDIA_MOUNTED.equals(state)){
ssConfigDirectory = new File("PATH_WHERE_I_SHOULD_STORE_IT");
}
File ssLogDirectory = new File(ssConfigDirectory+"/SweetSyncal/log/");
ssLogDirectory.mkdirs();
ssConfigDirectory.mkdirs();
File outputFile = new File(ssConfigDirectory, mUsername+".cfg");
FileOutputStream fOut = new FileOutputStream(outputFile);
OutputStreamWriter osw = new OutputStreamWriter(fOut);
writeFile(osw);
osw.flush();
osw.close();
}catch(Exception e){
e.printStackTrace();
}
}
}
If the file isn't too big you can save it in the device's Internal Storage.
To access the Internal Storage you can use the following method:
FileOutputStream openFileOutput (String name, int mode)
(You need an instance of Context to use it)
Example:
String FILENAME = "hello_file";
String string = "hello world!";
FileOutputStream fos = openFileOutput(FILENAME, Context.MODE_PRIVATE);
fos.write(string.getBytes());
fos.close();
As to why the code you provided is not working then there are two possibilities:
You forgot to add the required permission (WRITE_EXTERNAL_STORAGE).
You'r emulator doesn't have an SD card enabled. Assuming you are using Eclipse you can enabled it in the AVD Manager. Just edit your AVD instance and type in the size of the SD card in the appropriate field. You should also add a hardware feature called SD Card Support and set it to TRUE.
There is a great article in the official Developer Guide which will tell you everything you need to know about storage in Android.
You can read it HERE

SQLite Database not showing up in File Explorer

I have an SQLite database in my application. When I go to check if the database is created or not in File Explorer, there is no any database file within the data folder. It didn't even show up using the adb shell command. Does this mean that the database hasn't been created yet? Any suggestions on how to solve this?
If you are using an actual device, you will not be able to see or access it from outside unless you root your phone.
If you are using the emulator, the DDMS view will let you navigate to it and pull it from there.
Or, you could have an issue with creating your DB. Without seeing your code we cannot tell.
EDIT
If you want to get the file off of a real device, you'll need to implement a method to copy it from your data directory to someplace where you do have access (such as your SD card). This would do the trick:
public void backup() {
try {
File sdcard = Environment.getExternalStorageDirectory();
File outputFile = new File(sdcard,
"YourDB.bak");
if (!outputFile.exists())
outputFile.createNewFile();
File data = Environment.getDataDirectory();
File inputFile = new File(data, "data/your.package.name/databases/yourDB");
InputStream input = new FileInputStream(inputFile);
OutputStream output = new FileOutputStream(outputFile);
byte[] buffer = new byte[1024];
int length;
while ((length = input.read(buffer)) > 0) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
} catch (IOException e) {
e.printStackTrace();
throw new Error("Copying Failed");
}
}

Android SD card path inaccessible

I have a knock off android device HT-PAD1051
But, really having trouble accessing the root directory in the built in sd card. card is partitioned and what is called "mnt/extsd" is accessible. Any ideas on how to get at root?
The device has been rooted.
Thanks
Joe
For any android device, If you want to get external storage always use.
Environment.getExternalStorageDirectory();
instead of giving hard coded path.
public void writeFile(String text){
try{
Writer output = null;
File dir = new File(Environment.getExternalStorageDirectory() + "/RequiredDirectory");
dir.mkdirs();
File file = new File(dir,<filename>);
output = new BufferedWriter(new FileWriter(file,true));
output.write(text);
output.close();
System.out.println("Your file has been written");
}catch (Exception e) {
e.printStackTrace();
}
}

How to create/write file in the root of the Android device?

I found out that you can use something like this to create a file:
FileOutputStream fs = openFileOutput("/test.in", MODE_WORLD_WRITEABLE);
String s = "[Head]\r\n";
s += "Type=2";
byte[] buffer = s.getBytes();
fs.write(buffer);
fs.close();
When running the above code I get an IllegalArgumentException stating:
java.lang.IllegalArgumentException:
File /test.in contains a path
separator
and I'm guessing the "/" is not appreciated. I wanted the "/" since I need to write the file to the root directory of the device, as stated in the API in trying to follow:
A request is a textfile (UNICODE) with
the file extension ".in". The
application reads and parses the .in
file when it's placed in root
directory on the mobile device.
Question is: how do I place a file in the root-directory? I have been looking around for an answer, but haven't found one yet.
Context.openFileOutput is meant to be used for creating files private to your application. they go in your app's private data directory. you supply a name, not a path: "name The name of the file to open; can not contain path separators".
http://developer.android.com/reference/android/content/Context.html#openFileOutput(java.lang.String, int)
as for your question, you can't write to / unless you're root:
my-linux-box$ adb shell ls -l -d /
drwxr-xr-x root root 2010-01-16 07:42
$
i don't know what your API is that expects you to write to the root directory, but i'm guessing it's not an Android API and you're reading the wrong documentation ;-)
You can add files with path in private directory like that
String path = this.getApplicationContext().getFilesDir() + "/testDir/";
File file = new File(path);
file.mkdirs();
path += "testlab.txt";
OutputStream myOutput;
try {
myOutput = new BufferedOutputStream(new FileOutputStream(path,true));
write(myOutput, new String("TEST").getBytes());
myOutput.flush();
myOutput.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
I faced the same problem today
java.lang.IllegalArgumentException: File /test.txt contains a path separator
and when i tried the following it worked.
File inputFile = new File("/storage/new/test.txt");
FileInputStream isr = new FileInputStream(inputFile);
DataInputStream in = new DataInputStream(isr);
if(!inputFile.exists()){
Log.v("FILE","InputFile not available");
}
else
{
while((line = in.readLine()) != null)
{
........ //parse
}
}
(Btw, I was getting this problem outside /root dir and while searching i saw this post)

Categories

Resources