Access Assests from another application?
To get the base /assets
folder you need to use the AssetsManager to list the directory with just quotes:
AssetManager am = this.getAssets();
String[] names = am.list("");
There will be some additional files listed: images, sounds, webkit, maybe others. You can ignore these directories, they are part of the frameworks assets directory. This is a quote from groups.google.com:
Currently the asset manager merges the
asset directory from the framework
resources along with your own files
placed in "assets". We should
probably change this behavior (it was
part of an old resource/ localization
model), but it doesn't do much damage
except that you see more
files/directories in your own assets
than you might expect. Any of your
files that are the same as one in the
framework assets will be used instead,
when accessed through your
AssetManager.
You can also list a subfolder inside the assets directory and do not need any slashes:
String[] names= am.list("subfolder");
Note that I did not include "/assets"
in the filename.
Finally once you have your list of files you can load them in like:
InputStream in = am.open("file.png");
That will load in a file in the base assets folder. Or you can load a file in a subfolder like this:
InputStream in = am.open("subfolder/file.png");
If you need to load those pngs into a Bitmap you can also do the following:
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
Get some asset file path in APK
This "file:///android_asset"
path is used for applications which uses webview ex. cordova/phonegap. As it is part of resources because when Apk is created you can not use this path to access your assets folder. You have to work with context.getResources().getAssets().open("fileName")
this code only.
can asset folder be accessed by the user when the apk is installed?
Anyone can open your .apk just by changing the extension to .rar, what you can do to protect your code is releasing it using proguard rules, but still in that case an image for instance in the drawable folder can be compromised, however an string in your strings.xml is bit more protected.
open a file outside of assets folder by assetManager
finally, I soloved it by using getClass().getResourceAsStream() instead of assetManager:
public byte[] loadBinAsset(String name) {
AssetManager assets = context.getResources().getAssets();
InputStream stream = null;
try {
try {
stream = assets.open(name);
} catch (IOException e) {
stream = context.getClass().getResourceAsStream("/" + name);
}
return readFully(stream);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (stream != null) {
stream.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
return null;
}
Trying to access sub folders in assets folder
SOLVED
it was really simple..
to access the sub folders in assets folder you would need the following..
1st access the file by its file name..
File file = new File(getFilesDir(), fname);
2nd while attempting to open the file use both file name and the url to the file
in = assetManager.open(url+fname);
3rd in the intent use only the file name..(i was using the entire path)
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/"+fname),
"application/pdf");
Related Topics
Onitemclicklistener Was Not Work with the Checkbox
Android - Choose File Button in Webview
Gridlayoutmanager - How to Auto Fit Columns
Disable Android Browser's Input Overlays
CSS Media Queries on Nexus 7, Display Resolution Not Working in Code
How to Paging Query from Firebase Using Android Firebaseui
Getting the Pixel Color Value of a Point on an Android View That Includes a Bitmap-Backed Canvas
Android Animate Drop Down/Up View Proper
Generating Edit Text Programmatically in Android
Create Free/Paid Versions of Application from Same Code
Eclipse Can't Find Android.Support.V4.Widget.Swiperefreshlayout
Android: HTML in Textview with Link Clickable
Bypass Android's Hidden API Restrictions