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);
Related
I select image from gallery, convert into Base64 and sends to server. For JPEG image, it works fine; the image I upload from gallery on server same gets shown in server folder. However, when I upload PNG format image from mobile gallery, it doesn't show same on server; instead it creates black edges around it. I really don't know what's going wrong?
Also, my actual image is as equal as given JPEG image.
Reference images:
JPEG:
PNG:
I just want to get rid of BLACK borders which should not appear for PNG format images.
Below is my code snippet
FileInputStream mFileInputStream = null;
try {
mFileInputStream = new FileInputStream(imagePathFromSDCard);
ByteArrayOutputStream bos = new ByteArrayOutputStream();
byte[] b = new byte[1024];
int bytesRead = 0;
while ((bytesRead = mFileInputStream.read(b)) != -1) {
bos.write(b, 0, bytesRead);
}
Bitmap bitmap = safeImageProcessing.decodeFile(uri);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] ba = bos.toByteArray();
String encodedImage = Base64.encodeToString(ba, Base64.NO_WRAP);
//this line sends image base64 to server & there i decode into original
new ImageAsync().sendImageProcess(getActivity(), encodedImage, this);
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
You want to upload image files. But you use BitmapFactory to make a Bitmap out of them first. Then you compress the bitmap to a jpg or png byte array. After that you base64 encode the bytes to a string which you upload.
What a waiste. And you have changed the files. Do away with the intermediate bitmap.
Instead load the files directly in a byte array. Then continue with the byte array as usual.
Having said that i think its a bad idea to base64 encode the bytes of a file first as it increases the amount of bytes that have to be transferred with 30%.
when a user is taking a photo I want to save it to background as a parse file as follows:
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, stream);
try {
stream.flush();
stream.close();
} catch (IOException e) {
e.printStackTrace();
}
filePic = new ParseFile(userName + ".png", byteArray);
filePic.saveInBackground();
This code works, but the problem is that the image loses its quality even though its set to 100.
Can anyone help with a solution on how to compress the bitmap without losing the quality? or maybe saving it without compressing?
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.
I am frustrated searching almost every google page for this problem.
I need to save my bitmap to file.
I have used this method several times with no problem at all. But now I am having a problem.
The Bitmap I am saving is a round image with transparency and having .png format which I have placed in res folder. But I am getting black portion in place of transparent portion.
This is the code I am using for saving the bitmap.
void saveImage(Bitmap bmp) {
try {
ByteArrayOutputStream bytes = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, bytes);
finalImg = new File(Environment.getExternalStorageDirectory(),
"emoticon_temp" + ".png");
finalImg.createNewFile();
FileOutputStream fo = new FileOutputStream(finalImg);
fo.write(bytes.toByteArray());
fo.flush();
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
I am saving this bitmap as file because I need to share this image and for sharing the image I need the Stream Uri.
If you got any other Idea for sharing a bitmap, please let me know.
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();