Android Studio Get Application Directory dataDir - android

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

Related

After install app, Internal storage (Android>data) package folder not created

When I fresh install app, I notice most of the app will have a folder created in Internal Storage > Android > Data > com.example.package folder. For example its facebook app it will be something like com.facebook.xxxxx.
However, one of the project I involve recently, I notice there is no such app folder in the path Internal storage > Android > Data > (No packege folder).
In what scenario it won't create an app folder in the above path ? Because most of the app I debug run via android studio or install via apk file...Always have one app folder in the above location by default.
Because I want to write below file into getFilesDir(),
File file = new File(activity.getFilesDir(),imageFileName);
By using getFilesDir(), written file will go into Android > Data > Package folder > Files folder, most of the installed app have a package folder path in Android > Data > (Here). Today I realise one of the project I am doing now, it doesn't create such package folder. So, when I use getFilesDir() , those files will still be stored and created inside the mentioned path but it is invisible? Or Those files I create using getFilesDir() won't be created? Because as I mentioned this app doesn't create a package folder. Do I have to use makedir ? If yes what will be the checking ? if(!activity.getFilesDir(). exist())
File file = new File(activity.getFilesDir(),imageFileName);
//getFilesDir() normally is package folder Android > Data > (package folder*) > Files
I have tested and find out that if phone is having internal storage only (Not support SDCard) or maybe phone support external storage but don't have SDCard insert. App folder in this path by default won't be created (Android > Data > [App folder]), until I called (getExternalFilesDir(null) , "Example1") check its exists() and makedirs() if it doesn't exist, finally the app folder is created with a folder Example1 in this path Android > App > com.xxxxx.myapp > files > Example1.
However, why some sample app I create I never call getExternalFilesDir(null) or any File writing storage path but by default I can find my sample app path in Android > App > [Here].
Some app folder won't be created by default but until getExternalFilesDir(null) check exist and mkdirs then it become exist and created. Some app folder will be created when app is installed by default. How Android determine if the Android > App > App folder should be created and visible or created and make it invisible??
It makes me confuse. Some app will create app folder, some app wont create it by default when app is installed. Let's assume both app never call any File path create method. Just a very sample app I run using latest Android Studio to test something, it created package folder. The big project app is using old gradle settings and almost 5 years ago, the app by default won't create package folder until I call File(getExternalFilesDir(null), Example1) check if it exists and it doesn't then mkDirs(), package folder become created and visible.
How SY MY told you need to check if phone is having internal storage only (Not support SDCard) or maybe phone support external storage but don't have SDCard insert. App folder in this path by default won't be created. And how he checked you shoud make if statement. So... you can paste this code in your project:
File file = new File(getExternalFilesDir(null),fileName);
if (!file.exists()&&file.mkdirs()){
File file1 = new File(getFilesDir(), fileName);
}
fileName it is a String varable with txt foramt:
private String fileName = "list.txt";

Kivy on Android : How to keep local saved file after app update?

I want to know the correct steps to keep local saved data after app update.
I confirmed the following old question, but I can't solve the issue.
Save app data in kivy on Android
I tried the following.
1-1:I made android APK with buildozer(command:buildozer android debug). 1-2:And execute 'store.put' method using 'kivy.storage'. then 'hello.json' file was created in the './' directory.
https://kivy.org/doc/stable/api-kivy.storage.html
1-1:'./' directory in APK ver.1
main.pyo
1-2:'./' directory in APK ver.1
main.pyo, hello.json(created)
2-1:I made android APK ver.2(same app name as 1.APK). 2-2:After installing in update mode, execute only 'store.get' method using 'kivy.storage'. then method failed because 'hello.json' file was automatically deleted with app update.
2-1:'./' directory in APK ver.2
main.pyo(changed from APK ver.1)
2-2:'./' directory in updated APK
main.pyo(ver.2)
I tried changing the output location of the 'hello.json' file but it made no sense.
e.g. './data/data/[app_name]/files/hello.json' and './data/data/[app_name]/shared_prefs/hello.json'
How can I update my application?
Everything in the default current directory, which is named app, is deleted and replaced on app update. You can place persistent data in the directory above this, i.e. ../, and it should stick around between updates.
You can also use your app's external storage directory, using pyjnius to query the Android API for its location, but I don't have code for that right now.
This isn't especially well documented, I'll try to improve it.

How do I read a text file created by an installed app from my app?

