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%
// here i use JPEG image but now can anyone tell how can i convert any format image in String
bitmap.compress(Bitmap.CompressFormat.JPEG, 40, byteArrayOutputStream);
byte[] byteArray = byteArrayOutputStream.toByteArray();
return Base64.encodeToString(byteArray, Base64.DEFAULT);
}
Asked
Active
Viewed 52 times
-1
-
You have a file. Why would you inflate it into a bitmap and then compress it again? Not only is that wasteful in time and memory, but you're adding in compression issues. Just read the file in as a byte stream. – Gabe Sechan Sep 01 '18 at 05:19
-
Is this approach is not right to achieve the target ??? Can you please share some code , it is helpful for me to understand the concept – NICK Sep 01 '18 at 05:30
-
check this out https://stackoverflow.com/a/17874349/5148289 – V-rund Puro-hit Sep 01 '18 at 05:31
-
@V-rundPuro-hit yeah i check your link but still i cant find the result – NICK Sep 01 '18 at 06:42
1 Answers
0
You can use the Base64 Android class:
String ecodImage = Base64.encodeToString(byteArrayImage, Base64.DEFAULT);
You'll have to convert your image into a byte array though.Like:
Bitmap btm = BitmapFactory.decodeFile("/path/to/image.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
btm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //btm is the bitmap object
byte[] b = baos.toByteArray();
Vishal Sharma
- 1,049
- 2
- 7
- 15
-
yeah , I am doing the same but still not get the result , Actually its run perfectly for JPEG images but i want to make it feasible for all format . – NICK Sep 01 '18 at 06:40
-
so you want to convert jpeg and png images into string or jpeg, png, BMP, Gif all into a string – Vishal Sharma Sep 01 '18 at 06:59
-
i want to convert all image format [jpeg,png,bmp,gif] into string . Is there any specific method for that ??? – NICK Sep 01 '18 at 07:16