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.
Related
I have created a video thumbnail from a video file. What I need to do is to create a java.io.File from the Bitmap in order to upload the thumbnail to a server. How could achieve this?
Bitmap thumb = ThumbnailUtils.createVideoThumbnail(videoFile.getPath(), MediaStore.Video.Thumbnails.FULL_SCREEN_KIND);
File thumbFile = ?
can use the following code:
//create a file to write bitmap data
File f = new File(context.getCacheDir(), filename);
//f.createNewFile(); (No need to call as FileOutputStream will automatically create the file)
//Convert bitmap to byte array
Bitmap bitmap = your bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
byte[] bitmapdata = bos.toByteArray();
//write the bytes in file
FileOutputStream fos = new FileOutputStream(f);
fos.write(bitmapdata);
fos.flush();
fos.close();
you can also try this method: it does not convert bitmap to byte Array..
private static void bitmap_to_file(Bitmap bitmap, String name) {
File filesDir = getAppContext().getFilesDir();
File imageFile = new File(filesDir, name + ".jpg");
OutputStream os;
try {
os = new FileOutputStream(imageFile);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, os);
os.flush();
os.close();
} catch (Exception e) {
Log.e(getClass().getSimpleName(), "Error writing bitmap", e);
}
}
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.
Here's the code that saves an image file to... somewhere? How do I get the URI for the file "webimage"?
Bitmap bmp = ((BitmapDrawable)imageView.getDrawable()).getBitmap();
String fileName = "webImage";//no .png or .jpg needed
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, bytes);
FileOutputStream fo = openFileOutput(fileName, Context.MODE_PRIVATE);
fo.write(bytes.toByteArray());
// remember close file output
fo.close();
} catch (Exception e) {
e.printStackTrace();
}
Use getFileStreamPath() like this:
String fileName = "webImage";
//...
Uri uri = Uri.fromFile(getFileStreamPath(fileName));
Figured it out I think:
final File file = new File(getFilesDir(), "webImage");
Uri weburi = Uri.fromFile(file);
You can use Uri.fromFile(imageFile), where imageFile is an instance of File
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);
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();