Unable to see the saved image as it looks in imageview - java

I use following code to save image that is taken by camera
File storageDir= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myphoto.png");// getAlbumName()
FileOutputStream out = new FileOutputStream(storageDir);
ObjectOutputStream oos =new ObjectOutputStream(out);
mImageBitmap.compress(Bitmap.CompressFormat.PNG,100 , oos);
oos.flush();
oos.close();
I get "myphoto.png" in "\pictures" folder, however when try to open the image I can see just black window instead of the image. What am I missing? Thanks

You should not compress into ObjectOutputStream oos but into FileOutputStream out. ObjectOutputStream is for Java objects and it adds unwanted data to your output.
File storageDir= new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES),"myphoto.png");// getAlbumName()
FileOutputStream out = new FileOutputStream(storageDir);
mImageBitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
out.flush();
out.close();

Related

Bitmap saving corrupted image

While saving an image from the camera to an external storage directory, the image appears as a grey box in the gallery and is not visible in the file manager as well.
I have already enabled the required write permissions for the storage as well.
The image looks like this in the gallery
https://i.imgur.com/9D6IDEI.jpg
String filepath= "/storage/emulated/0/Pictures/Email_Client/1564907913797.jpg"
File file = new File(filepath);
file.createNewFile();
final FileOutputStream fos = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
fos.close();
Try something like below.
String filepath= "/storage/emulated/0/Pictures/Email_Client/1564907913797.jpeg"
File file = new File(filepath);
file.createNewFile();
Uri imageUri = Uri.fromFile(file);
OutputStream outstream;
try {
outstream = getContentResolver().openOutputStream(imageUri);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, outstream);
outstream.close();
}
If you're using Camera (and not Camera2) and PictureCallback, try this:
String filepath= "/storage/emulated/0/Pictures/Email_Client/1564907913797.jpg"
File file = new File(filepath);
final FileOutputStream fos = new FileOutputStream(file);
fos.write(data);
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, fos);
fos.close();
where data is the array of bytes of the picture taken by the camera that you get as parameter in onPictureTaken(byte[] data, Camera camera).
The files you save are empty because you're not actually saving any picture data.

Generate and open PDF in new browser at clients machine with java IText

