How to Load a Image from 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;
}

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

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 can I dynamically load an image from my assets folder based on props in ReactJS?

The best way to do that should be this:

First: make index.js inside image folder then import all image

import img1 from "./image_name.jpg";
export const image_name = img1;

or

export const image_name = require("./image_name.jpg")

Second: import them like this:

import * as images from "../assets/img";

Last: use them like module

 images.image_name 

Or

<img src={images[props.image_name])} />

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

Can't load a new image from .xcassets file

Finally figured this out. This is a fairly large project with multiple targets, and it seems that the code in the target I'm working in is for some reason looking at the .xcassets file from another target (I checked the other xcasset file's target membership, and it's not included in the target I'm working in, so I don't understand why this is happening).

I can work around this by putting my images in the other xcasset file (don't really like this), or specifying the bundle with UIImage(named:in:with:).

If anyone knows why this is happening, I'd love to fix the root issue (maybe it's not an issue, and I'm just not understanding how it's supposed to work). I'll let this sit for 24 hours before accepting this as an answer.

How to add image in Flutter

I think the error is caused by the redundant ,

flutter:
uses-material-design: true, # <<< redundant , at the end of the line
assets:
- images/lake.jpg

I'd also suggest to create an assets folder in the directory that contains the pubspec.yaml file and move images there and use

flutter:
uses-material-design: true
assets:
- assets/images/lake.jpg

The assets directory will get some additional IDE support that you won't have if you put assets somewhere else.



Related Topics



Leave a reply



Submit