my code
String passenger_sign = assignedJobJson.getJSONObject(position).getString("passenger_sign");
Log.e(TAG, "passenger_sign: "+passenger_sign );
byte[] Bytedata = passenger_sign.getBytes();
Log.e(TAG, "Bytedata: "+Bytedata );
ByteArrayInputStream arrayInputStream = new ByteArrayInputStream(Bytedata);
Bitmap bitmap = BitmapFactory.decodeStream(arrayInputStream);
//Bitmap bmp = BitmapFactory.decodeByteArray(Bytedata, 0, Bytedata.length);
Log.e(TAG, "bitmap: "+bitmap );
problem
i want to convert byte array to bitmap but it showing only null value while converting to bitmap.
Assuming your passenger_sign contains the image data converted in Base64, you can apply the following code:
byte[] decodedString = Base64.decode(passenger_sign, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
// Now, we set the decoded bytes to an image view to check if conversion is successfull
imageView.setImageBitmap(decodedByte);
Related
This is my code. I am converting my Gallery image into base 64 string. this base 64 string is saving through this code in database but when i decode that string that gives me mime error. why this is happening?
if (resultCode == Activity.RESULT_OK) {
// Picasso.get().load(data.getData()).into(ivprofile);
filePath = data.getData().getPath();
Glide.with(this).load(data.getData()).into(ivprofile);
ivprofile.getDrawingCache();
Bitmap bitmap = ivprofile.getDrawingCache();
ByteArrayOutputStream stream=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] image=stream.toByteArray();
img_str = Base64.encodeToString(image, Base64.DEFAULT);
}
this is what i am getting after saving
0x2F396A2F34414151536B5A4A5267414241514141415141424141442F3277424441414D4341674D4341674D4441774D4541774D45425167464251514542516F484277594944416F4D4441734B0A4377734E44684951445134524467734C4542595145524D55465255564441385847425955474249554652542F3277424441514D454241554542516B4642516B554451734E46425155464251550A46425155464251554642515546425155464251554642515546425155464251554642515546425155464251554642515546425155464251554642542F7741415243414751415A4144415349410A41684542417845422F38514148774141415155424151454241514541414141414141414141414543417751464267634943516F4C2F38514174524141416745444177494541775546424151410A414146394151494441415152425249684D5545474531466842794A7846444B426B61454949304B78775256533066416B4D324A7967676B4B4668635947526F6C4A69636F4B536F304E5459330A4F446B3651305246526B644953557054564656575631685A576D4E6B5A575A6E61476C7163335231646E643465587144684957476834694A69704B546C4A57576C35695A6D714B6A704B576D0A7036697071724B7A744C57327437693575734C44784D584778386A4A79744C54314E585731396A5A32754869342B546C3575666F3665727838765030396662332B506E362F385141487745410A417745424151454241514542415141414141414141414543417751464267634943516F4C2F385141745245414167454342415144424163464241514141514A334141454341784545425345780A42684A425551646863524D694D6F454946454B526F62484243534D7A55764156596E4C524368596B4E4F456C3852635947526F6D4A7967704B6A55324E7A67354F6B4E4552555A4853456C4B0A55315256566C64595756706A5A47566D5A326870616E4E3064585A3365486C36676F4F456859614869496D4B6B704F556C5A61586D4A6D616F714F6B7061616E714B6D7173724F30746261330A754C6D367773504578636248794D6E4B3074505531646258324E6E613475506B3565626E364F6E7138765030396662332B506E362F396F4144414D4241414952417845415077443871714B4B0A39612B4250774B757669646666326871486D577668753366456B713850634D502B576165337133626F4F6569625356324E4B2B694F532B4833777238522F4571394D4F6A57526142446957390A6D4F79434C36743350734D6E3272365838452F736A2B4764466A6A6D312B6562587277594C52686A4441443742666D50346E6E3072313155305034662B476A6757326A614C597835503843520A722F556B2F556B6E7554587A72385266327670704A4A4C50776261434B4D456A2B3072354D7333756B66516656732F376F72486D6C50593235597833506F58522F4148686E772F477161646F0A476E5765332B4B4B31514D6671324D6E38545736694C47675246434B42674B6F77425835336139385576463369576379366A346931436335794557637878672B794C68522B41726D7070354C0A683930736A794E6A4735324A4E487333315965305332522B6D3946666D4C5252374C7A4432766B6670315258356930556579387739723548366455562B5974464873764D50612B522B6E56460A666D4C5252374C7A4432766B6670315258356930556579387739723548366455562B5974464873764D50612B522B6E5646666D4C5252374C7A4432766B6670315258356930556579387739720A3548366455562B5974464873764D50612B522B6E5646666D4C5252374C7A4432766B6670315258356930556579387739723548366455562B5974464873764D50612B522B6E5646666D4C52520A374C7A4432766B6670315258356930556579387739723548366455562B5974464873764D50612B522B6E5646666D4C5252374C7A4432766B66703152583569305565793877397235483664550A562B5974464873764D50612B522B6E5646666D4C5252374C7A4432766B667079364C49685231444B526771777944574672486744777A7238624A714F6761646562757253327146683947786B0A6667612F4F61476553336664464938545978755269445853364438557646336871667A644F385261684163354B4E4F5A497966644779702F4555657A6652683752645566546E6A54396B62770A78724D556B7567547A364464386C5979786D674A3977783344384734394B2B61506942384C5045587731766842724E6C746763346876495476676C2F33573748324F4437563756384F6632760A706F35493750786C6143574D6B41616C5970686C39336A364836726A2F644E6652544A6F667841384E444974745A30572B6A795034306B582B68422B68424859696A6D6C44634F574D396A380A33714B39612B4F76774B7576686A6648554E50387937384E3344346A6C626C37646A6E39322F74364E3336486E72354C57796161756A4671326A4F742B4666772B75666956347A737447684C0A5277452B6264546A2F6C6C43702B5A7672794150646858336F69364E385076436F4759744D305854494F7034574E462F6D542B5A4A376B3135482B7950344B54526641302B767A52347539570A6C4952694F5241684B67666932382B2F792B6C636A2B31393852576B756254776261534552786862752B4B6E71787A3561483644356A3956394B7866767973625239794E7A793734796647580A5576696A726367456B6C746F55446E374A5935774D644E372B7248394D3448636E7A6D6969746B72614978627671776F6F6F70694369696967416F6F6F6F414B4B4B4B414369696967416F6F0A6F6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B41436969690A67416F6F6F6F414B39472B4458786C314C3457363347444A4A6336464F342B31324F636A4854656E6F772F586F6578486E4E464A712B6A476E625648365173756A6645447771526D4C5539460A314F44714F566B52763545666D434F784666426678542B48317A384E664756376F73354D6B4B2F766257636A2F5777746E6133313449507570723272396B48346A504863336E6732386B4A6A0A6B4458646957503357482B736A4831487A41657A657464622B3178344B5457664130477678526733656B79674F34484A676B4F306A38473248322B6173592B354B78732F656A6339593841610A4F6E682F77526F476E497533374E5977787350566767334838546B2F6A58775838554E666C38546645507846715575637A587367554535495253555166677167562B694B4973534B69414B710A6A41413641562B5A4D3072547A5353763935324C484871546D696C75324654524A444B4B4B4B334D416F6F6F6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B414369696967416F6F6F0A6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B414369696967416F6F6F6F414B4B4B4B4143696969670A416F6F6F6F41366A345761394C34612B496E683355597335697659315941344A526A736366697245666A583370342B30644E663845612F707A72752B30324D30616A30596F32302F676347760A7A6D676D61336D6A6C5847354744445054494F612F545A305752475267475668676739434B7771614E4D3370624E43312B59746670315835693055756F5665675555555675594252525251410A555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142520A525251415555555541464646464142525252514155555555414646464641425252525141562B6E56666D4C5836645668563647394C7146666D4C583664562B5974464C7146586F46464646620A6D4155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646410A4252525251415555555541464646464142525252514155555555414646464641425252525141555555554146667031583569312B6E565956656876533668583569312B6E56666D4C525336680A563642525252573567464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646460A464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142583664562B59746670315746586F6230756F562B59746670310A5835693055756F5665675555555675594252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541460A46464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141562B6E56666D4C5836645668563647394C710A46666D4C583664562B5974464C7146586F46464646626D41555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555550A414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146667031583569312B6E560A5956656876533668583569312B6E56666D4C525336685636425252525735674646464641425252525141555555554146464646414252525251415555555541464646464142525252514155550A555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425836640A562B59746670315746586F6230756F562B59746670315835693055756F56656755555556755942525252514155555555414646464641425252525141555555554146464646414252525251410A555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142520A52525141562B6E56666D4C5836645668563647394C7146666D4C583664562B5974464C7146586F46464646626D415555555541464646464142525252514155555555414646464641425252520A514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646410A425252525141555555554146667031583569312B6E565956656876533668583569312B6E56666D4C5253366856364252525257356746464646414252525251415555555541464646464142520A525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646460A46414252525251415555555541464646464142583664562B59746670315746586F6230756F562B59746670315835693055756F566567555555567559425252525141555555554146464646410A425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541460A4646464142525252514155555555414646464641425252525141562B6E56666D4C5836645668563647394C7146666D4C583664562B5974464C7146586F46464646626D4155555555414646460A464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555550A41464646464142525252514155555555414646464641425252525141555555554146667031583569312B6E565956656876533668583569312B6E56666D4C52533668563642525252573567460A464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155550A5555414646464641425252525141555555554146464646414252525251415555555541464646464142583664562B59746670315746586F6230756F562B59746670315835693055756F5665670A555555567559425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251410A555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141562B6E56666D4C5836645668563647394C7146666D4C583664562B590A74464C7146586F46464646626D4155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252520A5141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146667031583569312B6E56595665687653366858350A69312B6E56666D4C52533668563642525252573567464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142520A525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142583664562B59746670315746580A6F6230756F562B59746670315835693055756F5665675555555675594252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646410A42525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141562B6E56666D0A4C5836645668563647394C7146666D4C583664562B5974464C7146586F46464646626D41555555554146464646414252525251415555555541464646464142525252514155555555414646460A464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555550A4146667031583569312B6E565956656876533668583569312B6E56666D4C525336685636425252525735674646464641425252525141555555554146464646414252525251415555555541460A464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155550A555541464646464142583664562B59746670315746586F6230756F562B59746670315835693055756F56656755555556755942525252514155555555414646464641425252525141555555550A414646464641425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251410A55555555414646464641425252525141562B6E56666D4C5836645668563647394C71496A7249697568444B7779434F6846666D544E453045306B542F41486B597163656F4F5036562B6A50670A445745385165434E41314647336661624747526A364E73473466676369766776346F61424A345A2B4966694C545A633568765A43704977536A4573682F4657422F47696C75304654564A6E4C0A305555567559425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251410A5555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251412B43467269614F4A63626E594B4D394D6B3472394E6E6459305A32490A5656475354304172383776685A6F4D76695834696548644F697A6D57396A5A6942794555373350344B704E66656E6A37574530447752722B6F753233374E597A534B6656676A62522B4A77500A78724370756B6230746D7A7966396B6278716D732B427039416C6B2F3076535A5355556E6B77794573442B44627837636574636C2B3137384F586A756254786C5A786B78794262532B436A370A7244506C7948366A3553665A665776466668583851626E34612B4D72505759415A49422B36756F416639624333336C2B76414939774B2B394562527669423456427846716569366E4230504B0A7949333869507A42485969682B354B3452392B4E6A3833714B39472B4D6E7761314C345861334952484A6336464F352B7958324D6A4839782F52682B754D6A7542357A5779643955597457300A595555555578425252525141555555554146464646414252525251415555555541464646464142525252514155555555414646464641425252525141555555554146464646414252525251410A55555555414646464641425252525141555555554146464646414252525251415555555541464646656A664272344E616C385574626A4A6A6B74744367636661373747426A2B346E71782F540A4F543242546474574E4B2B695055663251666879306C7865654D72794D694F4D4E615749596665592F7743736B483048796A3674365631763758486A564E47384451614246495074657253670A756F5049676A4F346E32793277652F7A5636367A614E38502F43704F49744D305854494F6734574E462F6D542B5A4A376B3138462F465034675850784B385A5875737A686F3457506C5773420A502B7168424F316672795366646A574D66666C633266755273636C587258774A2B4F7431384D62372B7A3951387937384E3344356B69586C37646A2F414D7445397656652F556339664A614B0A326154566D59703231522B6B4B746F667841384E484274746130572B6A77656A7049763943443943434F7846664F7678462F5A426D6A6B6B76504274324A59795366374F765877792B7953640A443947782F7647764666683938552F45587731766A506F7437746763356D73356876676C2F7742356338483347443731394C2B4376327550444773785278612F424E6F4E3377476B436D61410A6E324B6A635078586A3172486C6C445932356F7933506D50587668623475384E542B56715068335549446E4164594449682B6A726C542B42726D706F4A4C643973736278746A4F3131494E660A6F7A6F2F6A2F773134676A56394F312F54727A642F44466449574831584F522B49726452316B514F6A426C4979475535426F396F2B71443261657A507A476F72394F714B50612B516579387A0A3878614B2F5471696A32766B4873764D2F4D57697630366F6F39723542374C7A507A466F72394F714B50612B516579387A3878614B2F5471696A32766B4873764D2F4D57697630366F6F39720A3542374C7A507A466F72394F714B50612B516579387A3878614B2F5471696A32766B4873764D2F4D57697630366F6F39723542374C7A507A466F72394F714B50612B516579387A3878614B2F0A5471696A32766B4873764D2F4D57697630366F6F39723542374C7A507A466F72394F714B50612B516579387A3878614B2F5471696A32766B4873764D2F4D57697630366F6F39723542374C7A0A507A466F72394F714B50612B516579387A3878614B2F5471696A32766B4873764D2F4D57697630366F6F39723542374C7A507A466F72394F714B50612B516579387A387949594A4C683973550A6279746A4F31464A4E644C6F5077743858654A5A2F4B303777377145357A677530426A51665632776F2F45312B694C7573614633594B716A4A5A6A6743734C5750482F6872514932665564660A30367A322F77414D743067592F5263355034436A326A3649505A72717A35362B485037494D306B6B643534797578464743434E4F736E797A657A79644239467A2F7643766F6F746F66772F380A4E444A7474473057786A774F694A477639535439535365354E6552654E5032756644476A5279526142627A36396434777368557777412B3562356A2B43382B74664E4878412B4B66694C346B0A33776E316D394C516F637857634F5567692F33567A31397A6B2B39484C4B65346330596248572F485834363358784F766A702B6E2B5A61654737643878784E773977777A2B386632394637640A547A30386C6F6F725A4A4A5752693366566E2F2F32513D3D0A
follow the reference code in order to encode and decode image using base64 in android.
//encode image to base64 string
ByteArrayOutputStream baos = new ByteArrayOutputStream();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.logo);
bitmap.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] imageBytes = baos.toByteArray();
String imageString = Base64.encodeToString(imageBytes, Base64.DEFAULT);
//decode base64 string to image
imageBytes = Base64.decode(imageString, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0, imageBytes.length);
image.setImageBitmap(decodedImage);
You need to enable drawing cache before you can get bitmap from imageview. Use below code for it.
imageView.setDrawingCacheEnabled(true);
imageView.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED),
MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
imageView.layout(0, 0,
imageView.getMeasuredWidth(), imageView.getMeasuredHeight());
imageView.buildDrawingCache(true);
Bitmap bitmap = Bitmap.createBitmap(imageView.getDrawingCache());
imageView.setDrawingCacheEnabled(false);
or you can simply use this.
Bitmap bitmap = ((BitmapDrawable)image.getDrawable()).getBitmap();
You can do that in the following way:
Saving to db
byte[] data = text.getBytes("UTF-8");
String base64 = Base64.encodeToString(data, Base64.DEFAULT);
Receiving from db
byte[] data = Base64.decode(base64, Base64.DEFAULT);
String text = new String(data, "UTF-8");
For more information, check Base64
I receive byte string in response but whenever i trying to convert it in bitmap it giving me null value.I am using below code for converting to bitmap.Anyone Help me?
byte[] decodedString = Base64.decode(strImageid, Base64.NO_WRAP);
Bitmap decodedByte = BitmapFactory.decodeByteArray( decodedString, 0, decodedString.length);
holder.imageView.setImageBitmap(decodedByte);
it's working fine using glide getting bitmap images.
private void applyProfilePicture(EmployeeViewHolder holder, List<AllRestaurantList> dataList, final int position) {
if (!TextUtils.isEmpty(dataList.get(position).getImage())) {
String imageBytes = dataList.get(position).getImage();
byte[] imageByteArray = Base64.decode(imageBytes, Base64.DEFAULT);
Glide.with(context)
.load(imageByteArray)
.asBitmap()
.into(holder.ivResIcon);
/*Glide.with(context)
.load(dataList.get(position).getImage())
.thumbnail(0.5f)
.crossFade()
.diskCacheStrategy(DiskCacheStrategy.ALL)
.into(holder.ivResIcon);*/
// holder.img.setColorFilter(null);
//holder.iconText.setVisibility(View.GONE);
} else {
holder.ivResIcon.setImageResource(R.drawable.ic_perm_identity_gray_24dp);
//holder.imgProfile.setColorFilter(dataList.getColor());
// holder.iconText.setVisibility(View.VISIBLE);
}
}
As mentioned in the comment's link If you have converted your image to string by using Base64. You should convert it back with Base64.
Im using this snippet maybe it helps u too.
byte[] data = Base64.decode(iconString, Base64.DEFAULT);
Bitmap imageBitmap = BitmapFactory.decodeByteArray(data, 0, data.length, null); // null for options
imageView.setImageBitmap(imageBitmap);
I have the following dataurl
data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAARgAAADSCAMAAACmVC/YAAAAGXRFWHRTb2Z0d2FyZQBBZG9iZSBJbWFnZVJlYWR5ccllPAAAAyJpVFh0WE1MOmNvbS5hZG9iZS54bXAAAAAAADw/eHBhY2tldCBiZWdpbj0i77u/IiBpZD0iVzVNME1wQ2VoaUh6cmVTek5UY3prYzlkIj8+IDx4OnhtcG1ldGEgeG1sbnM6eD0iYWRvYmU6bnM6bWV0YS8iIHg6eG1wdGs9IkFkb2JlIFhNUCBDb3JlIDUuMC1jMDYwIDYxLjEzNDc3NywgMjAxMC8wMi8xMi0xNzozMjowMCAgICAgICAgIj4gPHJkZjpSREYgeG1sbnM6cmR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XK7irn8aqDIE+yInN3Z58t+v8cdEd7J/7X2wvs5JfyN7Vyfrv9Nmgl2RDHIEKOpCAhAMmj5+KJBGjCC26IPoIhCMBqiKhPFjRHI4BmwmMRgNmyENRiwMNkMZzHadkgGbCQzmSwyfz0PewRvMMx2+jshUJT2YY+jokHqIDcalQn14sspk7ZAaYTPBECmwlA02c9WbKAcjYTPzSJ2z6pPjn2sz00GS5F3O5+jMAAAAAAAAAAAAAAAAAAAAAADAL/n7nOs09bYCYvG3+WlXLNtCzWNnf0HspptVEEu08HuUhcCZy7tyeHRNnuhkhfSW3etH3vF4rP6ghFHJJW/JPeuNMhMX21ErX8q3exMXRXawqIi+KzJThu/FDW4LdZNk8/u9/OWXYmds1qWNj8aWd/75qxC1
is there a way to load the dataUrl in imageview?
First you need to convert the Base64 encoded string to a Bitmap.
Code snippet below from linked question:
byte[] decodedString = Base64.decode("Your Base64 String", Base64.DEFAULT);
Bitmap bitMap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
When that is done you have your bitmap in bitmap
Assuming your imageViewis defined as R.id.image_view
Then just do as you would with any other bitmap:
ImageView imageView;
imageView = (ImageView) findViewById(R.id.image_view);
imageView.setImageBitmap(bitmap);
Since I was getting the error:
java.lang.IllegalArgumentException: bad base-64
, I used the following workaround:
import java.io.ByteArrayInputStream
import java.io.InputStream
import android.util.Base64
...
val imageData: ByteArray = Base64.decode(dataUri.substring(dataUri.indexOf(",") + 1), Base64.DEFAULT)
val inputStream: InputStream = ByteArrayInputStream(imageData)
val bitmap: Bitmap = BitmapFactory.decodeStream(inputStream)
imageViewComponent.setImageBitmap(bitmap)
Perhaps it will help someone.
Just decode the Base64 image encoding using Base64.decode() method to receive a byte array, and then create the Bitmap using BitmapFactory.decodeByteArray() method.
If you take the Base64 part of the input, it can easily be converted to a Bitmap via this:
byte[] decodedString = Base64.decode("your base64 string", Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
get base64 string in String variable decode
String encodedImage;// set base64 string value in this string
byte[] decodedString = Base64.decode(encodedImage, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
I want to convert base64 encoded string into bitmap so i can put it in image view, but getting error like
D/skia(7490): --- decoder->decode returned false and bitmap returns null value
My code is:
byte[] imageAsBytes = Base64.decode(imageData);
image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Firts you have to check that the string you want to decode is vaild and has the intended value to be decoded and to do so, you can do something like below:
filePath= Environment.getExternalStorageDirectory()
+ "/SaudiScore/temporary_holder.jpg";
Bitmap selectedImage = BitmapFactory.decodeFile(filePath);
ByteArrayOutputStream stream = new ByteArrayOutputStream();
selectedImage.compress(Bitmap.CompressFormat.JPEG, 100, stream);
byte[] byteArray = stream.toByteArray();
String strBase64=Base64.encodeToString(byteArray, 0);
then you can decode the string that you just encoded and get the image back by doing something like the following:
byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);
byte[] decodedString = Base64.decode(mBase64string, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,decodedString.length);
mImageView.setImageBitmap(decodedByte);
String base = "Base64 string values of some image";
byte[] imageAsBytes = Base64.decode(base.getBytes(), Base64.DEFAULT);
ImageView image = (ImageView) this.findViewById(R.id.imageView1);
image.setImageBitmap(BitmapFactory.decodeByteArray(imageAsBytes, 0, imageAsBytes.length));
Try this code.
This method can help:
private void setExistImage(ImageView imageView, String base64String){
if (!base64String.isEmpty()) {
byte[] bytes = Base64.decode(base64String, Base64.DEFAULT);
imageView.setImageBitmap(BitmapFactory.decodeByteArray(bytes, 0, bytes.length));
}
}
Decode/Convert base64 string to image
imageBytes = Base64.decode(imageString, Base64.DEFAULT);
Bitmap decodedImage = BitmapFactory.decodeByteArray(imageBytes, 0,imageBytes.length);
image.setImageBitmap(decodedImage);
byte[] decodedString = Base64.decode(strBase64, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
image.setImageBitmap(decodedByte);
I was using the above solution .It always returned errors like decoded String is null,IllegalStateException ..
All I did was I just wrapped that in a try catch
I'm doing for testing purposes.
I have this image:
I encoded it to base 64 using this app and the following is the result:
Pz8/PwAQSkZJRgABAQEAYABgAAA/PwByRXhpZgAATU0AKgAAAAgAAT9pAAQAAAABAAAAGgAAAAAAAT8/AAIAAAA7AAAALAAAAABDUkVBVE9SOiBnZC1qcGVnIHYxLjAgKHVzaW5nIElKRyBKUEVHIHY2MiksIHF1YWxpdHkgPSAxMDAKAAAAAD8/AEMAAgEBAgEBAgICAgICAgIDBQMDAwMDBgQEAwUHBgcHBwYHBwgJCwkICAoIBwcKDQoKCwwMDAwHCQ4PDQwOCwwMDD8/AEMBAgICAwMDBgMDBgwIBwgMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDAwMDD8/ABEIACAANQMBIgACEQEDEQE/PwAfAAABBQEBAQEBAQAAAAAAAAAAAQIDBAUGBwgJCgs/PwA/EAACAQMDAgQDBQUEBAAAAX0BAgMABBEFEiExQQYTUWEHInEUMj8/PwgjQj8/FVI/PyQzYnI/CQoWFxgZGiUmJygpKjQ1Njc4OTpDREVGR0hJSlNUVVZXWFlaY2RlZmdoaWpzdHV2d3h5ej8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/Pz8/PwAfAQADAQEBAQEBAQEBAAAAAAAAAQIDBAUGBwgJCgs/PwA/EQACAQIEBAMEBwUEBAABAncAAQIDEQQFITEGEkFRB2FxEyIyPwgUQj8/Pz8JIzNSPxVicj8KFiQ0PyU/FxgZGiYnKCkqNTY3OD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View.setImageBitmap(bmp);
But all I'm getting is empty spots in my gridview! I wonder what is the problem?!
Try this,
byte[] decodedString;
decodedString = Base64.decode(s, Base64.DEFAULT);
bitmap = BitmapFactory.decodeByteArray(decodedString, 0, decodedString.length);
imgView.setImageBitmap(bitmap);
And let me know what happen...
String s is base64 string for image..