Packaging "hidden" asset files
Asked Answered
M

2

6

I'm placing the file '.nomedia' into a folder in order to avoid Android's MediaScanner from detecting the media files in the folder. I need to copy this folder (including '.nomedia') from the APK's assets to the SD card (so other apps can make use of these media files, etc.). When I package the APK in Eclipse, it doesn't package the '.nomedia' file. Presumably it's detecting it as a hidden file. Any ideas how to fix this? Is there a secret aapt flag I can use? I'd like to avoid copying the folder and then manually creating a '.nomedia' folder, if possible.

Michaeu answered 9/6, 2010 at 14:41 Comment(2)
Why is it a problem to copy the folder and then create the .nomedia file?Fatigue
Simply reduces the generality of the code. If I want to add more folders containing media in the future, I'd prefer not to have to continually change the code each time. It would be much better to simply be able to drop a .nomedia file in those folders I want protected.Michaeu
D
8

I know this is several years after the question was asked, but I ran into this question searching for the same issue myself, and discovered the solution that worked for me so I thought I would post it:

In your "ant.properties" file for your project (create a new one if you don't have it) add these two lines:

# DO NOT ignore the .nomedia file in the assets directory!
aapt.ignore.assets="!.svn:!.git:<dir>_*:!CVS:!thumbs.db:!picasa.ini:!*.scc:*~"

This will remove the ".*" from the default exclude list, and thus .nomedia will now be included in your assets directory inside your .APK.

Dearing answered 20/7, 2012 at 18:53 Comment(3)
any idea hot to achieve it with Gradle?Campo
I'm also trying to do this with gradle and I'm not sure how. I'm considering just zipping the folder I want to include and unzipping at startup.Fictionist
You can do this with Gradle. See this post. Although now that aapt2 is out, it seems there is no way to disable this. If you look at the source code for the compile command in aapt2, there is a built in check for hidden files (search for "IsHidden").Ophthalmoscope
S
1

Assets get compiled into the application, you can't see them through file browser. You'd have to access this folder from code and then copy it to the file system when the app is first launched or installed. Take a look at this example, it talks about a database file, but in general you want to do the same thing for ANY file you put in assets and want to move to the file system:

http://www.helloandroid.com/tutorials/how-have-default-database

Sextuplet answered 9/6, 2010 at 17:58 Comment(1)
I've been successful at copying files in general from the assets folder to SD. What I have had trouble with was particularly the '.nomedia' file which inexplicably seems to be absent from the APK. It seems that at compile time, .nomedia is simply not being included in the final APK while all the other files are indeed included.Michaeu

© 2022 - 2024 — McMap. All rights reserved.