I want convert my captured image into byte[]. When I capture image using camera it gets captured and preview is also shown and images saves on my external storage as well successfully.But when I try to convert my preview image it doesn't stores anything in the byte array.
Following is my method which is called when I press preview image button on my phone.
public static void previewCapturedImage() {
try {
static ByteArrayOutputStream stream = null;
imgPreview.setVisibility(View.VISIBLE);
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 8;
final Bitmap bitmap = BitmapFactory.decodeFile(fileUri.getPath(),options);
imgPreview.setImageBitmap(bitmap);
stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
} catch (NullPointerException e) {
e.printStackTrace();
}
}
convert image to string & byte array, use following short of code.
ByteArrayOutputStream baos = new ByteArrayOutputStream();
yourbitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
//this will convert image to byte[]
byte[] byteArrayImage = baos.toByteArray();
// this will convert byte[] to string
String encodedImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
Check the below working code. This includes quality adjustment also
/**
* #param bitmap
* #param quality 1 ~ 100
* #return
*/
public static byte[] compressBitmap(Bitmap bitmap, int quality)
{
try
{
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, quality, baos);
return baos.toByteArray();
} catch (Exception e)
{
PrintLog.print(TAG, e.toString(), e);
}
return null;
}
Related
It's my MySql DB
Its encoding:-
public String convertBitmapToString(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream); //compress to which format you want.
byte[] byte_arr = stream.toByteArray();
String imageStr = Base64.encodeToString(byte_arr, 1);
return imageStr;
}
This is decoding:-
String img=o.toString();
byte[] imageAsBytes = Base64.decode(img.getBytes(), Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Thank you.
It could be done by
private String getBase64String() {
// give your image file url in mCurrentPhotoPath
Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();
// In case you want to compress your image, here it's at 40%
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
And for Decode
private void decodeBase64AndSetImage(String completeImageData, ImageView imageView) {
// Incase you're storing into aws or other places where we have extension stored in the starting.
String imageDataBytes = completeImageData.substring(completeImageData.indexOf(",")+1);
InputStream stream = new ByteArrayInputStream(Base64.decode(imageDataBytes.getBytes(), Base64.DEFAULT));
Bitmap bitmap = BitmapFactory.decodeStream(stream);
imageView.setImageBitmap(bitmap);
}
i upload image to server with help of volley and bitmap and i successfully pass the data, but when i take the image using camera the image quality become so poor and also when i pass an image of size above 500kb the app become crash. Why this happen??
can anyone help me,
this is how my camera intent perform
private void onCaptureImageResult(Intent data) {
thumbnail = (Bitmap) data.getExtras().get("data");
File destination = new File(Environment.getExternalStorageDirectory(),
System.currentTimeMillis() + ".jpg");
FileOutputStream fo;
try {
destination.createNewFile();
fo = new FileOutputStream(destination);
//fo.write(bytes.toByteArray());
fo.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
if (thumbnail!=null){
addImageNew.setImageBitmap(thumbnail);
}
}
this is how my gallery intent perform
private void onSelectFromGalleryResult(Intent data) {
thumbnail=null;
if (data != null) {
try {
thumbnail = MediaStore.Images.Media.getBitmap(getApplicationContext().getContentResolver(), data.getData());
} catch (IOException e) {
e.printStackTrace();
}
}
if (thumbnail!=null){
addImageNew.setImageBitmap(thumbnail);
}
}
this how i convert Bitmap to string
public String getStringImage(Bitmap bmp){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 90, baos);
byte[] imageBytes = baos.toByteArray();
String encodedImage = Base64.encodeToString(imageBytes, Base64.DEFAULT);
return encodedImage;
}
NOTE: i have only problem in image quality and high size image passing
You can try to use below link solution. It may be work for you.
200kb image to base64 cannot send to web service
I didn't find where getStringImage(Bitmap bmp) is called, but you can try to do something like that:
BitmapFactory.Options options = new BitmapFactory.Options();
options.inSampleSize = 1;
Bitmap bitmap = BitmapFactory.decodeByteArray(data, 0, data.length, options);
getStringImage(bitmap);
Or maybe you can change the compress to 100, for high quality:
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
I need to send the image (from gallery) to server in the format of base64 string. I get the path of the image and converted it to base64 string. but at server the image is not showing fully. only 10% of the image is showing at server side.Please any one help me how to convert the image to base64 string.
code:
filePath = cursor.getString(columnIndex);
Bitmap bitmapOrg = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream bao = new ByteArrayOutputStream();
bitmapOrg.compress(CompressFormat.PNG, 100, bao);
byte [] ba = bao.toByteArray();
ba1 =Base64.encodeToString(ba, Base64.DEFAULT);
image size:460kb
try with this i hope it will work for you and send using httpPost method.
public static String BitmapToString(Bitmap bmp) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
String base64 = Base64.encodeToString(byteArray, Base64.DEFAULT);
return base64;
}
and server side.
Base64 base64=new Base64();
saveImage(base64.decode(profilePic),"yourName");
public void saveImage(byte[] bytes,String name){
String path="D:/"+name.hashCode()+".png";
try {
FileOutputStream f = new FileOutputStream(path);
f.write(bytes);
f.close();
} catch (Exception e) {
e.printStackTrace();
}
}
I have image's sd card path. Now what are the next steps to convert image into byte array because I want to upload the image to server?
Thanks in advance.
public static byte[] toByteArray (Bitmap raw) {
byte[] byteArray = null;
try {
ByteArrayOutputStream stream = new ByteArrayOutputStream ();
raw.compress (Bitmap.CompressFormat.JPEG, 100, stream);
byteArray = stream.toByteArray ();
}
catch (Exception e) {
e.printStackTrace ();
}
return byteArray;
}
Hi guys I wanted to ask you one thing, I have a chat that transfers strings and I can even attach of JPEG images before sending them to convert it into a string and then decode in BITMAP just that when I decode it crashes the app. I wanted to know if it is the right code to decode it.
NOME = (TextView) row.findViewById(R.id.comment);
NOME.setText(coment.comment);
String a = NOME.getText().toString();
if(a.length() > 1024 )
{
byte[] image = Base64.decode(a, 0);
int lung = a.length();
Bitmap bitmap = BitmapFactory.decodeByteArray(image, 0, lung);
Image = (ImageView) row.findViewById(R.id.image);
Image.setImageBitmap(bitmap);
}
The code looks fine, if I had to guess I would say you're getting the Out of Memory error, which is very common when loading images. Check out
http://developer.android.com/training/displaying-bitmaps/load-bitmap.html
for some best practices when loading images.
The method for Encoding an Image to String Base64 :
public static String encodeToString() {
String imageString = null;
try {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
imageString = Base64.encodeToString(b, Base64.DEFAULT);
} catch (Exception e) {
e.printStackTrace();
}
return imageString;
}
The method for Decoding String Base64 to Image :
public static void decodeToImage(String imageString) {
try {
byte[] imageByte = Base64.decode(imageString, Base64.DEFAULT);
Bitmap bm = BitmapFactory.decodeByteArray(imageByte, 0, imageByte.length);
image_view.setImageBitmap(bm);
} catch (Exception e) {
e.printStackTrace();
}
}