how to update image showing in imageView
i set this image from database
code to get image on imageView from db is...
InputStream is = resultSet.getBinaryStream("image");
OutputStream os;
os = new FileOutputStream(new File("src/sources/images/photo.jpg"));
byte[]content = new byte[1024];
int size = 0;
while((size=is.read(content))!= -1)
{
os.write(content,0,size);
}
os.close();
is.close();
image = new Image("file:src/sources/images/photo.jpg");
imageView.setImage(image);
image is saving in BLOB type in MySQL database
Now i want to update this image and set it again this image if i dont choose any image using file chooser
please solve my this problem
thanks in advance
File chooser code
stage = (Stage) showScene.getScene().getWindow();
file = fileChooser.showOpenDialog(stage);
if(file != null){
image = new Image(file.getAbsoluteFile().toURI().toString(),imageView.getFitWidth(),imageView.getFitHeight(),true,true);
imageView.setImage(image);
imageView.setPreserveRatio(true);
}
fis = new FileInputStream(file); // here i got error(null pointer exception) if i try to update withouting choosing image from filechooser
I pass fis to the preparedstatement to update and also insert
You are getting a NullPointerException because if you don't choose a file with the FileChooser, your file variable is set to null. This can be resolved by altering your if statement a little.
file = fileChooser.showOpenDialog(stage);
if (file == null) {
file = new File("path/to/default/file")
}
image = new Image(file.getAbsoluteFile().toURI().toString(),imageView.getFitWidth(),imageView.getFitHeight(),true,true);
imageView.setImage(image);
imageView.setPreserveRatio(true);
fis = new FileInputStream(file);
Related
I hope you all doing good. I have been trying to save video in my gallery. I have video path which is already saved in a hidden folder. I just want to save that video in my gallery. Here is the code I might making a mistake. If you can resolved it out I will be thankful to you.
File newfile;
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(Uri.parse( path), "r");
FileInputStream in = videoAsset.createInputStream();
String root = Environment.getExternalStorageDirectory().getAbsolutePath();
File dir = new File(root + "/" + "Pictures");
if (!dir.exists()) {
dir.mkdirs();
}
newfile = new File(dir, "status_"+System.currentTimeMillis()+".mp4");
if (newfile.exists()) newfile.delete();
OutputStream out = new FileOutputStream(newfile);
// Copy the bits from instream to outstream
byte[] buf = new byte[1024];
int len;
while ((len = in.read(buf)) > 0) {
out.write(buf, 0, len);
}
in.close();
out.close();
By this code I am having this no content provider /storage/emulated/0/android/data/ exception. As I have already added provider for image and permissions to write storage, but I don't know what provider are needed for video's or the problem is with the code?
I have found the solution, the mistake I was making that not getting the proper FileInputStream from the start.
Just replace this
AssetFileDescriptor videoAsset = getContentResolver().openAssetFileDescriptor(Uri.parse( path), "r");
FileInputStream in = videoAsset.createInputStream();
with this and your good to go :D
FileInputStream in = new FileInputStream(new File(path));
I am using java spring and jpa I have a query to retrieve the blob from an oracle db. I get the blob and if it is a text file it is downloading onto my local machine just fine - but if it is an image I have to go through a BufferedImage, and I am still working on PDF. So far I'm just seeing the extension of the file by getting it's original filename which is also stored in the DB, then filtering the string for the extension. When I get a PDF it says the file is corrupted or open in another window, which it is not.
So far, this is my code that tries to turn blob from DB into a file:
Blob blob = Service.retrieveBlob(ID);
if (blob == null){
return false;
}
String fileName = Service.getFileName(ID);
String extension = fileName.substring(fileName.lastIndexOf('.') + 1);
System.out.println(extension);
if (extension.equals("jpg") || (extension.equals("png"))){
File file = new File("./DBPicture.png");
try(FileOutputStream outputStream = new FileOutputStream(file)) {
BufferedImage bufferedImage = ImageIO.read(blob.getBinaryStream());
ImageIO.write(bufferedImage, "png", outputStream);
System.out.println("Image file location: "+file.getCanonicalPath());
} catch (IOException e) {
e.printStackTrace();
}
}
else {
InputStream ins = blob.getBinaryStream();
byte[] buffer = new byte[ins.available()];
ins.read(buffer);
File targetFile = new File("./" + fileName);
OutputStream outStream = new FileOutputStream(targetFile);
outStream.write(buffer);
}
available is simply that. it is not necessarily the length of the stream.
Try something like
InputStream ins = blob.getBinaryStream();
File targetFile = new File("./" + fileName);
OutputStream outStream = new FileOutputStream(targetFile);
byte[] buffer = new byte[8000];
int count = 0;
while ((count = ins.read(buffer)) != -1) {
outStream.write(buffer);
}
// then close your output
outStream.close ();
Am writing an ID CARD processing app in java but am having problem with the code that will upload and display a client image in a label i created for picture. the only thing am getting with the code below is the image path but not the image it self.
This is the code i have attempted so far.
FileFilter ff = new FileNameExtensionFilter("images","jpeg");
fc.addChoosableFileFilter(ff);
int open = fc.showOpenDialog(this);
if (open == javax.swing.JFileChooser.APPROVE_OPTION){
java.io.File path = fc.getSelectedFile();
String file_name = path.toString();
pathe.setText(file_name);
java.io.File image = fc.getSelectedFile();
ImageIcon photo = new ImageIcon(image.getAbsolutePath());
The code that does the magic is below
FileFilter ff = new FileNameExtensionFilter("images","jpeg");
fc.addChoosableFileFilter(ff);
int open = fc.showOpenDialog(this);
if (open == javax.swing.JFileChooser.APPROVE_OPTION){
java.io.File path = fc.getSelectedFile();
String file_name = path.toString();
pathe.setText(file_name);
BufferedImage bi; // bi is the object of the class BufferedImage
// Now you use try and catch `enter code here`
try{
bi = ImageIO.read(path); // path is your file or image path
jlabel.setIcon( new ImageIcon(bi));
}catch(IOException e){ }
I want to load a picture i have in the smartphone so i can than send it over the internet to a webservice i created.
Here i provide a sample code of what i am trying and not working.
Bitmap bm = BitmapFactory.decodeFile(path);
System.out.println("BITMAP: "+bm != null);
ByteArrayOutputStream buffer = new ByteArrayOutputStream(bm.getWidth() *bm.getHeight());
bm.compress(CompressFormat.JPEG, 100, buffer);
I made sure that bm isn't null with the system out print. I get a NullPointerException in ByteArrayOutputStream. Any suggestions?
Try this. Use file name with the path
String[] files = null;
File path = new File(Environment.getExternalStorageDirectory(),"folder path");
if(path.exists())
{
filename = path.list();
}
for(int i=0; i<count;i++)
{
Bitmap bitmapOrg = BitmapFactory.decodeFile(path.getPath()+"/"+ files[i]);
}
I have opened a webpage in HtmlUnit headless browser. Now that webpage contains a image html tag as follows:
<img src="..." />
So I want that image only. But the problem is that the same src URL of the image shows diff. image each time. Means, if we refresh the img src URL, then it shows diff. image each time.
So how to get the image that is displayed on the html page.
When you get the HTMLPage, you have to get the image through one of its method. You can then get an HtmlImage, which can be saved as a file. You'll just have to analyse this file later.
This is the function to store your image with fully qualified I
protected String saveImage(String imageUrl) throws Exception {
InputStream inputStream;
OutputStream os;
ByteArrayOutputStream byteArrayOutputStream;
String destinationFile = "File path where you want ot store the image";
URL url = new URL(imageUrl);
inputStream = url.openStream();
byteArrayOutputStream = new ByteArrayOutputStream();
os = new FileOutputStream(destinationFile);
int read;
String barcode = null;
while ((read = inputStream.read()) != -1) {
os.write(read);
byteArrayOutputStream.write(read);
barcode = byteArrayOutputStream.toString();
}
inputStream.close();
os.close();
byteArrayOutputStream.close();
return barcode;
}