How can I convert all the images stored in one folder to base64 data? in the following code tFotos is the folder where all my images are stored.
ArrayList<String> mList = new ArrayList<String>();
Bitmap selectedImage = BitmapFactory.decodeFile( Environment.getExternalStorageDirectory()+"tFotos");
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100,
stream);
byte[] byteArray = stream.toByteArray();
String strBase64 = Base64.encodeToString(byteArray, 0);
mList.add(StrBase64);
#Ruben JG
You have to provide full path of image like sdcard/foldername/filename.PNG/.JPEG
Related
I'm trying to convert a PNG file into a Bitmap into a Byte[] so I can store the image in my SQL database.
Converting the PNG to a Bitmap.
BitmapFactory.Options options = new BitmapFactory.Options();
options.inJustDecodeBounds = true;
Bitmap b = BitmapFactory.decodeResource(getResources(), R.drawable.MyPicture, options)
Convert Bitmap to Byte[]
ByteArrayOutputStream stream = new ByteArrayOutputStream();
b.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] bArray = stream.toByteArray();
Insert into database
MainViewModel myViewModel = ViewModelProviders.of(this).get(MainViewModel.class);
MyEntity myEntity = new MyEntity(bArray);
myViewModel.insert(myEntity);
I'm Currently getting NullpointException, in the b.compress line of my code.
Thanks for any and all help.
your bitmap is null because you have set options.inJustDecodeBounds = true and passing this options variable to decodeResource() function. Read these docs
I'm trying to encode an animated gif to base64, so I can upload it to server. I am already doing it with static images, but if I try to do it with GIF, it simply does not work. I get the image from Intent image picker and put it inside an ImageView.
Here is the code I use to encode images to base64:
Bitmap bitmap = ((BitmapDrawable) ((ImageView) nextChild).getDrawable()).getBitmap();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 60, baos);
byte[] imginbyte = baos.toByteArray();
value = Base64.encodeToString(imginbyte, Base64.DEFAULT);
try this :
byte[] imginbyte = Base64.decodeBase64(bitmap.getImageBase64());
new FileOutputStream("image2.gif");
write(imginbyte);
close();
I need to convert an image file(large size to small size). So, I am decoding image file to bitmap image and then I compressed bitmap image. I need to save this bitmap into File object again. Can anyone help me?
first covert your bitmap to byte[] object by
Bitmap bmp; //your bitmap object
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.PNG, 100, stream);
byte[] byteArray = stream.toByteArray();
Then create a FileOutputStream Object and write the byte[] to the fileoutputstream
File yourOutputFile = new File("/the/path/to/your/file")
FileOutputStream fos = new FileOutputStream(yourOutputFile);
fos.write(byteArray);
fos.close();
return yourOutputFile;//this will be your output
I have some standard code to share an image in my Android app. The image exists on the storage and I provide an URI to the image. This all works fine.
However, this requires the WRITE_EXTERNAL_STORAGE permission. Is there a way I can share an image without the need of this permission, for example, to not save the image to storage, but specifying a memory stream or byte array?
Thanks!
You can convert an image file to a byte array with the following code, which I taken from an answer to a similar question: How to convert image into byte array and byte array to base64 String in android?
String filepath = "/sdcard/temp.png";
File imagefile = new File(filepath);
FileInputStream fis = null;
try {
fis = new FileInputStream(imagefile);
} catch (FileNotFoundException e) {
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100 , baos);
byte[] b = baos.toByteArray();
Optional step to encode in Base64
encImage = Base64.encodeToString(b, Base64.DEFAULT);
Bitmap bm = BitmapFactory.decodeResource(getResources(),R.drawable.image);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
final String encodedImage = Base64.encodeToString(baos.toByteArray(), Base64.DEFAULT);
This is my code. It uploads the image onto the server. However, All I can see is a box. Where am I going wrong?
Make sure you convert back to bitmap at the End, ie Server side
1, convert your imageview to bitmap.
imageView.buildDrawingCache();
Bitmap bmap = imageView.getDrawingCache();
2, convert bitmap to base64 String and pass to server
public String getEncoded64ImageStringFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
byte[] byteFormat = stream.toByteArray();
// get the base 64 string
String imgString = Base64.encodeToString(byteFormat, Base64.NO_WRAP);
return imgString;
}
3, At server side convert base64 to bitmap.
(this is java code, do it in your server side language)
byte[] decodedString = Base64.decode(Base64String.getBytes(), Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
set this bitmap to display image
convert your image to bitmap.
File imagefile = new File(Environment.getExternalStorageDirectory(),"/image/test.jpg" );
FileInputStream fis = null;
try {
fis = new FileInputStream(imagefile);
} catch (FileNotFoundException e) {
logger.error(Log.getStackTraceString(e));
e.printStackTrace();
}
Bitmap bm = BitmapFactory.decodeStream(fis);
Bitmap to Byte[]
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.PNG, 10 , baos);
byte[] img = baos.toByteArray();
Byte[] to String:
String s= Base64.encodeToString(img , Base64.DEFAULT)