I should start by giving some background. We're about to release a non profit html5 children's game for android platforms. I have used eclipse to create an apk file but there is no audio (the rest of the game works fine). I have tried phone gap but there is no audio for the game. After doing days of research and possible solutions, I decided to ask here on stackoverflow.
The game uses html/css/js.
Project folder structure in Eclipse is:
assets(android_assets)->www->index.html, game.xml, js Folder (the game engine), styles Folder, and assets Folder.
The sub asset folder contains -> music folder, images folder, and backgrounds folder
Error Message:
The index.html file loads the game.xml. The game.xml file loads all the files in the assets folder. However, all music files are not found in logcat:
Failed to open file '/android_asset/www/assets/music/song.mp3.' (No such file or directory). My xml file sound call is: <source href="assets/music/song.mp3" type="mp3" />.
I use this same reference structure with images and backgrounds that are currently loading fine. When loading the music from an external url like www.oursever.com/song.mp3. The game is able to play the song just fine when running as an apk file on an Android device. So the code is right but the local file path to the song in the apk file is wrong.
After much reading, I learned that the assets folder is compressed and it is not recommended to read audio files from that folder without uncompressing them. The alternative is to place the mp3 files in the res/raw folder. I change my xml to read: ''. However, I receive the same error about the file not being found. I know items place in the res folder uses identifiers. But I'm not sure what I should put in the xml source file path to reference the song in res/raw. I printed out the file path to the song by using this code: System.out.println("Raw Path: " + getString(R.raw.menu));
Oh another method I tried, was pushing the entire app to the SDCard. I still received the "cannot find file" error.
Application Permissions:
- <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
- <uses-permission android:name="android.permission.INTERNET"/>
- <uses-permission android:name="android.permission.MOUNT_UNMOUNT_FILESYSTEMS"/>
Summary
The game works fine on our local webserver with all assets. When trying to create an apk file using Eclipse and Phonegap options produces the game with no audio. I read that the assets folder has a 1 mb limit per file so that is another reason why I am attempting to use res/raw folder. The only issue that is stopping us from publishing this non profit children's game is that I cannot find where the music file is located at within the apk file. I have explored the apk file contents and it shows that song.mp3 is inside res/raw/ folder. However the Android SDK states it not. Does anyone have any ideas?
Thank you four time,
Its my first time posting here so let me know if anything is confusing.
UPDATE 1
I tried an alternate idea by trying to read the song from the SD Card, when I tried this I received a permission denied error (so it did find the file). Even though I have READ_EXTERNAL_STORAGE permissions set, my app is being denied from using the mp3 file. So I tried copying the song from the raw directory, making a new folder on the sd card, and placing the file in the new folder by using this link:
How to Copy Raw files into SD Card
When using this solution the folder is never created and the file is never copied either. I also cannot find my app after installing it to my phone. I checked the internal storage->sdcard folder and examine the com folders. I also checked my external storage->sdcard for the com.mycompany folder and its not their either. Our test phone is running Android 4.1.2.
Update 2
I tried the suggestion for the sample project ''. I receive the error Failed to open file 'android.resource://com.mycompany/raw/song.mp3'. (No such file or directory). Song.mp3 is located in the res/raw folder. I tried an alternate idea by trying to read the song from the SD Card, when I tried this I received a permission denied error (so it did find the file). Even though I have READ_EXTERNAL_STORAGE permissions set.
Update 3
I'm working on a blank project again from scratch. The tutorial I used for the previous project is: https://www.youtube.com/watch?v=QRa4yMjoI7c
I'll update this post next Monday, thank you Andrew and anyone else that at least viewed this post.
Update 4
I have made a sample project that I have uploaded here:
http://tinyurl.com/p7s5w2v
Go to file then download.
The project is extremely small but you can see that I put the menu.mp3 file in the raw/res directory and that 'android.resource://com.mycompany/raw/menu.mp3' is unable to find the song when called in 'fisher.xml'. The song plays correctly on our webserver and our local server. But menu.mp3 cannot be found when running on an android device or emulator. I can always tell the xml file to load all the audio files for the game from our webserver but this will be slow.
Update 5
Current Tries:
source href="android.resource://com.brg.example/raw/menu" file not found
source href="sdcard/menu.mp3" permission denied on sdcard (app has read access)
source href="storage/sdcard0/menu.mp3" permission denied
source href="file:///assets/music/menu.mp3" file not found
Manifest File:
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.brg.example"
android:versionCode="1"
android:versionName="1.0" >
<uses-sdk
android:minSdkVersion="16"
android:targetSdkVersion="19" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="#drawable/ic_launcher"
android:label="#string/app_name"
android:theme="#style/AppTheme" >
<activity
android:name="com.brg.example.MainActivity"
android:label="#string/app_name" >
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
Update 6
I have decided to settle on loading the media files from a remote server using a url. I initially thought converting a html project to an android webview with audio would be less time consuming than other platforms. I was mistaken with this. We may revisit Android again in the future. PhoneGap support team has been basically silent after we paid for their service. I thanked everyone that decided to glance at this posting.
Try this: "android.resource://com.your.package/raw/filename"
I've just had a similar problem -- for me the key part was omitting the extension of the media file: android.resource://com.mycompany/raw/song
Related
In my android project I have created an assets folder in which I have added some json files and I want to read them with a stringBuffer. The assets folder is listed inside src/main. So far I have added the following code:
String[] files = assetManager.list("");
ArrayList<String> it = new ArrayList<>(Arrays.asList(files));
And so far I am getting the names of the files successfully inside my ArrayList. Then I am trying this:
InputStream input = assetManager.open("filename.json");
But I get the following error:
Failed to open file
'/data/data/package/code_cache/.overlay/base.apk/assets/filename.json':
No such file or directory
I also have included the follwing permission in the manifest:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
Any idea what might be the problem here?
You can clear storage in device and then rebuild app, that help my same issue.
I got the same issue on my Android project many times. Try uninstalling the app on your emulator and rebuild your app.
I uploaded several files to folder "/sdcard/Books/LadySusan" on my android emulator. When I check if file in folder exist, expression
new File("/sdcard/Books/LadySusan/ladysusan_1_austen_64kb.mp3").exists()
returns true, file exists in folder, but when I use
new File("/sdcard/Books/LadySusan").listFiles()
it returns empty array, not null but File[0]. I have permissions to read files from SD card
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
and code to request them. Any idea what can be wrong?
Finally I found. It seems to me there is a bug in Android SDK (10.0+). When I started emulator with Android 9.0, it returns files as I expected.
It is not a bug. As of Android 10, external storage access is scoped to app files and media.
This means that your application can only access the files in the app-specific directory (which can be retrieved in code with getExternalFilesDir()) or media in the media store.
As long as you target an Android version lower than API level 30 (Android 11) you can get away with accessing the external storage via the following permission:
<application android:requestLegacyExternalStorage="true" ... >
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'm trying to read from a file in Unity that I've written to the Downloads folder in Android.
I'm writing the file natively with another apk and I've copied the url that I wrote to: "/storage/emulated/0/Download/file" but when I try File.Exists(thatUrl) it's returning false.
So apparently the permission: <uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" /> means something different in Unity than it does it the native Android SDK. Having that permission allows you to read from the INTERNAL storage as well as the external.
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