In a Struts 1 Web App I can access images from a .jsp by
<img src="../../images/myImageName.png"/>
Am I also able to reference that image from a .java class in my source directory?
I am using iText and can fetch an image from a url
String imageUrl = "http://jenkov.com/images/" +
"20081123-20081123-3E1W7902-small-portrait.jpg";
Image image2 = Image.getInstance(new URL(imageUrl));
but if I try and fetch one from
Image image = Image.getInstance("../../images/myImageName.png"/);
It always looks in the bin folder of my server. How can I get the relativePath back to my image?
If the image is in the web app content directory then you need the app context relative path:
String relativeWebPath = "images/myImageName.png";
String absoluteDiskPath = getServletContext().getRealPath(relativeWebPath);
Would I do that? Not unless there's a good reason; I'd prefer either a path off the app context altogether (and the image could be streamed by the app, or set as a symlink, etc) or a classpath-based resource.
Related
I want to get image base64 from cahcedNetworkImageProvider widget without redownload it to share it in my App.
or if there's a way to share image as a URL and could be saved to the device in flutter
used flutter_cache_manager plugin to get cached data through this function
var file = await DefaultCacheManager().getSingleFile(url);
and works fine for cached images and if URL is new will download it then get back with file object
I have Flutter app which needs to load dynamic assets and content which I want to save for later use. I know about Assets I can have in build time at the folder "assets/" inside the app.
I want to download content using ZIP files and unzip them to app local folder so they won't delete in the next app update.
what are the folders Flutter allows me to add assets to at runtime?
You cannot dynamically add assets to Flutter app at runtime and that is why Shared_Preferences package was developed by the official Flutter_Dev Team.
https://pub.dev/packages/shared_preferences
If you want to store a File instead of bits of information then refer to the below example code (For a Image File):
Future getImage(ImageSource imageSource) async {
// using your method of getting an image
final File image = await ImagePicker.pickImage(source: imageSource);
// getting a directory path for saving
final String path = await getApplicationDocumentsDirectory().path;
// copy the file to a new path
final File newImage = await image.copy('$path/image1.png');
setState(() {
_image = newImage;
});}
If your still want to somehow add/delete files dynamically then the answer is that it is practically not possible because assets weren't designed to dynamically store files.
One should only use assets to store files which shall remain common for all users.
I have a web project and I have uploadedImage inside the web folder of the project. I want to load an image which is inside uploadedImage folder. Im deploying the war file in Tomcat server. I coded as follows
Upload a new Image
</div>
</div>
<%
String fname = (String) request.getAttribute("name");
String pathImage = "C://bimla//Dev//java//OCRSystem//WebContent//uploadedImage//";
System.out.print("uploaded image"+fname);
session.setAttribute("filename", fname);
fname = fname + ".jpg";
System.out.println("with extension"+fname);
String path = "";
if (request.getAttribute("name") != null) {
path = request.getAttribute("name").toString();
}
%>
<div class="row">
<div id = "display" class="col-lg-8 center-block modal-content">
<img src="C:\\bimla\\Dev\\java\\OCRSystem\\WebContent\\uploadedImage\<%=fname%>" width="600" height="400"/>
</div>
</div>
But image is not displayed. When copy paste the path in the browser image displays correctly. Do you have any idea?
Your page is loaded through http (I assume). Nowadays luckily there is quite some boundary that websites (e.g. everything coming in through http) can't address your local filesystem (e.g. URLs starting with file:/// or C:/)
On a related note, don't build a web application that allows to upload files right into the webapplication's folder. This is bad for
security
backup
updates
redeployment
Rather upload to some other location and include a download servlet.
Side note: you might need duplicate \\ to escape backslashes in various cases (HTML has no such limitations though), but you definitely only need single / if you choose to use slash as the directory separator (this is not your problem though)
Quick Fix: You should load the image through http. Assuming that the image is in the same web application as your JSP, using <img src="<%=request.getContextPath()%>/uploadImages/<%=fname%>"> (or similar - you don't provide enough information for a definitive statement) will load the image through http. Note that this only quickfixes your current problem. The proper fix will be to upload the images to a folder (or database or storage) outside of your web application's directory - for the reasons given above.
I am going through http://developer.xamarin.com/guides/cross-platform/xamarin-forms/working-with/images/
and trying to get the Local Images working with Android however I am experiencing some issues when attempting to do something in normal monodroid just as a further test. I'm using a SharedProject, just for reference.
I have added the test image at Resources/drawable (test1.png), and also setting the Build Action as AndroidResource as it describes, and if I do the following in Xamarin.Forms it works:-
Xamarin.Forms.Image myimage = new Image();
myimage.Source = ImageSource.FromFile("test1.png");
However, if I try and retrieve the same file via the following it comes back as null.
Android.Graphics.Bitmap objBitmapImage = Android.Graphics.BitmapFactory.DecodeFile("test1.png")
Does anyone know why this is null and how to correct?
The Xamarin.Forms FileImageSource has a fallback mode as it covers two different scenarios.
First it will check whether the file exists in the file system via the BitmapFactory.DecodeFile.
Should the file not exist as specified in the File property, then it will use BitmapFactory.DecodeResource to try and load the resource content as a secondary alternative.
FileImageSource for Android therefore isn't only just for files that exist in the file system but also for retrieving named resources also.
This link indicates that Android Resources are compiled into the application and therefore wouldn't be part of the file system as physical files.
Since test1.png is a Drawable you should use BitmapFactory.DecodeResource ()
string scr = "#drawable/test1.png";
ImageView iv = new ImageView(context);
Bitmap bm = BitmapFactory.DecodeFile(src);
if (bm != null)
iv.SetImageBitmap(bm);
I'm using iText for creating a PDF with the AbstractPdfView.
My image is located under
-webapp
--resources
---img
----logo.jpg
I'm trying to load this into my PDF but I always get a FileNotFoundException
Image.getInstance("/resources/img/logo.jpg")
How do I load an image which is located under my webapp folder into my PDF?
Was just doing that this AM...
ServletContext servletContext = request.getSession().getServletContext();
Resource res = new ServletContextResource(servletContext,"/images/logo.png");
First of all add a folder inside src/main/resources. For example:
src/main/resources/images
Inside this folder put your image. Let's say:
src/main/resources/images/logo.png
Then you can use this resource from an AbstractPdfView as:
URL imageUrl = getClass().getResource("/images/logo.png");
Image logo = Image.getInstance(imageUrl);
Then use that image in your document however you need to.
Regards!