How to Pass a File Path Which Is in Assets Folder to File(String Path)

How to pass a file path which is in assets folder to File(String path)?

AFAIK, you can't create a File from an assets file because these are stored in the apk, that means there is no path to an assets folder.

But, you can try to create that File using a buffer and the AssetManager (it provides access to an application's raw asset files).

Try to do something like:

AssetManager am = getAssets();
InputStream inputStream = am.open("myfoldername/myfilename");
File file = createFileFromInputStream(inputStream);

private File createFileFromInputStream(InputStream inputStream) {

try{
File f = new File(my_file_name);
OutputStream outputStream = new FileOutputStream(f);
byte buffer[] = new byte[1024];
int length = 0;

while((length=inputStream.read(buffer)) > 0) {
outputStream.write(buffer,0,length);
}

outputStream.close();
inputStream.close();

return f;
}catch (IOException e) {
//Logging exception
}

return null;
}

Let me know about your progress.

Specify path for android assets subfolder

May this help you:

AssetManager aManager = appContext.getAssets();
String[] filelist = aManager.list("MyName");

for(String name:filelist){
System.out.println(name);
}

And if you want pass reference of a file then you can use
file:///android_assets/MyName/yourfilename

how to get file path of asset folder in android

You could put your mp3 files at : res/raw folder.

MediaPlayer mediaPlayer = MediaPlayer.create(getApplicationContext(), R.raw.myringtone);
mediaPlayer.start();

How to determine the Absolute path for specific file from Assets?

Assets are compiled into .apk file (which basically is zip file). You can't get system path into zip file. You have to manually unzip .apk to some folder and get file system path to asset folder and files inside that folder.

But it sounds like you don't really need a path to asset files. You should look into these functions:

Context.getAssets()

AssetManager.open(String fileName)

AssetManager.openFd(String fileName)

These functions allow you to get input stream or file descriptor for any of your assets. Here is an example:

AssetManager assetManager = getAssets();
InputStream assetIn = assetManager.open("excanvas.js");
//do something with assetIn...

You need to specify path to your asset relatively to asset folder.

How to read a text file from assets directory as a string?

You can open an input stream using AssetsManager.

InputStream input = getAssets().open("origin");
Reader reader = new InputStreamReader(input, "UTF-8");

getAssets() is a method of the Context class.

Also note that you shouldn't recreate a buffer of characters (buf = new char[1024], the last line of your cycle).

Pass path from Assets in Android

Ok i wrote an email to the author and the solution is still not published and will be added in a future distribution. i got an experimental version in which the code looks like the following:

AndroidThemeLoader loader = new AndroidThemeLoader(getBaseContext());
Theme theme = new Theme(loader);
Chunk chunk = theme.makeChunk("test#subtest");
chunk.set("name", "Dennis");
String html = chunk.toString();
String mime = "text/html";
String encoding = "utf-8";
WebView myWebView = (WebView)this.findViewById(R.id.myWebView);
myWebView.getSettings().setJavaScriptEnabled(true);
String baseURL = null;
myWebView.loadDataWithBaseURL(baseURL, html, mime, encoding, null);

Thank you everyone for helping me!

Access file in assets folder via fileinputstream(string fileName) in android app

Due to using a third party API I am stuck trying to pass a String "file path" to a method in the API in order to use the functionality of the API.

Find a better library, one that accepts an InputStream or Reader. Feel free to point whoever created that library to this blog post to help explain the issue.

How do I programmatically get string file path of a file located in the assets folder of my apk on an android device?

You don't, because assets are not files on the device. They are only files on your development machine.

Your only option is to copy the content from the asset to some file that you control (e.g., in getCacheDir()) using standard Java I/O, then use that file with the library.

Trying to get the path for a file in the assets or raw directory

I want to get the path so I can pass it to a function and load it. However, I can’t seem to get the path

That is because there is no path. Those are not files. They are entries in the APK file.

I get FileNotFoundException Exception and the and the string contains: android.content.res.AssetManager$AssetInputStream@5363f738

That is because you called toString() on an InputStream returned from the AssetManager.

Source snippet for loading the keystore

load() takes an InputStream. You do not have to use a FileInputStream. You are welcome to pass the InputStream that you get from the AssetManager to load():

KeyStore keyStoreFile = KeyStore.getInstance(KeyStore.getDefaultType());
keyStoreFile.load(resources.getAssets().open("snapzkeystore.bks"), password);

Just a quick questions, which is the best folder to put a keystore in, raw or assets?

Either should be fine. You can use openRawResource() on Resources, IIRC, to get an InputStream on a raw resource.

How to get URI from an asset File?

There is no "absolute path for a file existing in the asset folder". The content of your project's assets/ folder are packaged in the APK file. Use an AssetManager object to get an InputStream on an asset.

For WebView, you can use the file Uri scheme in much the same way you would use a URL. The syntax for assets is file:///android_asset/... (note: three slashes) where the ellipsis is the path of the file from within the assets/ folder.



Related Topics



Leave a reply



Submit