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.
Related
Target SDK in my project is 31 and when I'm trying to delete a file from DCIM directory, I'm getting following error such as delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER.
Please consider file path like this: "/storage/emulated/0/DCIM/Screenshots/pic1.jpg"
I have tried so much to find the result every where including stack overflow, but didn't succeeded yet. Even I changed target SDK to lower 30 but not worked.
Following are the solutions that I had already worked on but nothing works:
1.flags in manifest file
android:requestLegacyExternalStorage="true"
android:preserveLegacyExternalStorage="true"
2.permissions in manifest file
<uses-permission
android:name="android.permission.WRITE_EXTERNAL_STORAGE"
android:maxSdkVersion="40"
tools:replace="android:maxSdkVersion" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission
android:name="android.permission.MANAGE_EXTERNAL_STORAGE"
tools:ignore="ScopedStorage" />
Please find the attached code
val fileToDelete = File("/storage/emulated/0/DCIM/Screenshots/pic1.jpg")
fileToDelete.delete()
NOTE: filepath is hardcoded only to explain better
Please find the attached log
2022-03-21 11:22:35.331 8639-20226/com.filepickerdemo D/ContentResolver: delete denied by permission:android.permission.ACCESS_MEDIA_PROVIDER#content://media/external/images/media#_data = ?#/storage/emulated/0/DCIM/Screenshots/pic1.jpg
What else should I put in my code to solve this issue. Thanks in advance.
To delete media files in android 10 and above, You can try contentresolver API.
You need to pass the media's content uri to the 'contentResolver.delete()' method & you will be able to do it easily.
Here is my post on how to do that using Java - Scoped Storage in Android — Writing & Deleting Media Files
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 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
I am having a problem copying files (.so binaries) from my assets directory to the app installation "/data/data/name.ofmy.app/lib" directory.
When I try to I get the following error:
07-09 11:00:48.902: W/System.err(25122): java.io.FileNotFoundException: /data/data/name.ofmy.app/lib/libOlsrdPudWireFormat.so: open failed: EACCES (Permission denied)
This happens when the following code is being ececuted:
if ((so = new File(dir, fileName)).exists()) {
/*
* Check to see if this timestamp pre-dates the current package
*/
if ((pkg = new File(ctx.getPackageResourcePath())).exists()) {
if (pkg.lastModified() < so.lastModified()) {
Log.v(TAG, "extractFile: File up to date");
return;
}
}
Log.v(TAG, "extractFile: library present but out of date");
so.delete();
}
Log.v(TAG, "extractFile: copying library");
InputStream in = ctx.getAssets().open(name);
FileOutputStream out = new FileOutputStream(so);
It breaks right at InputStream in = ctx.getAssets().open(name); when there is not an already existing file with that name...
I have been reading similar questions here on Stack Overflow, but unfortunately that did not help. Most are copying to the SD card, but I am not.
Also the strange part is that a previous directory (/bin instead of /lib), which is also supplied within my assets, does get copied without any problem in the same /dat/data... directory!
I tried adding the <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" /> permission to no avail.
I even tried adding Runtime.getRuntime().exec("su"); just before I try to copy anything, also to no avail.
It's a rooted phone BTW!
I also double checked if the file is really there in the assets, and it is!
Just for the sake of trying I changed the directory name from lib to libs (because as mentioned in my question, I also copied another directory named /bin without any issues) and guess what: now it works!
It seems you cannot use the /lib directoryname whilst copying. I have no idea why, though.
Definitely you forgot to put in the manifest:
<manifest
.
.
</application>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
</manifest>
How can I delete a file from an Android application? Can I do it the same way I would for deleting a file in Java?
File file = new File(selectedFilePath);
boolean deleted = file.delete();
and set this permission
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
in AndroidManifest.xml file
The Android Developer Homepage has a really good Dev Guide section.
Answer to your question:
Files can be deleted using: File.delete() method. (I found that by searching for "file delete" on the page above!)
But of course, there's much more to that: you need to understand how Android stores files and which files your application is allowed to modify! (basically only its own files, all the others are not accessible)
File file = new File(path);
return file.delete();