Assets on storage card - android

I have an application that stores a lot of data in the assets folder (html, audio, video, etc.). I use the file:///android_assets to access these files. But due to the apk size limitation of 50 MB imposed by the market I will be migrating these files to a storage card.
Is it possible to install the application on phone memory and have the assets directory on the storage card, so that they can still be accessed using file protocol?

Since assets are part of your apk, it's not possible to separate them or move them to the sdcard. You can enable "Move to SD Card" for your app, but that doesn't help with the 50 MB market limit.
One solution would be to store the data files on the web and not include them in the apk. When your app is launched for the first time after installation, you would kick off a one-time process that downloads them to the sdcard.
Once the files are on the sdcard, you can access them with:
file:///sdcard/<your_directory>
Or to get a File reference to the sdcard root directory:
File sdCard = Environment.getExternalStorageDirectory();

Related

How do apps like BattlegroundMobile access to obb without storage permissions?

I've done developing obb function on my game app. To explain the logic of obb function simply,
makes a zip file with assets and rename as .obb.
uploads it to play store with apk. then it automatically makes an obb file
with build version code.
extracts obb file to app data folder with java.util.zip and mount to the
assets.
but if do extracting assets, it should require external storage permissions.
and it makes 10%~15% loss of customers.
Also lots of posts I've been looking for are saying permission should be needed.
but some apps like battlegroundmobile don't request access permission although it uses obb function.
So, I want to know how the apps avoid permission requirement programmatically.
extracts obb file to app data folder with java.util.zip and mount to
the assets.
Maybe that's where your problem is.
Battleground Mobile is not copying or extracting the obb file to Internal Storage. It is accessing the data stream in the file inside the OBB folder. If you just use the file in the OBB folder, you don't need to ask permission for it.
The download size is 1.53 GB. The obb file is in under Internal Storage - Android - obb - com.tencent.ig folder. Its size is 1.53 GB. So the APK size is pretty small.
It downloads an extra 195 MB update data after it starts up.
The downloaded files are stored in
/storage/emulated/0/Android/data/com.tencent.ig
Internal Storage - Android - data
which you can access via getExternalFilesDir.
Again, there is no need to ask permission to download into or read that folder.
The total app storage is 1.85GB.
App: 1.65 GB
Data: 202 MB
Cache: 10.59 MB
If you clear data, it will remove all the updates. But the content in the OBB folder remains.

Does assets/ folder reside in device's RAM?

When I launch the app, where do the assets/ folder contents go?
Are they loaded into RAM?
Or maybe AssetsManager always reads from inside /sdcard/?
Am I safe to assume that any Android device handles assets/ folder in the same manner?
My concern comes from the fact that some devices that I target are very limited in the amount of RAM they can provide, while all of them have a decent /sdcard/ storage, therefore I must decide if the assets should be downloaded at runtime or if they can safely be included into the apk file.
Assets are not saved in the "external files" directory (a.k.a the SD card). They're saved in the main app storage.
You can encourage your app to be installed on the SD card via android:installLocation="preferExternal". Your app will use this less valuable storage for everything, not just assets.
To have just assets on external storage consider expansion files like #pskink mentioned in a comment, or downloading them yourself and caching them in external storage.
I have successfully launched a 587 MB application on a device that comes with 512 MB available RAM, so it turns out that at least some of the devices indeed avoid pulling the whole assets/ folder to RAM upon launching the application.

Does Expansion Files stores all files into the res/drawable folder in my phone?

