I am getting this image as a byte array. So I encode the bytearray and store it in db. Later decode this byte array and saved it in sd card. Issue is that, I am not able to load this image into image view. It tells, skia--> decoder decode false. The bytearray could not be converted to bitmap. Here is the code I used to convert the bytearray to bitmap
Infact, not even able to open the image in Imagebrowser in android device (I have tried in Es Image browser also). But, this image if I open with chrome browser or HTML viewer, it opens flawlessly.
byte[] byteArray= android.util.Base64.decode(in.getBytes(), android.util.Base64.DEFAULT);
if (byteArray != null) {
Log.d(Utilities.class.getSimpleName(), byteArray.toString() + "byteArray Length-->" + byteArray.length);
} else {
Log.d(Utilities.class.getSimpleName(), "byteArray is null");
}
Bitmap bitmap = BitmapFactory.decodeByteArray(byteArray, 0, byteArray.length);
Encoding code :
ByteArrayOutputStream baos = new ByteArrayOutputStream();
InputStream is = null;
try {
is = conn.getInputStream();
byte[] byteChunk = new byte[4096]; // Or whatever size you want to read in at a time.
int n;
while ( (n = is.read(byteChunk)) > 0 ) {
baos.write(byteChunk, 0, n);
}
}
catch (IOException e) {
System.err.printf ("Failed while reading bytes from %s: %s", url.toExternalForm(), e.getMessage());
e.printStackTrace ();
// Perform any other exception handling that's appropriate.
}
finally {
if (is != null) { is.close(); }
}
response.append(Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT));
Here is the base64 string which I get
Base64 string of image
Can anyone help me is sorting out this issue.
Related
I am trying to upload multiple images to my node server via Android Studio. However the image file always seems to be corrupt on the server. I am able to take and preview the picture in Android but not able to store it on my server.
Here is my server code
app.post('/upload', function(req, res) {
var base64Data = req.rawBody;
fs.writeFile("test.jpg",base64Data,'base64',function(err,written){
if(err) console.log(err);
else {
console.log("file Succesfully written ");
cloudinary.uploader.upload("test.jpg", function (image) {
if(image !== undefined) {
res.json({link: image.secure_url}).end();
console.log("url = " , image.secure_url);
// fs.unlink(ImageFile);
} else console.log.error("Error uploading to Cloudinary, ", image);
});
}
});
});
Here is my front end code I use to convert my bitmap to an encoded Base64 String.
final String encodedString = ImageBase64.encode(fileArray.get(i));
Here is a log of what encodedString holds after being set. Obviously the full value is not displayed.
/9j/4AAQSkZJRgABAQAAAQABAAD/2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB
AQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH/wAARCAMgA+gDASIA
AhEBAxEB/8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQFBgcICQoL/8QAtRAAAgEDAwIEAwUFBAQA
However on my backend it is displayed as this
2F9j%2F4AAQSkZJRgABAQAAAQABAAD%2F2wBDAAEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEB%0AAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH%2F2wBDAQEBAQEBAQEBAQEBAQEBAQEBAQEB%0AAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQEBAQH%2FwAARCAMgA%2BgDASIA%0AAhEBAxEB%2F8QAHwAAAQUBAQEBAQEAAAAAAAAAAAECAwQ
If you get it url encoded then you should url decode it.
InputStream imageStream;
try {
imageStream = getContentResolver().openInputStream(uri);//uri is the image URI
Bitmap bm = BitmapFactory.decodeStream(imageStream);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] byteArray = baos.toByteArray();
String encodedImage = Base64.encodeToString(byteArray,Base64.NO_WRAP);//encodedImage is your image string
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Hi I am working on application which gets .png files in byte stream from server. When I got it I try to make from it bmp and later convert it to .png file, but the following method ( Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length); ) returns me null.
Here is my code:
byte[] result
Bitmap img = BitmapFactory.decodeByteArray(result, 0, result.length);
try {
File filename = new File(imageUri.getPath()+name);
File parentFile = new File(imageUri.getPath());
parentFile.mkdirs();
FileOutputStream out = new FileOutputStream(filename);
img.compress(Bitmap.CompressFormat.PNG, 90, out);
} catch (FileNotFoundException e) {
Log.e("imageDownloaded", e.toString());
} catch (Exception e) {
Log.e("imageDownloaded", e.toString());
}
but the img Bitmap is always, null. I uploaded the image as multi part data and it was png file parsed to byte array, but now when i want to retrieve it i get this ugly null. Thanks for any help.
My Android-App reads image-files from the sd-card and stores the image in a blob in a sqlite database.
Currently i am converting a FileInputStream to a byte array and store this in the blob. A blob cannot exceed the size of 1MB, so in this case i am posting an error-message and cancel the operation.
FileInputStream fis = null;
try {
fis = new FileInputStream(FilePath); // FilePath contains a valid path
} catch (FileNotFoundException e) {
Toast.makeText(MyActivity.this, "File not found: " + FilePath, Toast.LENGTH_LONG).show();
return;
}
BufferedInputStream bis = new BufferedInputStream(fis, 1070);
ByteArrayBuffer bab = new ByteArrayBuffer(128);
int current = 0;
try {
while ((current = bis.read()) != -1) bab.append((byte) current);
} catch (IOException e) {
Toast.makeText(MyActivity.this, "Error reading Picture: " + FilePath, Toast.LENGTH_LONG).show();
return;
}
byte[] imageBa = bab.toByteArray();
if (imageBa.length > 1024*1024)
showErrorDialog();
else {
saveImageInDatabase(imageBa); // sage image byte[] in BLOB-column so sq-lite database
// show image in imageView
imageStream = new ByteArrayInputStream(imageBa);
Bitmap imageBitmap = BitmapFactory.decodeStream(imageStream); // uncompressed imageBitmap has a much bigger size than the image-byte[]
imageView.setImageBitmap(imageBitmap);
}
I want to get rid of the 1MB-limitation and store also bigger images, by reducing the resolution (not the size).
I could go for a solution using the BitmapFactory option inSampleSize to compress an image and / or convert the bitmap back to a byte[], e.g. using bitmap.compress.
However, even an uncompressed bitmap created with BitmapFactory has a much bigger size than the original byte [], so i fear that i lose quality.
Any ideas how to solve my issue? Many thanks in advance, Gerhard.
I am trying to download images from a remote server, the number of images downloaded is 30. The code i am using to download image is as below. Some images download successfully and some images don't download and raises the above exception. What might be the problem.
public static Bitmap loadBitmap(String url)
{
Bitmap bitmap = null;
InputStream in = null;
BufferedOutputStream out = null;
try {
in = new BufferedInputStream(new URL(url).openStream(), 4*1024);
final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
out = new BufferedOutputStream(dataStream, 4 * 1024);
int byte_;
while ((byte_ = in.read()) != -1)
out.write(byte_);
out.flush();
final byte[] data = dataStream.toByteArray();
BitmapFactory.Options options = new BitmapFactory.Options();
//options.inSampleSize = 1;
bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
} catch (IOException e) {
Log.e("","Could not load Bitmap from: " + url);
} finally {
try{
in.close();
out.close();
}catch( IOException e )
{
System.out.println(e);
}
}
return bitmap;
}
Please look on my this post
Image download code works for all image format, issues with PNG format rendering
In my case I solved this error with encode the url
Because image url that i wanted to download has the Persian letters(or other Unicode character) in it
So I replaced all Persian characters with encoded UTF-8 letters
I've got the URL of a .png image, that needs to be downloaded and set as a source of an ImageView. I'm a beginner so far, so there are a few things I don't understand:
1) Where do I store the file?
2) How do I set it to the ImageView in java code?
3) How to correctly override the AsyncTask methods?
Thanks in advance, will highly appreciate any kind of help.
I'm not sure you can explicity build a png from a download. However, here is what I use to download images and display them into Imageviews :
First, you download the image :
protected static byte[] imageByter(Context ctx, String strurl) {
try {
URL url = new URL(urlContactIcon + strurl);
InputStream is = (InputStream) url.getContent();
byte[] buffer = new byte[8192];
int bytesRead;
ByteArrayOutputStream output = new ByteArrayOutputStream();
while ((bytesRead = is.read(buffer)) != -1) {
output.write(buffer, 0, bytesRead);
}
return output.toByteArray();
} catch (MalformedURLException e) {
e.printStackTrace();
return null;
} catch (IOException e) {
e.printStackTrace();
return null;
}
}
And then, create a BitMap and associate it to the Imageview :
bytes = imagebyter(this, mUrl);
bm = BitmapFactory.decodeByteArray(bytes, 0, bytes.length);
yourImageview.setImageBitmap(bm);
And that's it.
EDIT
Actually, you can save the file by doing this :
File file = new File(fileName);
FileOutputStream fos = new FileOutputStream(file);
fos.write(imagebyter(this, mUrl));
fos.close();
You can explicity build a png from a download.
bm.compress(Bitmap.CompressFormat.PNG, 100, out);
100 is your compression (PNG's are generally lossless so 100%)
out is your FileOutputStream to the file you want to save the png to.