How to Get Uri from an Asset File

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 can I get uri of a file that located in assets

There is no "absolute path for a file existing in the asset folder".

You can use AssetManager object to get an InputStream

Android get URI of assets folder

Use something like this:

AssetManager assets = getApplicationContext().getResources().getAssets();
String[] pics = assets.list("pics");
ArrayList<Bitmap> bmps = new ArrayList<>();

for (String path : pics) {
bmps.add(BitmapFactory.decodeStream(assets.open(path));
}

The bmps list now contains all the images in your pics folder as Bitmaps.

I wrote this purely from documentation, and I haven't tested it. I recommend you read the documentation yourself if you run into trouble: https://developer.android.com/reference/android/content/res/AssetManager

How to get uri to included asset in Uno Platform?

Files read from the StorageFile.GetFileFromApplicationUriAsync() method may not return a stable path on UWP, and this is particularly the case on particularly on Android or WebAssembly, where the file is not necessarily on the file system.

On Android, the file is a Stream directly built from the APK file, and on WebAssembly it is stored in a temporary location.

In order to use keep a stable copy of the file, use the following:

var file = await StorageFile.GetFileFromApplicationUriAsync(new System.Uri("ms-appx:///TextFile.txt"));
var newFile = await file.CopyAsync(Windows.Storage.ApplicationData.Current.LocalFolder, file.Name, NameCollisionOption.ReplaceExisting);

var txt = await FileIO.ReadTextAsync(newFile);

Getting content URI for a file in asset folder

try this code

InputStream inputStream = am.open("myfoldername/myfilename");
File file = createFileFromInputStream(inputStream);
final Uri uri = Uri.fromFile(file);

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;
}

Android assets, how can I read a file from a subfolder?

Use AssetManager and its open() method. So, you would replace:

ContentResolver r = controls.activity.getContentResolver();
InputStream in = r.openInputStream(Uri.parse(u));

with:

AssetManager assets = controls.activity.getAssets();
InputStream in = assets.open("sub/file.txt");


Related Topics



Leave a reply



Submit