I'm trying to get the names of my folders in "assets". I can get the names of the files with an AssetManager by using the method assetManager.list(). But the problem is that it return only files' name and not folders' names. So I'm trying to use the listFiles() method but i can't access to the Assets directory; I've try the following :
File dir = new File ("file:///android_asset/");
File[] files= dir.listFiles();
But it doesn't work :( ... Is there a way to get the folders' names contained in the Assets directory ?
The URL "file:///android_asset/" doesn't point to a particular directory, it is only used by WebView to address assets. Assets are read-only and defined at compile time, so it's assumed their directory structure is known to the programmer. It's inconvenient, but that's the way it's designed
Related
I have a File object named file, which points to a file in any of media directories (DIRECTORY_DCIM, DIRECTORY_MUSIC, DIRECTORY_MOVIES, DIRECTORY_DOWNLOADS). When I am calling file.delete() method it fails to delete the file. However, it works if the file points to files on other directory. Any solution for this?
Issue
I wonder how to make file hierarchy of assets folder in Android from native code. I'm using AAssetManager_openDir but AAssetDir_getNextFileName doesn't return any directory names so basically I have no way to dive deeper into hierarchy because I can't obtain names of subfolders. Is there any way to resolve this issue with AAssetManager or should I implement this in Java?
Example
Assets looks like
a/
file1
file2
b/
file3
c/
file4
C++ code
AAssetDir* assetDir = AAssetManager_openDir(env, "");
// filename is NULL since there is only folders at the root folder
const char* filename = AAssetDir_getNextFileName(assetDir);
AAssetDir_close(assetDir);
AAssetDir* assetDir = AAssetManager_openDir(env, "a");
// filename is "file1"
const char* filename = AAssetDir_getNextFileName(assetDir);
// filename is "file2"
filename = AAssetDir_getNextFileName(assetDir);
AAssetDir_close(assetDir);
EDIT:
I found out that AAssetDir_getNextFileName implementation literally filters directories from output.
Link to source code
You can achieve your goal in three steps, each requires some programming. The bottom line is, your assets all live in the APK file, and your app can find and read the APK file, and its format is a ZIP archive.
Find your APK file name.
Open this file for read as zip archive.
Iterate the zip contents for assets/<whatever>.
For 1), you can use Java or if you must stay in c++, filter /proc/self/fd. You may find some system APK files (framework), but your APK will be the only one that is not system.
For 2), see use zlib to list directory structure of apk file in android. Note that zlib is part of public NDK libraries.
How do I delete a file or folder on the internal storage in android with Delphi for android
You should use the TFile and TDirectory classes in the System.IOUtils unit.
For example:
TDirectory.Delete(<YOUR DIR PATH>);
or
TFile.Delete(<YOUR FILE PATH>);
Look in Embarcadero's documentation to get the right path of your files and folders on the various platforms:
Standard RTL Path Functions across the Supported Target Platforms
Referred from :
https://stackoverflow.com/a/27047502/5193608
https://stackoverflow.com/a/3554949/5193608
Main Part from both links:
You should always delete files that you no longer need. The most straightforward way to delete a file is to have the opened file reference call delete() on itself.
myFile.delete();
If the file is saved on internal storage, you can also ask the Context to locate and delete a file by calling deleteFile():
myContext.deleteFile(fileName);
Note: When the user uninstalls your app, the Android system deletes the following: All files you saved on internal storage All files you saved on external storage using getExternalFilesDir(). However, you should manually delete all cached files created with getCacheDir() on a regular basis and also regularly delete other files you no longer need.
Source : http://developer.android.com/training/basics/data-storage/files.html
Directly you can do is :
File dir = getFilesDir();
File file = new File(dir, "my_filename");
boolean deleted = file.delete();
Hope,it helps!
I believe that every application can get access to its own files, for example - to classes.dex file.
I wonder how the application get access to its files?
and another question, can I fake this access and get access to another application.?
you can point to the apk file of your application using ApplicationInfo.publicSourceDir
File yourApk = new File(this.getApplicationInfo().publicSourceDir);
//create the destination dir
File tempDir = new File(getFilesDir(), "temp_dir");
//and then unzip the apk to that dir
unzip(yourApk , tempDir);
//now the tempDir will contain all the resources including the dex file and its accessible
this file is a zip file you can decompress that file using this link
How to unzip files programmatically in Android?
so you will have the access to all resources plus the classes.dex
hope this will help
I am trying to get an instance of an image in Unity that is in:
/assets/texture/image.png
Here is what I did:
path = Application.persistentDataPath;
path2 = Application.dataPath;
path3 = Application.streamingAssetsPath;
iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance("jar:file://" + path2 + "!/assets/texture/image.png");
I did try with all the paths, and I am getting:
NotSupportedException:
jar:file:/data/app/com.myapk!/assets/texture/image.png.
Any help?
To access a file in the assets folder of your apk:
String path = getAssets() + "/texture/image.png";
iTextSharp.text.Image imageHeader = iTextSharp.text.Image.GetInstance(path);
The path should be something like:
file:///android_asset/texture/image.png
EDIT: Can't use getAssets in Unity - Then there will not be a direct system-mounted file path for files in the assets folder.
What I usually do for C++/NDK applications with graphical assets is to decompress the assets directory to the SDCard to have a direct path access. On first launch of the app, you extract your assets folder to /mnt/sdcard (getExternalStorageDirectory) then use that. Otherwise I have no Idea how to do it in Unity directly, but there must be something.
If you want to make this easy on yourself, put your texture in the Resources/ folder and use Resources.Load to load it. Or put it in StreamingAssets/ and use the WWW class to load if from the jar file.