Load an image from assets folder
If you know the filename in the code, calling this won't be a problem:
ImageView iw= (ImageView)findViewById(R.id.imageView1);
int resID = getResources().getIdentifier(drawableName, "drawable", getPackageName());
iw.setImageResource(resID);
Your filename will be the same name as drawableName so you won't have to deal with assets.
How to load a image from assets?
You can follow my tutorials on displaying data from Assets: https://xjaphx.wordpress.com/2011/10/02/store-and-use-files-in-assets/
The sample with loading image and text to display.
I now added the relevant part of the provided link
(in case of earthquake or something) ;-) Taifun
// load image
try {
// get input stream
InputStream ims = getAssets().open("avatar.jpg");
// load image as Drawable
Drawable d = Drawable.createFromStream(ims, null);
// set image to ImageView
mImage.setImageDrawable(d);
}
catch(IOException ex) {
return;
}
How to load image (and other assets) in Angular an project?
I fixed it. My actual image file name had spaces in it, and for whatever reason Angular did not like that. When I removed the spaces from my file name, assets/images/myimage.png
worked.
How to use image in android:src= from assets folder instead of drawable
No it is not possible directly in XML although you can do it pragmatically.
AssetManager assetManager = getAssets();
ImageView imageView = (ImageView) findViewById(R.id.iv_image);
try {
InputStream ims = assetManager.open("my_image.jpg");
Drawable d = Drawable.createFromStream(ims, null);
imageView.setImageDrawable(d);
} catch (IOException ex) {
return;
}
How to get images from assets folder?
I found the solution. This code (require) doesn't exist in vue.js 3:
<img :src="require(`../assets/${slide}.jpg`)" />
Run: npm run build and you'll get dist folder where you can save your images then call from there. It works for me.
So the solution is:
<template>
<img :src="picture.pic" />
</template>
<script>
import { ref } from "vue";
const loadImage = async () => {
return new Promise((resolve) => {
resolve({
pic: "dist/assets/bg-1.jpg",
});
});
};
};
export default {
async setup() {
const picture = ref(await loadImage());
return {
picture,
};
},
};
</script>
Trying to load an image inside a assets/img folder in a v-for, but the images does not load
I met this question before.
Maybe the following code could help you.
// create a function in util.js
export const getSrc = ( name ) => {
const path = `/src/assets/img/${name}`
const modules = import.meta.globEager('/src/assets/img/*.jpg')
return modules[path].default
}
// use getSrc in someItem.vue
import { getSrc } from '@/util/util.js'
content: [
{
headline: 'Teste',
paragraph: 'this is a paragraph',
img: getSrc('img.jpg')
}
]
unable to load image from assets folder in angular
A few things needs to be considered:
ViewChild binds to the view, so its best to await for the view to finish initialization (use ngAfterViewInit hook), but ngOnInit will also work (it is just if you would need in future to consider offsets etc for drawing into canvas it might bite you:)
Image loading is an async task, you need to leverage onload hook before you attempt to draw the image
Use "/assets/.." urls to avoid issues
Updated code:
export class CanvasComponent implements OnInit {
@ViewChild("crfCanvas", { static: true }) crfCanvas: ElementRef;
constructor() { }
ngAfterViewInit() {
let canvas = this.crfCanvas.nativeElement;
let ctx = canvas.getContext("2d");
let img = new Image();
img.src = "/assets/aCRF-PRV111_CLN-001 v1.4-images/aCRF-PRV111_CLN-001 v1.4-blank_0.jpg";
img.onload = () => {
ctx.drawImage(img, 10, 10, 250, 250);
}
}
}
Stackblitz: https://stackblitz.com/edit/angular-v5tqbn
How to load image from assets folder inside a pdf in Flutter web?
You can convert your ByteData
directly to Uint8List
as shown in the example code below. This can then be passed to the MemoryImage
constructor:
Future<void> addPage(pw.Document pdf, String filename) async {
final imageByteData = await rootBundle.load('assets/$filename');
// Convert ByteData to Uint8List
final imageUint8List = imageByteData.buffer
.asUint8List(imageByteData.offsetInBytes, imageByteData.lengthInBytes);
final image = pw.MemoryImage(imageUint8List);
pdf.addPage(
pw.Page(
build: (pw.Context context) {
return pw.Center(
child: pw.Image(image),
); // Center
},
),
);
}
Related Topics
How to Repeat a Method Every 10 Minutes After a Button Press and End It on Another Button Press
Switch Keyboard Profile Programmatically
Turn Off Device Programmatically
Android - How to Enable Autostart Option Programmatically in Xiaomi Devices
How to Serialize My User Class for the Firebase Database and Fix This Error
How to Implement Draggable Map Like Uber Android, Update with Change Location
How to Call a Function After Delay in Kotlin
Android Studio: Cannot Recover Key
Seek Bar, Change Path Color from Yellow to White
How to Get Child of Child Value from Firebase in Android
Handling Buttons Inside Android Notifications
Work Around Canvas.Clippath() That Is Not Supported in Android Any More
Autocompletetextview with Custom Adapter and Filter
Search in Listview with Edittext
Wrong Requestcode in Onactivityresult
How to Delete SQLite Database from Android Programmatically
Android Studio 3.0 Execution Failed for Task: Unable to Merge Dex