How to Get the Android Path String to a File on Assets Folder

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.

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 get the android Path string to a file on Assets folder?

AFAIK the files in the assets directory don't get unpacked.
Instead, they are read directly from the APK (ZIP) file.

So, you really can't make stuff that expects a file accept an asset 'file'.

Instead, you'll have to extract the asset and write it to a seperate file, like Dumitru suggests:

  File f = new File(getCacheDir()+"/m1.map");
if (!f.exists()) try {

InputStream is = getAssets().open("m1.map");
int size = is.available();
byte[] buffer = new byte[size];
is.read(buffer);
is.close();

FileOutputStream fos = new FileOutputStream(f);
fos.write(buffer);
fos.close();
} catch (Exception e) { throw new RuntimeException(e); }

mapView.setMapFile(f.getPath());

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.

access path of file in android as a string

Font urFontName = FontFactory.getFont("assets/fonts/arial.ttf",BaseFont.IDENTITY_H,BaseFont.EMBEDDED);


Related Topics



Leave a reply



Submit