I am new to android expansion files. I am not sure how it works or what to do (step-by-step build) an expansion file. As far I been reading, if I am not wrong please correct me; expansion files can be files that are stored in the Asset folder or Res folder. What I don't understand is if my app is depended on files that are stored in both folders and I take them out wouldn't that cause an error build? How does expansion really work? Furthermore, If I successfully create an expansion file from res folder (example) would the expansion file store it in the res folder in the phone? or I have to change all my code that makes reference to the res/drawable folder to the appropriate location? If so, then what is the correct path that I need to change my code into?
My app is so far like 600mb and continue to grow. Within my app, the core dependance is a database stored locally in the asset folder and some images in the res\drawable folder. There are some images that are not dependance on the app directly which makes the remaining 550mb. Nevertheless, those images are needed because the database makes reference to those images (just the name) then I populate an imageview with reference from the database. I was thinking to store all those images into an expansion file but I was not sure if those images will be store within the res/drawable folder in my phone or it will be store in a different folder? if so, would that cause an error?
The expansion files are not supposed to save in your res folder. Res folder is part of your APK, so if you save your extra images in the res folder, your APK will still be over the 50MB limit.
From the Google Developer page, it states that "The expansion files are saved to the device's shared storage location (the SD card or USB-mountable partition; also known as the "external" storage) where your app can access them. On most devices, Google Play downloads the expansion file(s) at the same time it downloads the APK, so your application has everything it needs when the user opens it for the first time. In some cases, however, your application must download the files from Google Play when your application starts."
So, your expansion files should be saved in SD card. You can zip your images into a zip file, stored zip in your SD card, and read them while your application start.
You can use the APK expansion Zip Library and read the file from the zip. Sample code from Google Developer page:
// Get a ZipResourceFile representing a merger of both the main and patch files
ZipResourceFile expansionFile =
APKExpansionSupport.getAPKExpansionZipFile(appContext,
mainVersion, patchVersion);
// Get an input stream for a known file inside the expansion file ZIPs
InputStream fileStream = expansionFile.getInputStream(pathToFileInsideZip);
You can read this documentation for more information about APK Expansion file.
You can also find some tips from the Android developer blog about the APK Expansion file.
Or you can visit this APK Expansion file tutorial.

Where are files saved in PhoneGap app stored on Android?

I am using PhoneGap with File plugin. I am able to read & write files to file system and those are persisted correctly. But when I try to locate those files from native file explorer app on tablet or from Windows Explorer (through USB connection), I can't find any traces of these files by browsing to folder that PhoneGap claims to store those (Tablet\Android\data\com.xxx.yyy\files) to nor with search functionality. I do not have any external cards attached to my Android tablet. Any ideas how to find those files?
After carefully studying Android documentation about storage options I spotted that this is the intended behavior of internal storage which is used for applications private data that shouldn't be possible to read by other applications accessing the file system.
I have all the time thought that the external referred to SD cards but instead the external storage is described like this
Every Android-compatible device supports a shared "external storage"
that you can use to save files. This can be a removable storage media
(such as an SD card) or an internal (non-removable) storage. Files
saved to the external storage are world-readable and can be modified
by the user when they enable USB mass storage to transfer files on a
computer.
which seems to be the exact behavior I was looking for. By using the cordova.file.externalDataDirectory, I was able to store the files on internal file system (not to SD card) and see them in the file explorer.
I think that the assumption of external storage referring to SD card only isn't totally unreasonable since the File plugin has / as a directory for cordova.file.externalRootDirectory in file system layout for Android. This must have been the thing that made me think that way.

Android folder to store media files and access them via an absolute path

I'm working on an Android application that needs to store media (document files like pdf or so for later reading) but the main requirement is that all media files have to be accesible through an absolute path (a physical path on filesystem).
I'd like to avoid copying files to external storage (like sdcard or phone internal memory) so to prevent that if application is uninstalled those files remain in phone (and of course to avoid duplicating the size in kb for each file) and instead to keep files in iny App internal resources folder, but tried "file:///asset_folder" whith no success. As far as I know "file:///asset_folder" only Works for a webview to Access www folder but not for regular files.
I'm not sure if there is any app internal data folder which I can access through an absolute path or if not which is the best way to store App resource files.
Thanks in advance!!
Edit: To make it more clear, the resource files are already bundled with the App, and not written during runtime, and what I'd like to know is where to put them so I can later Access them via absolute path for Reading.
To retrive your app specific data
openFileInput(file_name)
To save your app specific data
openFileOutput(file_name, Activity.MODE_PRIVATE)
Update : Read from asset folder. (InputStream)
getAssets().open(fileName);
getAssets().open(fileName, accessMode);
check the image to where to put asset files
now to make other app readable your files from private data/data directory use content providers.
You can not write data inside asset_folder because it is packed on apk file. You can use sd card or location where your app is installed inside internal memory.

Categories

Resources