I have spent many hours trying to find/create files for an app I am writing. When I pull the application directory name I get: /data/data/com.example.android.[myapp]/files. I am using File(getFilesDir():
File fileDir = new File(getFilesDir() + File.separator);
Log.i(TAG, "File directory: "+fileDir);
When I try to find this path I find many application folders here: Android/data/com but no /data/data folder under Android. There are many other application folders there but not mine. I see the same results whether I use Android Files app or Windows Explorer over USB. I've also tried to look using Eclipse DDMS tab. I see a data folder with a (+) to the left but when I click, it does not expand.
I have also tried creating the directory and file manually with Windows explorer and my app still can't find neither the Android/data/com.example... nor the Android/data/data/com.example... paths.
Also puzzling to me is when the app creates the path and file and write to it (using MODE_WORLD_WRITEABLE) I get no exceptions thrown but then I am unable to read it back or see it with either of the tools mentioned above. I have set the manifest permissions to WRITE_INTERNAL_STORAGE and WRITE_INTERNAL_STORAGE for the app.
Obviously, I am making a very basic mistake.
I am on Android 4.1.2 (API 16).
Sincerely,
ScratchingMyHead
To get the path of my application directory, Try this code sample
PackageManager m = getPackageManager();
String s = getPackageName();
try {
PackageInfo p = m.getPackageInfo(s, 0);
s = p.applicationInfo.dataDir;
} catch (NameNotFoundException e) {
Log.w("yourtag", "Error Package name not found ", e);
}
When I try to find this path I find many application folders here: Android/data/com but no /data/data folder under Android.
That is because you are looking on external storage, not internal storage where your files are. Use DDMS on an emulator to examine internal storage.
I've also tried to look using Eclipse DDMS tab. I see a data folder with a (+) to the left but when I click, it does not expand.
That would sound like what you will get when testing on hardware, as neither you nor DDMS have access to the contents of /data on production hardware.
Related
In other Apps I can access files in directories like /storage/emulated/0/Android/data/com,xxx,yyy/files
In my own app I want to use it too. But with getApplicationInfo().dataDir I receive /data/user/0/com.xx.yy/files
How can I receive the path to /storage... and how to create this directory (it was not create automatically after installing the App)
Now I find out that getExternalFilesDir(null).getAbsolutePath() do the job
In my android app, I create a file and write some test text into it:
File externalPath = getExternalFilesDir(null);
File importPath = new File(externalPath, "pd-import");
if(!importPath.exists()) {
Log.d(this.getClass().getSimpleName(), "Create import dir: " + importPath.getAbsolutePath());
importPath.mkdirs();
}
File readme = new File(importPath, "README.txt");
try {
FileWriter fw = new FileWriter(readme);
fw.write("This is a test");
fw.flush();
fw.close();
} catch (IOException e) {
e.printStackTrace();
}
This writes the file, which can be approved with an android file browser like B1 File Manager. It's written to HOME/Android/data/JAVA_PACKAGE_NAME/files/pd-import/README.txt
The logcat shows me:
D/MainActivity﹕ Create import dir: /storage/emulated/0/Android/data/JAVA_PACKAGE_NAME/files/pd-import
When I connect my Nexus, where I tested the code, to my Ubuntu Laptop via USB, I see all the other applications data directories like NEXUS 5/Interner Speicher/Android/data/ALL_THE_OTHER_JAVA_PACKAGE_NAMEs ("Interner Speicher" stands for: internal memory). But the folder is not listed for my app is not listed.
Do I need to set some additional medatdata/information/whatever, to have the folder listed over the MTP connection? Anny suggestions?
Does the directory appear when the device is rebooted ? If yes, I think this bug is the cause : https://code.google.com/p/android/issues/detail?id=38282
All phones using MTP instead of USB Mass storage do not properly show
the list of files when that phone is connected to a computer using a
USB cable. Android apps running on the device also cannot see these
files.
This affects files written using Java APIs, but does not appear to
affect files written using the C API.
The solution is to reboot the device.
It does not seem to be resolved.
First of all, I'm an android newbie. Now my question...
I want to create a folder with some files, so I can easily access them when I plug in my droid via USB.
"directory.exists" returns true, saying the folder exists, but when I go to Windows File Explorer, Computer\Nexus 5\Internal storage, I do not see my app folder listed in there.
getExternalStorageDirectory returns: /storage/emulated/0/MyAppName
File directory = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + "/MyAppName/");
if (!directory.exists()) {
directory.mkdir();
}
I have added
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in manifest.
What am I doing wrong?
You're not doing anything wrong. Android does.
That MTP connection is unreliable as contents displayed is not in sync with the latest files wrote on the phone.
You can cause a manual rescan or reboot the phone... see below:
android bug
similar question with workarounds
On the emulator, I downloaded an image from Google. I can find the image on the emulator, but I have no idea what the file location of the image is. To debug my app I need to know where that image is. How can I get the full path of the image?
From the AVD documentation:
By default, the android tool creates the AVD directory inside ~/.android/avd/ (on Linux/Mac), C:\Documents and Settings\<user>\.android\ on Windows XP, and C:\Users\<user>\.android\ on Windows 7 and Vista.
Update (2020-02-22): This wording isn’t on the current documentation page anymore, but the location appears to be the same (C:\Users\<user>\.android\) on Windows 8 and 10.
The system images are downloaded in
[ {android_version_home_dir}/sdk/system-images/{android-version-number}/system.img> ]
and the avd are created in C:\Users\.android\avd\ (windows) or ~/.android/avd/ (linux/mac)
Not obvious thing in official documentation
This here seems to work for me:
String path = Environment.getExternalStorageDirectory().getPath();
String myJpgPath = path + "/Download/dog-best-friend-1.jpg";
If your image is stored on your emulator then you can find that image using file Explore
First Start your Emulator then:
Open your File Explorer and go to mnt/sdcard/Download you will find your image here if its downloaded.
To open file explore in Eclipse : window/show view/other/File Explore
To find the exact path open the emulator go to Photos then click on a photo (if there is any) then click on i (for info). There you can see the exact path on your device.To access these files dynamicly try:
String path = Environment.getExternalStorageDirectory().getPath();
String pathPhotos = path2 +"/Download";
File[] fileArr = new File(pathPhotos).listFiles();
for(int i = 0; i < fileArr.length; i++) {
Log.d("fileName12", " " + fileArr[i].getPath());
}
In case listFiles returns null you have to add:
uses-permission *android:name="android.permission.READ_EXTERNAL_STORAGE" />
And change the settings on the Emulator -> settings -> Apps -> App Permissions -> storage -> your app name -> switch on
In Windows ---> C:\Documents and Settings<user>.android\
On windows:-> C:\Users\yourUser\Documents\AndroidStudio\DeviceExplorer\Pixel_2_API_30 [emulator-5554]\data\data\com.yourpackage\files
After the emulator name, you can choose the path in your code.
I have a db located at
ctx.getApplicationContext().getDir("data", 0) + "/" + "db.db4o";
How can I browse this file with an file manager or via USB? I can't seem to find it :<
Browse with file manager, or USB, what does that mean? You can access the data directory, through a file manager, only if you are on a rooted device or an emulator.
Your application specific file are not directly stored in the data directory, but
data/data/your_package_name
you could create an "asset" folder into your android project and put it there. Before that you can try with this:
InputStream is = getAssets().open("db.db4o");
And convert "is" if is necessary.
Regards