Open image from Camera and save it loses quality - android

I have a problem, when I take a picture from my camera, I save the image by directly passing the parameter of the target folder.
takePictureIntent.putExtra("output", uriSavedImage);
This picture is saved with the maximum quality.
But if I open this picture, when I save it, even if I set to the maximum quality (100 if it's JPEG), the saved image loses quality and dimensions.
Anyone have this problem?
OutputStream outStream = null;
File file = new File(this.photosPath, this.photoTakedAltered);
try {
outStream = new FileOutputStream(file);
this.photoFilteredBitmap.compress(Bitmap.CompressFormat.JPEG, 100, outStream);
outStream.flush();
outStream.close();

Related

how can we store a bitmap image onto the mobile memory?

my bitmap image in the bitmap variable whose name is imageFile how we further give name this bitmap image and store it in my mobile memory
in this picture a function show which receive image file and convert it into bitmap
1- you have to find the path to where you wanna save your image.
2- open an OutputStream object and give it the file path.
3- save your bitmap to the output stream.
4- flush.
5- close.
// Get environment dir
String path = Environment.getExternalStorageDirectory().toString();
OutputStream fOut = null;
File file = new File(path, "MyImage"+".jpg"); // the File to save to
fOut = new FileOutputStream(file);
Bitmap pictureBitmap = BitmapFactory.decodeFile(imageFile.getAbsolutePath(), optons); // obtaining the Bitmap
pictureBitmap.compress(Bitmap.CompressFormat.JPEG, 100, fOut); // saving the Bitmap to a file compressed as a JPEG with 85% compression rate
fOut.flush(); // Not really required
fOut.close(); // do not forget to close the stream

Compress image after capturing from camera and storing it at given path

I'm working on an android app which needs a photo to be taken from camera and compress it and then store it to the given path.I'm a beginner in android ,so I'll be thankful for any kind of help .I'm able to take the picture and store it on sdcard ,but can't find a way to compress it .I want to upload the image to server so want to compress the image as low as 50kb.I tried many solutions on the internet but none could do the thing .
You can refer this link to achieve image compression.
I have implemented this code in my project and able to compress an image by maintaining quality.
FileOutputStream out = null;
String filename = getFilename();
try {
out = new FileOutputStream(filename);
scaledBitmap.compress(Bitmap.CompressFormat.JPEG, 80, out);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
You can change number 80 as per your requirements.
This number indicates quality of the compressed image.
quality must be in the range 0-100.
0 meaning compress for small size, 100 meaning compress for max quality.
Thanks
You can use bitmap compression 0 = MAX compression and 100 = Least Compression. I hope this will help you
public Uri getImageUri(Context inContext, Bitmap inImage) {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
inImage.compress(Bitmap.CompressFormat.WEBP, 0, bytes);
String path = MediaStore.Images.Media.insertImage(inContext.getContentResolver(), inImage, "Title", null);
return Uri.parse(path);
}

bitmap.compress destroys the picture quality

I'm using an app to get the gps location and draw it as a circle on a bitmap then save it to proceed, so I need repetitively to read and save the file. But unfortunately when I save the file and read it, the file is damaged after some iterations...! the code:
File output = new File(tmpDirectory, "map.jpg");
try {
OutputStream outputStream = new FileOutputStream(output);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, outputStream);
outputStream.flush();
outputStream.close();
} catch (Exception ex) {
Message("error!");
}
directory = tmpDirectory;//updating directory to load the manipulated image
readFile(directory + "map.jpg", false);//setting the image view new image
image included:picture after iterations
image included:
main image
JPEG uses lossy compression. That means with each iteration you will lose some quality. You should use loseless format like PNG if you want to preserve it.
bitmap.compress(Bitmap.CompressFormat.PNG, 100, outputStream);

Android: Saving a bitmap Image

I have a button that allows the user to save a bitmap image to his/her gallery in a seperate folder after applying modifications on some of pixel bits as shown below
Random rand= new Random(TimeZone.LONG);
File root=null;
File file = null;
try{
root= Environment.getExternalStorageDirectory();
file = new File(String.format("%s/IMG_%d.png", root.getCanonicalPath(), Math.abs(rand.nextInt()%100000)));
FileOutputStream out = new FileOutputStream(file);
btmImg.compress(Bitmap.CompressFormat.PNG, 100, out);
MediaStore.Images.Media.insertImage(getContentResolver(), file.getCanonicalPath(), file.getName(), file.getName());
out.flush();
out.close();
Toast.makeText(getApplicationContext(), "File is Saved in " + file, 2000).show();
}
The problem is that when I press 'save' button. The image that appears in the user's gallery is not the modified version it is a duplication of the original or something like that. To get the modified version I had to refresh the gallery (by deleting the applications running in the background).
Anyone has an idea about how to solve this !!

android : save svg file from web and save on a file

i want to save a svg file from web to a file and then show it from file. i use this code to save a png file :
OutputStream fos = null;
File file = new File(getApplicationContext().getCacheDir(),FilenameUtils.getBaseName(url.toString())+FilenameUtils.getExtension(url.toString()));
Bitmap bm = ((BitmapDrawable) drawable).getBitmap();
fos = new FileOutputStream(file);
BufferedOutputStream bos = new BufferedOutputStream(fos);
bm.compress(Bitmap.CompressFormat.PNG, 50, bos);
bos.flush();
bos.close();
what should i do for svg file ?
In theory, you should be able to do something like the following:
PictureDrawable pd = (PictureDrawable) imageView.getPicture();
Picture picture = pd.getPicture();
picture.writeToStream(os);
However you should not do this. writeToStream() is deprecated (as is createFromStream()). I presume the reason is that the format of a Picture may change in the future and any saved pictures may no longer load. If you are just using it for temporary caching while the app is running, then that may be okay.
But it would be better, as #greenapps says, to cache the original SVGs.

Categories

Resources