I've created an app which reads a text file created by an installed app. The location of this file is located at: This PC\Galaxy S4\Phone\veryfit2.2\file.txt.
I tried to get access to this file using Environment and getBaseContext(), but in both cases, it didn't help. How can I access the text file?
Here's the folder directory:

Can a Cordova Android app write in its native source folder?

I would like to know if an Android app using Cordova can write in the folder that it is installed in. Basically, I need this to update the app without having to go through the app store.
In iOS, ressources files are copied in a sandboxed environment and I cannot write to it. What about in Android?
Yes ,cordova can write into the app folder or in anyfile which it Android can access , if you want to write in private other folder that wont be possible .
For cordova you need to just add a plugin for accessing the storage or create a custom plugin for the same.
Below is code for writing into your App Folder :
if (!Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){
Log.d("Log", "No sdcard available");
} else {
File directory = new File(Environment.getExternalStorageDirectory()+File.separator+"ApplicationFolderName");
directory.mkdirs();
}
Make sure you add permission into manifest file also.

openalpr on android - path to config and runtime_data

I want to use open alpr (automatic licences plate recognition) library in my android project. I compiled everything successfully and now it is time to use open alpr in app but...
to create Alpr class object properly I have to provide path to config file and path to runtime_data folder which contains some mandatory files needed by open alpr (ocr and trained data).
I tried something like:
Alpr alpr = new Alpr("eu", "android_assets/alpr.conf", "android_assets/runtime_data");
but Alpr.isLoaded() returns false which means that config or runtime_data have not been found.
Path to assets folder in project is: src/main/assets.
Can someone explain to me how path to "runtime_data" directory and "alpr.conf"
should looks to be visible by open alpr?
Thanks in advance.
I am not familiar with the specific library, but on newer Android devices (Android 6 and up), you can not rely on your application files residing under /data/data/your.package.name
The actual library name still includes the package name of your app, but also has some identifier appended to it in base64 format.
This identifier is unique per installation, and it will change if you uninstall and reinstall the app on the same device.
So, if your library needs to use a configuration file with a path to some other files, there are 2 options:
The right way:
Get the real address of your application files folder using Context.getFilesDir().
Unpack you files from the assets folder of the APK on the device using AssetManager.
Programmatically rewrite your configuration file with the path returned by getFilesDir().
The "hacky" but simpler way:
Use public storage to unpack your files.
You will need to add WRITE_EXTERNAL_STORAGE permission to your app, and unpack the assets files to the external storage.
For backwards compatibility this will be available under /sdcard folder on most Android devices, even with the latest Android version.
The second method is not recommended since using /sdcard directly is deprecated and strongly discouraged by Google.
Also, not all Android devices have /sdcard link to their public storage, but this is the only way to avoid dynamically editing the configuration file after installation.
Important note before you start implementing those steps. This library supports only arm CPU architecture. Good news is, most probably, your physical device is using arm architecture but to make sure just double-check it before implemting those steps.
I've recompiled this library to a new wrapper library. In original library, you need to manually configure openalpr.conf file and edit its content with correct path to your data directory. Manual configuration is cumbersome because since Android 5 multiple user accounts is supported and we can't simply hardcode data directory as /data/data/com.your.packagename/..... Because every user gets their symlink to data directory as /data/user/0/com.your.packagename/..... All those manual steps are gone in recompiled wrapper library.
Implementation
Add this in your root build.gradle at the end of repositories:
allprojects {
repositories {
...
maven { url 'https://jitpack.io' }
}
}
Add the dependency into app module:
dependencies {
...
implementation 'com.github.mecoFarid:openalpr:1.0.0'
}
And you're done. Please check this sample app to get started with UI.
Troubleshooting:
If your target sdk is targetSdkVersion >= 24 and you're running your app on a device with Android API 24+ you'll get following error:
android.os.FileUriExposedException: file:///storage/emulated/0/OpenALPR/2019-09-21-01-32-13.jpg exposed beyond app through ClipData.Item.getUri()
To solve this error: you can add following lines into onCreate() of your Activity as a workaround or you may use this thread for offical solution:
if(Build.VERSION.SDK_INT>=24){
try{
Method m = StrictMode.class.getMethod("disableDeathOnFileUriExposure");
m.invoke(null);
}catch(Exception e){
e.printStackTrace();
}
}
TEST:
You can use this image to test your app.
"/data/data/yourpackagename" + File.separatorChar + "runtime_data"
+ File.separatorChar + "openalpr.conf";

Categories

Resources