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
Related
Im trying to create a sqlLite db. This code works and create the db and allow me do insert and select.
string dbPath = Path.Combine(
System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal),
"ormdemo.db3");
//dbPath = "/storage/emulated/0/DCIM/ormdemo.db3";
var db = new SQLiteConnection(dbPath);
db.CreateTable<Stock>();
Right now the dbPath returned is:
"/data/user/0/MyFirstGPSApp.MyFirstGPSApp/files/ormdemo.db3"
But when I use a sqlLite Manager to try open the db I cant find the folder /data/user
Where is that folder /data/user?
The sqlLite have the option to open "APP" databases, but when try to select MyFirstGPSApp say need a root device.
The starting folder for sqlLite is /storage/emulated/0 and have a sub folder /DCIM
So I try to use a folder I can see ... like /storage/emulated/0/DCIM/ormdemo.db3 but then the new SQLiteConnection(dbPath) give me this error.
SQLite.SQLiteException: Could not open database file: /storage/emulated/0/DCIM/ormdemo.db3 (CannotOpen)
Do I need special permision to write in /DCIM folder?
Ok, there are three questions in your post. Before answer your questions, I think some pictures are necessary.
P1:
P2:
Like #Yogesh Paliyal has said, you need File Explorer to see it. If you also want to see it and you don't want to root you device, please follow me: Visual Studio->Tools->Android->Android Device Monitor->select one simulator from Devices column->File Explorer. I suggest you create a simulator which Api is below 21, because the higher also can't see the file. I am using 19 to see the files.
And I think you should read this firstly.
Ok, let's see your questions:
Where is the app Personal Folder path?
In android, there are two folder: personal/internal folder and public/external folder, here is official document.
Personal/Internal Folder path:
/data/data/package name/shared_prefs: SharedPreferences
/data/data/package name/databases: Sqlite
/data/data/package name/files: Api:getFilesDir()
/data/data/package name/cache: Api:getCacheDir()
/storage/sdcard/Android/data/package name/files: Api:getExternalFilesDir()
/storage/sdcard/Android/data/package name/cache: Api:getExternalCacheDir()
Public/External Folder path:
In /storage/sdcard folder, only Android folder is personal/internal folder,so /storage/sdcard/DCIM, /storage/sdcard/Music,etc, all of them are public/external folder: Api:Environment.getExternalStoragePublicDirectory(String type)
Where is that folder /data/user?
From P1, you can see the ->/data/data/ is behind of /data/user/0, so the folder /data/user/0/MyFirstGPSApp.MyFirstGPSApp/files/ormdemo.db3 is /data/data/MyFirstGPSApp.MyFirstGPSApp/files/ormdemo.db3 actually.
Do I need special permision to write in /DCIM folder?
Personal/Internal folder doesn't need permission, public/external folder need permission.
In /storage/sdcard folder, only Android folder is personal/internal folder, so you need permission:<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> or <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> or both.
You can't see directly from mobile without root permissions.
Try using Android Studio's File explorer feature.
I'm trying to use expansion files and I found an issue that I'm not able to resolve.
It seems that you can access the /Android/obb folder and eventually delete the /Android/obb/my.package.name directory (tried with some file manager), but I cannot handle this situation whithin the app.
If I just let the Google Downloader library try to download the expansion file it will hang and error (for the missing folder), but it will start if I recreate the folder.
The strange thing is that I'm able to create the folder from other apps (the same file manager that I've used to see the directory) but not from mine!
I tried with
File f = new File(Environment.getExternalStorageDirectory(), "Android/obb/my.package.name");
f.mkdirs();
but it doesn't work.
I've the permissions in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and I'm targeting api 23 and compiling with the 25.
OP dropped the Target SDK to 22 and was able to avoid Dangerous permission work flow which was causing the issue.
I had this same problem, but lowering the Target SDK was not an option.
The Context::getObbDir() method allows access to the expansion folder without the usual security rules. I found that, if the folder doesn't exist, getObbDir() creates it too (You can also double check and create it manually with mkdir()).
Excerpt from the documentation linked above:
Return the primary shared/external storage directory where this application's OBB files (if there are any) can be found. Note if the application does not have any OBB files, this directory may not exist.
This is like getFilesDir() in that these files will be deleted when the application is uninstalled, however there are some important differences:
... Starting in Build.VERSION_CODES.KITKAT, no permissions are required to read or write to the path that this method returns. ...
Starting from Build.VERSION_CODES.N, Manifest.permission.READ_EXTERNAL_STORAGE permission is not required, so don’t ask for this permission at runtime. ...
So you can do something like:
File obbDir = getObbDir();
if (null == obbDir) {
// Storage is not available
} else if (!obbDir.exists() && !obbDir.mkdir()) {
// Failed to create directory. Shouldn't happen but you never know.
}
NOTE: You may need the read/write permissions to access the expansion files within the folder.
Currently
I'm storing my file(images/videos) like this:
File directoryToStore;
directoryToStore = getBaseContext().getExternalFilesDir("MyImages");
This will return this path:
/storage/emulated/0/Android/data/pacageName/files/MyImages/
Now, I want to store the files in root directory /storage/emulated/0/MyImages/. I have tried this by doing:
File directoryToStore;
directoryToStore = new File(Environment.getExternalStorageDirectory(), "MyImages");
This works perfectly fine when running on pre-Marshmallow devices, but In Marshmallow the files are not found.
My Question
How should/can I store files in the root directory so that the file will be found in all API's?
Firstly, make sure you have the permissions bellow in your Android Manifest file, because you are trying to save files into the device's external storage.
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
Secondly, check if storage permissions are granted to your application (when you execute on Marshmallow or higher OS version devices).
Finally, make sure if there is directory named "MyImages" and check it's chmod. If directory does not exist, create it (mkdir() will create the directory in your example) and then try to save your files again.
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.
I am creating a folder through my app
File direct_playlist = new File(Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + PLAYLIST_PATH);
if(!direct_playlist.exists())
{
if(direct_playlist.mkdir()); //directory is created;
}
Even though the command executes perfectly I can not see the folder in Explorer. I have also given permission to my app in the manifest.xml
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
When I try to access this folder my app crashes as the folder is not present.
This topic is already discussed So Use Following threads..
How to check if newly created folder is present into SD Card in Android
Creating a directory in /sdcard fails
It will surely help you.