I want to create some pre-created files when my Android application is installed.
I would like to create the file in both the internal memory (data/data//files/) and in a newly created sdcard directories (/sdcard//data1/).
How can I do this?
If you have a larger number of files and a directory structure you should use /assets. These are not given any R-constants and can be scanned by your application
To open an asset-file:
InputStream is = getAssets().open("path/file.ext");
To list a directory:
String[] files = getAssets().list("");
You can save you files in \res\raw and write the code to store this files to the desired locations if it does not exist when the app start.
Check this to access the internal memory and sdcard
and access the raw file using the following
InputStream databaseInputStream = getResources().openRawResource(R.raw.yourfile);
It is worth mentioning that really large amounts of data can be dealt with APK Expansion Files.
In 2016, Google Play currently requires that your APK file be no more than 100MB. For most applications, this is plenty of space.
The APK size limit increases from time to time, so it's meaningful to check the current digit.
Related
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.
I need to store a large number of audio files in my app (around 350/400 files ). Each file has an average size of 2MB. I really don't know where to store them. Should i use SQLite database ? external storage ? something else ?
There will be no update on these files, i just want to store them and then play them. Currently i have like 15 files in the res/raw folder and it's really slow at launching.
My question is similar to this one: How to store large number of Audio Files in Android? but i didn't get the answer.
Thanks
You need store in another place like the SD card. Google Play currently requires that your APK file be no more than 50MB. You need 800MB for my math.
Check this link:
http://developer.android.com/google/play/expansion-files.html
You should store them in the External storage directory, unless this app is for your personal phone, and you know you have enough space.
The reason is many phones produced in the past few years have virtually no internal storage space (e.g. my nexus S has 1g internal, 15g external).
As others have said, you would have to use an Expansion File to distribute via Google Play, and this is by default downloaded to an External Directory. You cannot move, delete, or rename these files. Thus, I don't think its possible to store them internally (unless you make a redundant copy).
If the files are zip, you can access the media in these expansion files directly (and that is the Android's recommended approach).
If you do unpack and copy the media, you should probably store the data as seperate files. There's a number of database concerns to look into if you store large blob data (e.g. gigabytes) in SQlite. You can use a database to store any metadata for your music, by associating it with the file name/location. But if you just want to just store music, the file system is enough.
If you want your music to be accessible by other apps, use the built in Android directory for music (code below), o/w store it under android/data/com.name.package.
public File getMusicStorageDir(String albumName) {
// Get the directory for the user's public pictures directory.
File file = new File(Environment.getExternalStoragePublicDirectory(
Environment.DIRECTORY_MUSIC), albumName);
if (!file.mkdirs()) {
Log.e(TAG, "Directory not created");
}
return file;
}
IMHO you MUST use sqlite DB, there aren't many other options.. Maybe you can think about cloud storage and streaming for play. But this will going to complicate stuffs. Try out the sql lite solution, here [ http://developer.android.com/reference/android/database/sqlite/SQLiteDatabase.html ] you can implement it in a couple of minutes. Cheers!
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.
I am developing an app which is heavy on images, videos and other resources. What would be the proper place to store all this info? I can see that programmatically the raw folder is probably the easiest to handle, but what is the correct policy to this?
Assets is really bad place for items that are already compressed - when you build your app those will be tried to be compressed and this will have performance impact. Also in decompression.
Res / raw is a good way to go if you have the images in advance, especially if you do not wan to download them after launching the app, thus having the user to wait.
Sdcard should give you the most space. However, you will either need to download the resources from the net, I can not think of a way you ship them with the application and them move them to the sdcard. When you download the resources from the internet make sure you store them in a subfolder of:
private File getResourceFilePath(String ralativePath, Context context) {
if (Environment.MEDIA_MOUNTED.equals(Environment.getExternalStorageState())) {
return new File(context.getExternalFilesDir(null), ralativePath);
} else {
return new File(context.getFilesDir(), ralativePath);
}
}
Otherwise you risk the resources being available in the Gallery of the user's device etc.
Keep in mind that if you store it in the res or raw folder it will be included in your apk file which will make your compiling and uploading to the play store longer and also make user downloads and updates from the play store take longer.
If the media is pretty static and over a few megs, make it a zip file that gets downloaded on the side and stored in a separate folder, that the user can pick. That way you can update the media separate from updating the application.
If you go down the zip file route, you should check out the APK expansion file support that google play has. http://developer.android.com/google/play/expansion-files.html
Note that if you use the expansion, other stores (Amazon, Nook, etc.) will still need the download a zip file support in your app.
-James
My project have multiple files in SD card. But when I install apk file on phone it does not work.
Please tell me how to attach those files with apk.
Your sdcard files cannot become the part of APK file, if you want those files to attach to your apk, insert them into asset folder or raw folder.
EDIT:
If you put your file in raw directory then:
com.your.package:raw/yourFile
Like this:
int resourceId = context.getResources().getIdentifier("com.your.package:raw/somefile.txt");
File f = new File(context.getResources().openRawResource(resourceId));
And here's someone doing it with the assets folder:
Android Assets with sub folders
InputStream is = getAssets().open("subfolder/somefile.txt");
Create a sqlite database and store the path of whatever files you want to store on the sdcard. Also you can store them in assets folder depending upon the type of file but it is not a good practice as many say.
You could store the supporting files on a web server and have the application download the files to the sdcard on first run. This is what many games do. Google Play also offers free storage of files for applications.
Here is the Google blog post on large APK's
http://android-developers.blogspot.fr/2012/03/android-apps-break-50mb-barrier.html