I am trying to write in an existing pdf file and want to open it in new browser at clients machine. I saw many post related this and i tried all answers but still i am not getting result. File can be generated on my disk but i want to open it at new window once created.
Code :
response.setContentType("application/pdf");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = new FileInputStream("Challan.pdf");
PdfReader pdfReader = new PdfReader(is, null);
PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);
for(int i=1; i<= pdfReader.getNumberOfPages(); i++){
Phrase Namephrase = new Phrase("John Smith");
Phrase Idphrase = new Phrase("DPG10001");
Phrase Datephrase = new Phrase("11/10/1980");
PdfContentByte pdfcontent = pdfStamper.getUnderContent(i);
pdfcontent.beginText();
ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED_ALL, Idphrase, 500, 428, 0); /*Do not change this values*/
ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED, Namephrase, 500, 407, 0);
ColumnText.showTextAligned(pdfcontent, Element.ALIGN_JUSTIFIED, Datephrase, 500, 386, 0);
pdfcontent.endText();
}
pdfStamper.close();
pdfReader.close();
// write ByteArrayOutputStream to the ServletOutputStream
response.setHeader("Content-Disposition", "inline;filename=ChallanStamped.pdf");
// the contentlength
response.setContentLength(baos.size());
ServletOutputStream sos = response.getOutputStream();
baos.writeTo(sos);
sos.flush();
sos.close();
In this i am trying to read challan.pdf and want to update file and create new one.From jsp i am calling this servlet.
Please help me.
Update:
I tried with some changes but still not working. My updated code is
` ByteArrayOutputStream baos = new ByteArrayOutputStream();
BufferedInputStream is = new BufferedInputStream(new FileInputStream("Challan.pdf"));
PdfReader pdfReader = new PdfReader(is, null);
PdfStamper pdfStamper = new PdfStamper(pdfReader, baos);
-----------Some Code -------------
pdfStamper.close();
pdfReader.close();
response.setHeader("Content-Disposition", "inline;filename=ChallanStamped.pdf");
response.setContentLength(baos.size());
output = new BufferedOutputStream(response.getOutputStream());
byte[] buffer = new byte[4096];
int length;
while ((length = input.read(buffer)) != -1) {
output.write(buffer, 0, length);
}
output.flush();
output.close();
input.close();
I am calling servlet from jsp through Ajax(sending lot of data).
And also i am getting java.io.IOException: Stream closed exception with this updated code.
In order to open the pdf file in new window you need to do some changes in your jsp form.
<form action="yourServletURLPattern" method="post" target="_blank">
-- Some Fields--
-- Submit Button--
</form>
target=_blank will do the needful.
Now coming to your servlet code :
ByteArrayOutputStream baos = new ByteArrayOutputStream();
---------- Some Code in between-----------------------
ServletOutputStream sos = response.getOutputStream();
baos.writeTo(sos);
But this do nothing since your ByteArrayOutputStream Object contains nothing so what will you write to ServletOutputStream
I will not write code for you but can guide, So that you can achieve it yourself
Read File after editing using BufferedInputStream
Write it on OutputStream of Response using BufferedOutputStream (Simply Loop
through the file)
Flush the Response's Output Stream

Incorrect upload file use FileOutputStream. Incorrect size

When I try upload the file from server, I use this code. I get the file size of 500kb, when the original file size about 300kb.
What am I doing wrong?
attachmentContent = applicationApi.getApplicationAttachmentContent(applicationame);
InputStream in = new BufferedInputStream(attachmentContent.getAttachmentContent().getInputStream());
ByteArrayOutputStream out = new ByteArrayOutputStream();
byte[] buf = new byte[1024];
int n=0;
while (-1 !=(n=in.read(buf)))
{
out.write(buf,0,n);
}
out.close();
in.close();
byte[] response = out.toByteArray();
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.write(response);
outStream.close();
Simplify. The same result:
File transferredFile = new File(attachmentName);
FileOutputStream outStream = new FileOutputStream(transferredFile);
attachmentContent.getAttachmentContent().writeTo(outStream);
outStream.close();
The ByteArrayOutputStream is a complete waste of time and space. You're reading the attachment twice, and writing it twice too. Just read from the attachment and write directly to the file. Simplify, simplify. You don't need 90% of this.

how to get a image for a label from FileOutPutStream in java?

i created a QR generator in java.I tested my application using FileOutPutStream.So it worked properly,now i need to get the generated QR image to a jLabel before save,so how to do this.please help me??
this is my code:
ByteArrayOutputStream out = QRCode.from("Hello World").to(ImageType.PNG).stream();
ByteArrayOutputStream webout = QRCode.from("http://viralpatel.net").to(ImageType.PNG).stream();
try {
FileOutputStream fout = new FileOutputStream(new File("D:\\QR_Code.JPG"));
BufferedImage image= ImageIO.read(new FileOutputStream(fout)));
fout.write(out.toByteArray());
fout.flush();
fout.close();
} catch (Exception e){
}
A ByteArrayOutputStream is used to write to a final byte array.
A ByteArrayInputStream allows to read from an initial byte array.
So:
ByteArrayOutputStream out = ...
ByteArrayInputStream in = new ByteArrayInputStream(out.toByteArray());
BufferedImage image= ImageIO.read(in);

Upload image to servlet from android client gives a corrupt file

I'm trying to upload an image to servlet from my android client. The code executes well, however when I try to open the sended image, it seems corrupted. ¿Do you know why?
The code is the next:
Android client:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 90, stream);//compress to which format you want.
byte [] byte_arr = stream.toByteArray();
String image_str = Base64.encodeToString(byte_arr, 0);
image_str is what I'm sending to servlet:
byte[] imageByteArray = Base64.decode(message);
FileOutputStream f = new FileOutputStream("/path/IMG/pruebaaaa.jpg");
f.write(imageByteArray);
f.close();
Thank you in advance!
Try putting in flush() just above close() as below.
f.write(imageByteArray);
f.flush();
f.close();

Categories