How to Retrieve Image from Project Folder

How to retrieve image from project folder?

Put all the images in images folder that you have in your project in parallel to src folder.

 F:/>Kiosk
|
|___src
|
|___lib
|
|___koisk
|
|__main1.java
|
|__images
|
|__c.jpg
|__d.jpg
|__e.jpg
|__f.jpg

Use this code

list.add(resizeImage(ImageIO.read(new File("images\\c.jpg"))));
list.add(resizeImage(ImageIO.read(new File("images\\d.jpg"))));
list.add(resizeImage(ImageIO.read(new File("images\\e.jpg"))));
list.add(resizeImage(ImageIO.read(new File("images\\f.jpg"))));

-- EDIT --

You can try any one

// Read from same package 
list.add(resizeImage(ImageIO.read(getClass().getResourceAsStream("c.png"))));

// Read from absolute path
list.add(resizeImage(ImageIO.read(new File("E:\\SOFTWARE\\TrainPIS\\res\\drawable\\c.png"))));

// Read from images folder parallel to src in your project
list.add(resizeImage(ImageIO.read(new File("images\\c.jpg"))));

Repeat any one for all four images.

How to retrieve image from project folder

You can list every file contained in the folder like this :

File[] files = new File("images/").listFiles();

Note that it will also give the subdirectories.

So instead of

list.add(resizeImage(ImageIO.read(new File("images\\Picture2.png"))));
list.add(resizeImage(ImageIO.read(new File("images\\Picture3.png"))));
list.add(resizeImage(ImageIO.read(new File("images\\Picture4.png"))));
list.add(resizeImage(ImageIO.read(new File("images\\Picture5.png"))));

You can simply loop over each files given by listFiles method. You could also use listFiles(FileFilter) in order to filter out each file that isn't an image.

Load image from folder inside project

Look at Filemanagers API. It offers all you need.

Example to get content at path:

let pathToDir1 = Bundle.main.resourcePath! + "/Stickers/1";
let fileManager = FileManager.default
let contentOfDir1 = try! fileManager.contentsOfDirectory(atPath: pathToDir1)

Example to iterate over:

let docsPath = Bundle.main.resourcePath!
let enumerator = fileManager.enumerator(atPath:docsPath)
let images = [UIImage]()
while let path = enumerator?.nextObject() as! String {
let contentOfCurDir = try! fileManager.contentsOfDirectory(atPath: path)
// do whatever you need.
}

For details see Apples documentation and sample code.

Load image from a file inside a project folder

Set the assets directory as a resource directory and then load the image as a resource from the location "/drawIcon.png":

URL url = getClass().getResource("/drawIcon.png");
Image image = ImageIO.read(url);

In case you want to create a javafx Image:

Image image = new Image("/drawIcon.png");

In this case, also, mark that folder as resource folder.

More info here: https://docs.oracle.com/javafx/2/api/javafx/scene/image/Image.html

retrieve images from different directory

Ok - so here's what you could do to make it work.

If you don't have already, create your own helpers file and add it to composer's autoload:

app/helpers.php

<?php

if (! function_exists('parentAsset')) {
/**
* Generate a parentAsset path for the application.
*
* @param string $path
* @param bool|null $secure
* @return string
*/
function parentAsset($path, $secure = null)
{
return app('parentUrl')->asset($path, $secure);
}
}

composer.json

{
//...
"autoload": {
"psr-4": {
"App\\": "app/"
},
"classmap": [
"database/seeds",
"database/factories"
],
"files": [
"app/helpers.php"
]
},
//...
}

Register parentUrl in the container

app/Providers/AppServiceProvider.php

use Illuminate\Routing\UrlGenerator;

public function register()
{
$this->app->singleton('parentUrl', function ($app) {
$routes = $app['router']->getRoutes();

$app->instance('routes', $routes);

return new UrlGenerator(
$routes,
$app->rebinding('request', $this->requestRebinder()),
$app['config']['app.parent_asset_url']
);
});
}

/**
* Get the URL generator request rebinder.
*
* @return \Closure
*/
protected function requestRebinder()
{
return function ($app, $request) {
$app['url']->setRequest($request);
};
}

Please note the new config entry for app namespace: parent_asset_url. You now need to add it to your app.php config file.

config/app.php

[

//...
'asset_url' => env('ASSET_URL', null),
'parent_asset_url' => env('PARENT_ASSET_URL', null),
//...

]

Lastly you need to add PARENT_ASSET_URL variable to your .env file and specify the url of your parent application.

PARENT_ASSET_URL=https://google.com

Recompile autoloader

composer dump-autoload -o

And you can now use the parentAsset helper to load files directly from the parent domain:

<img src="{{ parentAsset('/assets/images/logo.svg') }}">

Hope this helps.

how to load images from a local directory and display them in reactjs app?

There are a couple of different approaches:

1) Public folder

When you start a react app, in your folder structure you'll find a "public" folder. This folder for example also contains the favicon. When you put your images there, you can reach them by

<img src="/imagename.jpg" />

In your database, you will need to store the path "/imagename.jpg" to reach them then.

If you would create a subfolder in the "public" folder, the path in the src attribute will also need to change.
Example:
- public
-- assets
--- avatar.jpg

<img src="/assets/avatar.jpg" />

2) S3 (or some similar services)

Upload them to Amazon S3 and store that path. This can either be done manually or let your API handle this. This way you can just provide an absolute path to the img-tag.

3) Own webserver

Upload them to your own webserver while you upload the image.

Remark: Retrieving files from a local folder outside your project folder is not possible without the use of some other webserver.

Show Image View from file path?

Labeeb is right about why you need to set image using path if your resources are already laying inside the resource folder ,

This kind of path is needed only when your images are stored in SD-Card .

And try the below code to set Bitmap images from a file stored inside a SD-Card .

File imgFile = new  File("/sdcard/Images/test_image.jpg");

if(imgFile.exists()){

Bitmap myBitmap = BitmapFactory.decodeFile(imgFile.getAbsolutePath());

ImageView myImage = (ImageView) findViewById(R.id.imageviewTest);

myImage.setImageBitmap(myBitmap);

}

And include this permission in the manifest file:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />


Related Topics



Leave a reply



Submit