Where do files in dataDir exist in Android? - java

I am working on an Android App where a file is given its location like the code given below.
The method is like this:
public boolean log(ApplicationInfo aplinfo)
{
ApplicationInfo objAppinfo;
objAppinfo = apinfo;
_strFilePath = objAppinfo.dataDir;
_strFilePath += "/";
}
Where Appicationinfo is my activity name.
When I see in logcat I am getting file path as
09-07 06:34:41.355: INFO/System.out(525):
FileName::::::::::/data/data/com.andr.activity/Info_SA1.txt
My doubt is where can I find this file, where does this file exist?
When I am trying to read the contents of this file, then the contents that are written in to this file are also visible. But I am not able to find the physical location of the file.
Where does data/data/com.andr.activity path exists?
Can anyone help me in sorting out this issue?
Thanks in Advance.

This lives in the android filesystem, which is different from your computer's file system. To view the files in android filesystem, use adb shell on the command prompt. Run:
adb shell ls /data/data/com.andr.activity. Make sure your device/emulator is connected before you do this.
Or if you are using eclipse, switch to DDMS view and check out the file explorer.
To View file:
adb shell
Inside shell use:
cd /data/data/com.andr.activity
cat filename
Or Use adb pull to copy file to your computer's local filesystem and then view it using any editor. Read more about adb pull here

Related

How do I store a file on my Android emulated device and then access it from my program?

I am looking to manually add a [text] file to my Android emulated device, and then retrieve it as a file when I am running my program. I can access files in my own C drive in testing, but obviously I cannot access the C drive of my PC on my emulator.
I can get into my devide on file explorer [device].adk/data and the folder list is:
- app
- benchmarktest
- misc
- nativetest
And how I am trying to retrieve the file is through:
public static final String MY_FILE = "[directoryhere]";
My question is: which folder would I store my file in, and what directory would I retrieve it from?
Alternatively, I have tried to store it on my localhost and access the directory as:
public static final String MY_FILE = "http://[myip]/files/my.file";
however, when I try to retrieve it I get the following error:
java.io.FileNotFoundException: \\[myip]\files\my.file (The network name cannot be found)
Many thanks.
try these two methods to store your files in your emulator
the easiest way is that just do drag and drop i mean when u
start your emulator drag your file and drop it and it will save
it in your emulator than you can easily access that
you can use the ADB via Terminal to pass the file From Desktop to Emulator.
Copy from desktop to emulator:
adb push < local > < remote >
Copy from emulator to desktop:
adb pull < remote > < local >
Here < local > is path of file / folder on your desktop and < remote> is
path of file / folder on your emulator.
Here is an example:
adb push foo.txt /sdcard/foo.txt
foo.txt will be pushed (copied) to the emulator.
for more information
Android Debug Bridge

Android export SQLite Database to Computer / Mac

I have a SQLite Database on my Android Device (My own App). Now i want to see the SQLite Database on my Computer.
Is there any solution ( easy way ) to get the Database without root rights and without a SD-Card on my Computer?
I tried to google it but i can't find any solution for this problem....
Edit: I'm using not the Emulator ( need the Camera on my Device).
if phone is not rooted you cant access directly your DB, but you can copy it to download folder, then, copy to PC
public static void copyAppDbToDownloadFolder(Activity ctx) throws IOException {
File backupDB = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"databse_name.db"); // for example "my_data_backup.db"
File currentDB = getApplicationContext().getDatabasePath("databasename.db");
if (currentDB.exists()) {
FileChannel src = new FileInputStream(currentDB).getChannel();
FileChannel dst = new FileOutputStream(backupDB).getChannel();
dst.transferFrom(src, 0, src.size());
src.close();
dst.close();
}
}
This method worked for my unrooted device.
# check if your device connected
adb devices
adb shell
run-as your.package.name
chmod 777 databases/your.database.name
exit
cp /data/data/your.package.name/your.database.name /mnt/sdcard/
exit
adb pull /mnt/sdcard/your.database.name
If you are saving Sqlite DB in default location , you can find the same in path like following
/data/data/com.dev.myapplication/databases/mydb.db
You can find the same using Android Device Monitor, through which you can navigate to both internal and external memory.
Once you find the database file, extract the same and save in desktop after connecting through USB cable. You can use option "Pull a file from device"
Once you have extracted file to desktop, use any tools similar to Mozilla Firefox SQLite viewer to view database.

VB.Net How to run the application in a specific folder?

