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
Validate Username and Email Crashed and Cannot Insert to Firebase Database
How to Display Input Errors in Popup
Memory Leaks with Custom Font for Set Custom Font
Toolbar Overlapping Below Status Bar
How to Resolve "Waiting for Debugger" Message
Incompatible Plugins for Android-Apt After Upgrading to Android Studio 2.3
Differencebetween Activity and Context
How to Store a Boolean Value Using Sharedpreferences in Android
How to Set Android_Home Path in Ubuntu
How to Determine Which Dependency Causes Google Play Openssl Warning
What Is the Intent of the Methods Getitem and Getitemid in the Android Class Baseadapter
Android - R Cannot Be Resolved to a Variable
How to Get the Android Path String to a File on Assets Folder
Adb Is Not Recognized as Internal or External Command on Windows