Android Studio - Convert Bitmap into java.io.File - java

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);
}
}

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.

How do I get the URI of a file saved with FileOutputStream?

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

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);

java download file does not work in file size is more than 8K?

i try to download a pdf file from url using the code below. It works fine when the file size is under 8k and if the file is up to 8k, it downloads the pdf file but the file is not readable.
Code
InputStream in = new BufferedInputStream(url.openStream());
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();
FileOutputStream fos = new FileOutputStream(new File("C:\\temp\\TEST.pdf"));
fos.write(response);
fos.close();
System.out.println("Download Finished");
You may try using the IOUtils.copy(InputStream input, OutputStream output)
You can minimize the lines of code.
Apache IOUtils

creating .bmp image file from Bitmap class

I've created an application that uses sockets in which the client receives the image and stores the data of the image in Bitmap class....
Can anyone please tell me how to create a file named myimage.png or myimage.bmp from this Bitmap object
String base64Code = dataInputStream.readUTF();
byte[] decodedString = null;
decodedString = Base64.decode(base64Code);
Bitmap bitmap = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
Try following code to save image as PNG format
try {
FileOutputStream out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
}
out.flush();
out.close();
Here, 100 is quality to save in Compression. You can pass anything between 0 to 100. Lower the digit, poor quality with decreased size.
Note
You need to take permission in Android Manifest file.
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
Edit
To save your image to .BMP format, Android Bitmap Util will help you. It has very simple implementation.
String sdcardBmpPath = Environment.getExternalStorageDirectory().getAbsolutePath() + "/sample_text.bmp";
Bitmap testBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.sample_text);
AndroidBmpUtil bmpUtil = new AndroidBmpUtil();
boolean isSaveResult = bmpUtil.save(testBitmap, sdcardBmpPath);
try {
FileOutputStream out = new FileOutputStream(filename);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, out);
} catch (Exception e) {
e.printStackTrace();
} finally {
out.close();
}

Categories