I encounter problem with .decodeStream function.
File cacheMap = new File(context.getCacheDir(), "test.GIF");
if(cacheMap.exist()){
FileInputStream is = new FileInputStream(cacheMap);
Bitmap bitmap = BitmapFactory.decodeStream(is);
puzzle.add(test);
}else{
//retrieved from server and cache the image
}
The decode function always return null value. And from the internet, I found that in order to display the image, the Bitmap is always set to an ImageView. But what I am doing here is storing it into a ArrayList of Bitmap and display it later on in a Canvas. Can anyone tell me how to get back the image after decoding it?
From this page, it states the following,
public static Bitmap decodeStream (InputStream is)
Decode an input stream into a bitmap. If the input stream is null, or cannot be used to decode a bitmap, the function returns null. The stream's position will be where ever it was after the encoded data was read.
Parameters: The input stream that holds the raw data to be decoded into a bitmap.
Returns:The decoded bitmap, or null if the image data could not be decoded."
Does it means that the image that was cached previously cannot be use to decode it even though I had run the few line of code to cached the image?
cacheMap.createNewFile();
fos = new FileOutputStream(cacheMap);
bitmap.compress(CompressFormat.JPEG, 100, fos);
fos.flush();
fos.close();
Does anyone know what is wrong with my code? i have been tackling this issue for days and is driving me crazy!
Related
So in my app I have possibility to take photo. What I want to do later is to sent this photo to server using API as Base64 String. Here's my code to encode bitmap (photo) to String :
public static String convertPhotoToBase(Bitmap bmp) {
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
Log.e(TAG, Base64.encodeToString(byteArray, Base64.DEFAULT));
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
Looks ok, right ? Well, it's not and I don't why String I'm receiving is not encoded correctly. I add, that bitmap is ok , cuz I can display it in ImageView without any problems.
String I'm getting looks like this :
iVBORw0KGgoAAAANSUhEUgAAA8AAAAZACAIAAAA5PLyzAAAAA3NCSVQICAjb4U/gAAAgAElEQVR4 nKS9WbbsOK4kagZ5DON9VM1/hncL9j7QEKTkHnFXKXOd8K2GBNETBEH+3//z/0mSBIgkAJpIfmhx kXQ6ABEkP/YPyPjT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FerC3OahmIwLq/qKbBiSRgfCqlDaA d+VwkH7jtoJyDjO1wpfJHtIXtOECjsYbyUMfPmcvs71XlutHWnO5Jdfn5zKHSM6ckAn5qT8HUBxs L+UaS6N/5q5weDXTvwbAyy6aFjssbX+QPhcHlBokCfrAz/Zn3fJG5o4wrijpxPAPV2XTZq/spOF4 nMxf/DG83qWlW4sVL23j+g3Vtyu/zen9i9hK747ZZM7nh7ufrwPg1DkPrJL8XCCoXVTj1Z6xZXJz ZJzduoObJtOY1eTV12pOzE01aMNyqYHKqQrewtLbN1ZeFHTiukbt4eWbpUnIZ3PyrTvz8EhR6Wkx MVQThjW/nOoy1mSWJurZV0yhciJjXrGuwT0ZipjCncah/o9dSDzn+hxKO9VKdEcFu8BwvbgPy9XI WRRBRQYaO0s3P7ohDl22IRZlA10DfkEU4JFAqXsZb5d4U3RCMNGxhwY7373lk7AtedeowazItYIX n7LV0Bx8RHQv2AUJMmWgzuWd/oslJwT9loNkBmOuRfr9h3YJ70Hxi40vmQGASjlFO0AigysCaYHq UHOSaACXt9dDDgZlJXClyxBszo4tmGKJp
It looks like correct Base64 string but if you check it - it's not. Do you have any idea why is that ? Is it bitmap issue ,or what ? Any help, please ?
use this method to encode the bitmap you want
public static String enBitmap64(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
bitmap.recycle();
byte[] array = stream.toByteArray();
return Base64.encodeToString(array, 0);
}
and this method to decode the string
public static Bitmap deBitmap64(String string) {
byte[] decodedString = Base64.decode(string, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
return decodedByte;
}
However, before using encode64 you need to know few things
Encode and decode bitmaps needs to be done in background (NOT IN THE MAIN THREAD) it hangs the UI specially if the bitmap was large, use Asynctask, Service or Runnable (Asynctask is best choice).
It's not recommended to encode large images and upload it as string to server, encode and decode only in two cases.
First, if all you images are small.
Second, if you have large images and somewhere you app you display it as thumbnail For exp; In friends search of Facebook you see small images(thumbnail) you click it becomes large(original image). so there is small image and large image, when you are in friends search page you only request(download) the small image (low resolution) from the server no need to request(download) such large image(high resolution), but the two images are stored in the server. the small one could be saved as encoded but NOT the large one, large images is to be upload to server though HttpClient, Volley or HttpURLConnection.. there are more
I have almost the same issue,
encoding and decoding were working perfectly on the java side.
But my Base64 string couldn't be decoded on the server side.
comparing different base64s helped me to see that my base64 break into lines so using following code helped to correct base64
base64string.replace("\n", ""));
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%.
I have came across but not really sure how each line of codes really do. I hope that I can enlightened on what each line of codes mean.
URL url = new URL(urlPath);
stream = url.openConnection().getInputStream();
Bitmap image = BitmapFactory.decodeStream(stream);
fos = new FileOutputStream(output.getPath());
image.compress(Bitmap.CompressFormat.JPEG, 5, fos);
Thanks!
Create an URL object with url is the urlPath
Connect to the URL and get an InputStream from the connection
Decode the stream to a Bitmap image
Create a FileOutStream for write file
Compress the image with parameter: {format: JPEG, quality: 5(which mean low in scale 0-100), outstream}
So, the purpose of these code lines is to compress an image from the URL.
I'm trying to create a bitmap that is compressed.
I'm trying to use the bitmaps compress methed to save a compress bitmap in a Outputstream, then use decode stream to create a bitmap object from the Output stream.
My code has the following errors
1. methed toByteArray is undefined for OutputStream
2. Cannot initate the type outputstream.
My code
inputStream=assetManager.open(sb.toString());
Bitmap b=BitmapFactory.decodeStream(inputStream);
// this line produces a error saying cannot imitate OutputSream
OutputStream out=new OutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100,out);
// this ;ine has a error saying method does not exist
Bitmap decoded = BitmapFactory.decodeStream( new ByteArrayInputStream(out.toByteArray()) );
Try to change your OutputStream to this:
ByteArrayOutputStream out= new ByteArrayOutputStream();
There is no toByteArray() in the OutputStream.
In the compress method of the inputstream
ByteArrayOutputStream will still be accepted cause it is a subclass of the `OutputStream`
Here's the thing that's causing a bit of headscratching, maybe someone can shed some light on the situation. I'm using the Camera intent to snap a picture (well, any number of pictures really), like so:
ImageView imgPhoto = (ImageView)findViewById(R.id.imgButtonPhoto);
imgPhoto.setBackgroundColor(Color.rgb(71,117,255));
imgPhoto.setOnClickListener(new View.OnClickListener()
{
#Override
public void onClick(View v)
{
++snapNumber;
Intent intent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
lastPicSaved = String.valueOf(gd.getDeliveryId()) + "_" + String.valueOf(snapNumber) + ".jpg";
Uri imageUri = Uri.fromFile(new File(Environment.getExternalStorageDirectory(), lastPicSaved));
intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
startActivityForResult(intent, GooseConsts.IMAGE_CAPTURE_INTENT);
}
});
Once the activity has finished I snag the result like so:
case GooseConsts.IMAGE_CAPTURE_INTENT:
try
{
String newCompressedImage = Environment.getExternalStorageDirectory() + "/" + lastPicSaved;
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 4;
//options.inDensity = DisplayMetrics.DENSITY_MEDIUM;
Bitmap b = BitmapFactory.decodeFile(newCompressedImage, options);
FileOutputStream fos;
fos = new FileOutputStream(newCompressedImage);
b.compress(CompressFormat.JPEG, 60, fos);
fos.flush();
fos.close();
Image i = new Image();
i.setReported(0);
i.setReportedFull(0);
i.setImage(newCompressedImage);
//i.setImageData(b);
dbHelper.insertImageReference(i, gd.getDeliveryId());
}
Simple stuff really. As you can see I'm using the options.inSampleSize and reducing quality upon compression, to reduce the size of the end image, so as to maintain a small image capture to send back to hq, via an XMPP packet.
Here comes the fun part!
On the filesystem this resulting image is around the 50Kb size, possibly a bit more but never more than 60Kb. Which is fine, that sends via XMPP and I can process and display it in a custom connected client I've also written.
I figured it'd probably be best to keep the images, just in case sending fails for whatever reason, but didn't want them getting lost in the file system, so added a BLOB field to my local device database. I wondered if I could just send them directly from said DB and do away with the file system completely, so I tried it, suddenly NO images were being sent/received by my client bot. Strange! After a bit of digging, I noticed that the images that had been saved into the db BLOB are now (amazingly) 3x the size of the original. Same dimensions (486x684) and same quality (as I've adb pulled a few to test against the ones stored on the SD card).
Can anyone tell me why this is the case? I've been using BLOB fields for years and have never seen such a dramatic increase in file size before. A couple of Kb here and there, sure, but not jumping from 50(ish)Kb to over 160Kb?!
Many thanks.
After you compress the image convert the Image to a byte array instead of using a blob
Bitmap b = BitmapFactory.decodeFile(newCompressedImage, options);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 60, stream);
byte [] byteArray = stream.toByteArray();
This should keep the file size to a minimum. You will have to convert the byte array back to a bitmap for display, of course. But that should be straight forward.
I believe byte array's have a size limit though. Initialize it instead as
byte [] byteArray = new byte [1024];
// then
byteArray = stream.toByteArray();