Load an Image from Assets Folder

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:

  1. 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:)

  2. Image loading is an async task, you need to leverage onload hook before you attempt to draw the image

  3. 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



Leave a reply



Submit