So a little bit of my code is to run an application (a batch file in an external folder that the user can select.) The problem is that when I use
Path.GetDirectoryName(O.FileName) + "\BasicStart.bat"
however when I try to do the above it fails to open to java application due to the class folders trying to be referenced from where the vb.net application is launched
The code in the .bat is
#echo off
#title JavaApplet12
set CLASSPATH=.;dist\*
java -client -Dnet.sf.odinms.wzpath=wz server.Start
pause
And I know I can hard set the classpath and wzpath but users of my application will not want to do this.
How can I launch it in vb.net so that the application is launched from that location and can read the files from that folder location.
This should be resolvable by setting the working directory of the process before you start it:
Dim processStartInfo = New ProcessStartInfo()
processStartInfo.WorkingDirectory = Path.GetDirectoryName(O.FileName)
processStartInfo.FileName = "BasicStart.bat"
Process.Start(processStartInfo)

java.io.IOException: The system cannot find the path specified

On a particular server (Windows 2012 server R2) I am having trouble creating a temp file. I get the following error everytime I try.
java.io.IOException: The system cannot find the path specified
at java.io.WinNTFileSystem.createFileExclusively(Native Method)
at java.io.File.createTempFile(Unknown Source)
etc..
The error happens everytime the following code is ran:
InputStream inputStream = portalBean.createPDF( sessionID, foCode );
Things I have tried
Changed the java.io.tmpdir variable on the fly. System.setProperty("java.io.tmpdir", "C:\\");
Added -Djava.io.tmpdir=c:\\temp to the webnetwork lax file to an unrestricted location.
I tried setting the webNetwork service to run as a specified user with rights to temp files e.g. the Administrator.
Made sure I have free disk space and I cleaned out the c:\windows\temp folder.
Made sure the tmp environment variables were set to their default values.
I also tried running the service from a command prompt which was opened with the Run As Administrator option.
And the IOException lingers still. I have another server running the same code without issue (Windows Server 2012).
Does anyone else have any Ideas of what else I can try to resolve this issue? And or any tips on how I can debug the issue more thoroughly to get a grasp of what is going on?
One tool you can use to debug this is process monitor from system internal tool kit. The step is: add a filter to only monitor your process (I think it is javaw.exe in your case), after the error happens, go through the file activities in the process monitor log, you can find how the process is finding files and which directories the process searched. If the process is searching in the wrong directory, you can find it from the log.
I just used this tool to figure out a JVM crash problem today.
Based on the description of your problem, I guess the path variable of the process is changed in the middle of your code, with another tool process explore you can view the path variable of the process, it might help.
Try and create instead a directory somewhere under your home directory:
final Path tmpdir = Paths.get(System.getProperty("user.home"), "tmp");
Files.createDirectories(tmpdir);
System.setProperty("java.io.tmpdir", tmpdir.toAbsolutePath().toString());
Then try and Files.createTempFile() in there.
Note that if Files.createDirectories() refers to an existing file which is not a directory, you'll get a FileAlreadyExistsException.

Change permission of a created file so only the current user can read it

I need to set the permission to read a file to a specific user of the operating system, how can I do this in Java?
Edit:
The file will be created with permissions just to the user running the application, than it needs to set read permission to a single user, other users will not have the permissions to read the file.
Use the methods setExecutable, setReadable, and setWritable in java.io.File. You can use these to change any permission bit of a file you own. Direct link: http://download.oracle.com/javase/6/docs/api/java/io/File.html#setReadable%28boolean%29.
Testing this on MacOSX revels that only the user read value is changed. When program
import java.io.File;
public class Test
{
public static void main(String[] args)
{
File f = new File("test.txt");
f.setReadable(true);
}
}
the folloiwng happens.
$ touch test.txt
$ chmod 000 test.txt
$ javac Test.java
$ java Test
$ ls -l test.txt
-r-------- 1 morrison staff 0 Jun 7 13:28 test.txt
If you're targetting Windows Vista/7, build your JAR as EXE and embed a manifest requesting for Admin rights.
If it's just an I/O problem, use the default File methods setReadable, setWritable, setExecutable :)
In regards to making the permissions for a single user, start with using ncmathsadist's code to add read permissions, then change the owner of the file to whoever needs access.
I found in the Ant source code they use for the change-owner task. For unix this can be found in the Ant source tree at org/apache/tools/ant/taskdefs/optional/unix/Chown.java. You might be able to include this and use it as an API call to change the user programmatically.
if you want to change file permissions on old Java versions like Java 5, you can use this:
Runtime.getRuntime().exec("chmod 000 " + PATH + fileName);
on windows you'll have to replace chmod with the appropriate CACLS.exe command syntax

Categories

Resources