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
Related
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 ();
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.
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);
}
}
I am trying to open an image I have stored on external memory. Here is the code I have:
File imagePath = new File(imageURI);
InputStream inputStream=null;
try {
inputStream = getContentResolver().openInputStream(Uri.parse(imageURI));
}catch(FileNotFoundException e){
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(inputStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
byte[] originalImage = baos.toByteArray();
But it doesn't seem to be able to locate the file. The Uri is in the format content://com.android.providers.media.documents/document/image%3A21.
Thanks for any help.
In a project I am working on now I have external images in a directory on the SD card. I am using
String thePath = Environment.getExternalStorageDirectory() + "/myAppFiles”;
File imgFile = new File(thePath + " / " + "
externalImage.jpg ");
if (imgFile.exists()) {
try {
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 6;
Bitmap bm = BitmapFactory.decodeFile(imgFile.getAbsolutePath(), options);
}
}
Try this:
File imagePath = new File(imageURI.getPath());
url.getPath() returns a String in the following format: "/mnt/sdcard/xxx.jpg", without the scheme type pre-fixed
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();
}