I'm going to convert file to base64 so I send file and convert it to bitmap and when i want to compress it, it give me error null point exception
this is what everything that i did.
public static String getFileToByte(String path){
Bitmap bm = null;
ByteArrayOutputStream baos = null;
byte[] b = null;
String encodeString = null;
try{
bm = BitmapFactory.decodeFile(path);
baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
b = baos.toByteArray();
encodeString = Base64.encodeToString(b, Base64.DEFAULT);
}catch (Exception e){
e.printStackTrace();
}
return encodeString;
}
I got error on this error:
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos);
and pass:
getFileToByte(image.getAbsolutePath());
Do not convert the file to a bitmap first. Your bitmap is null as there is not enough memory to construct a bitmap for that image file with big resolution.
Instead you should directly base64 encode the bytes of the file.
Then your code is the same for all kind of files too.
Related
I make canvas view which I want to send in server using retrofit library. I get canvas view in bitmap format. Now I want to convert bitmap to file format so that I can upload in server in file format.
File file = new File(context.getCacheDir(), filename);
file.createNewFile();
/* Convert bitmap to byte array */
Bitmap bitmap = bitmap; //bitmap is your bitmap file which you want to convert
ByteArrayOutputStream bos = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.PNG, 0 , bos);
byte[] bitmapdata = bos.toByteArray();
/* write the bytes in file */
FileOutputStream fos = new FileOutputStream(file);
fos.write(bitmapdata);
fos.flush();
fos.close();
Always close and flush the FileOutputStream.
Convert ua bitmap to base64 String like below and send it using string
public static String bitmapToBase64(Bitmap image, Bitmap.CompressFormat compressFormat, int quality)//u can pass 100 in quality or any integer
{
ByteArrayOutputStream byteArrayOS = new ByteArrayOutputStream();
image.compress(compressFormat, quality, byteArrayOS);
return Base64.encodeToString(byteArrayOS.toByteArray(), Base64.DEFAULT);
}
I need to do this encoding so that in the end I decode to the original file again, could anyone help?
You can use commons.apache.org/proper/commons-codec/ for encoding and decoding.
import org.apache.commons.codec.binary.Base64;
byte[] encodedBytes = Base64.encodeBase64("YourResursesAsString".getBytes());
System.out.println("encodedBytes " + new String(encodedBytes));
byte[] decodedBytes = Base64.decodeBase64(encodedBytes);
System.out.println("decodedBytes " + new String(decodedBytes));
Try this to encode image:
private void generateBase64StringFromImage() {
Bitmap bm = BitmapFactory.decodeResource(getResources(), R.drawable.menu_circle);
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, 100, baos); //bm is the bitmap object
byte[] b = baos.toByteArray();
String encodedImage = Base64.encodeToString(b, Base64.DEFAULT);
Log.e("KEY2== ", encodedImage);
}
I am fetching the image (datatype is image) from the MS SQL database and the format i am getting is as follows.
0xFFD8FFE000104A46494600010101000000000000FFE100B24578696600004D4D002A00000008000187690004000000010000001A000000000001928600070000007C0000002C00000000554E49434F44450000430052004500410054004F0052003A002000670064002D006A007000650067002000760031002E003000200028007500730069006E006700200049004A00470020004A00500045004700200076003600320029002C0020007100750061006C0069007400790020003D0020003100300030000A0000FFDB00430001010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101FFDB00430101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101010101FFC00011080064006403012200021101031101FFC4001F0000010501010101010100000000000000000102030405060708090A0BFFC400B5100002010303020403050504040000017D01020300041105122131410613516107227114328191A1082342B1C11552D1F02433627282090A161718191A25262728292A3435363738393A434445464748494A535455565758595A636465666768696A737475767778797A838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE1E2E3E4E5E6E7E8E9EAF1F2F3F4F5F6F7F8F9FAFFC4001F0100030101010101010101010000000000000102030405060708090A0BFFC400B51100020102040403040705040400010277000102031104052131061241510761711322328108144291A1B1C109233352F0156272D10A162434E125F11718191A262728292A35363738393A434445464748494A535455565758595A636465666768696A737475767778797A82838485868788898A92939495969798999AA2A3A4A5A6A7A8A9AAB2B3B4B5B6B7B8B9BAC2C3C4C5C6C7C8C9CAD2D3D4D5D6D7D8D9DAE2E3E4E5E6E7E8E9EAF2F3F4F5F6F7F8F9FAFFDA000C03010002110311003F00FEFE28AF20F8DDF1C7E167ECE3F0B3C61F1A7E34F8D74DF01FC35F01E99FDABE25F136AC67782D2079E1B4B4B5B6B4B3B7B9D4353D5753BFB8B5D3748D1F4CB4BBD4F56D4EEAD74FD3ED2E2EEE2289FF0002750FF83A6FFE09CD677D77696DE05FDAC756B7B79E4861D4F4FF0086DF0DE2B2BF8D1B6A5D5A47AAFC63D37524826187896FB4FB3BA0AC04D6D13E507D1E49C21C4FC494AB57C8B22CC734A187A8A956AF85C3CE7469D57153F64EABE5A6EAA84A3395352738C65094A2A328B7E4E639EE4F94CE9D2CCB32C260EA558B9D3A75AAA8D4941371E7505792839271526945B5249B69A5FD2AD15FCCF7FC4549FF0004EDFF00A269FB5EFF00E1B9F851FF00CFC3DC7E747FC4549FF04EDFFA269FB5EFFE1B9F851FFCFC3DC7E75EE7FC42CF113FE890CE7FF09D7FF2679DFEBA70AFFD0F701FF835FF00F227F4C3457F3D9F05FF00E0E4BFD857E3B7C53F03FC22F067C39FDAAEDFC4BE3CD76D741D32F359F877F0E5347B092E773CBA8EAD268FF17B59D522D36C6DE396EAF24B0D2B50BA58227305A4F26D8DBB4F177FC1C2BFB0EF853F697D63F65887C29FB4578B7C7DA37C536F8413EBBE11F03781F51F055CF8B2DF5C4F0EEA2F63A85EFC4DD335DB8D2749D5BED36F7F77FF0008E24E16C2EE5B5B3BA8D6169BCFABC03C6543135F075B87732A58AC2E5EF35C4D19D28C67432E552749E32ADE76850F694AAC54E4D5DD3A8D26A126BAA9F13641568D3C453CD7093A35B14B054AA466DC6A629C613F610F76F2A9CB3837149D94E37B732BFEEFD15F85BFB59FFC1C13FB107EC77F1DFC61FB3CFC44F0DFED03E2BF1BF816DFC392788AFF00E1CF837C09AC786AD2EBC4BE1BD2BC556BA5ADFF0088BE26F857517D4EC34CD6AC5357B76D2238EC6FDA6B2334935BCC13E6FF00F88A93FE09DBFF0044D3F6BCFF00C375F0A3FF009F87B8AE8C2786DC778FC2E1B1D83E16CDF1184C650A589C357861FDCAD87AF4E3568D583724DC2A53946716D2BC5A7D4CABF16F0D61AB55C3D7CE7034ABD0A93A35A9CAAFBD4EAD39384E12B2694A124E3257D1A68FE98A8AFE67BFE22A4FF8276FFD134FDAF7FF000DCFC28FFE7E1EE3F3A3FE22A4FF008276FF00D134FDAF7FF0DCFC28FF00E7E1EE3F3AE8FF008859E227FD1219CFFE13AFFE4CCBFD74E15FFA1EE03FF06BFF00E44FE9868AFE703C27FF000744FF00C1373C47AFE9BA36ADA1FED35E04D3EFEE2382E3C55E2CF865E11B9F0FE908EC17ED5A943E0BF897E2FF0012B5B26774874BF0EEA7385195818E01FD7EF883FB707ECF7E01F817E17FDA321F183F8EFE1678D750D2B4EF0B6BBE005B2D617579357D3351D6E09D5F50BED1ECB4FB4B6D1F49D4750BE9B57BDD3CDB476DF66D8FA85C5AD9CFF3F9E70B711F0D3A0B3EC9B30CA962B9FEAF3C661E74E9D674EDED234EAEB4E7382945CE0A4E718CA329452945BF532ECE72ACDD54796E6185C6FB1E5F6B1A156329D352BF2B9C349C632B351938F2C9A6936D34BEC1A2B80F875F10344F89BE0DD17C6FE1F1AA5B69DAC25EC6DA7EB564FA6EB5A3EA9A46A579A26BDA0EB360C64169AC681AEE9BA8E8BAA4114D716E97F61702DAEAEADFCAB894AF00F4CFE73FFE0EA6D5B53B0FF827CFC29D3ECEF2E2D6CF5CFDAC7C0B67ABDBC333A47A8D9DAFC2DF8CBAA5BDB5DAA9C4D6F16A363657CB0B864175696D3ED3241132FF009FB57F7F3FF0758E7FE181FE09FAFF00C35D7833B7FD51EF8E1EE7F9D7F019F37B75F4FD7EF74FD7DABFBCFE8FAA2BC39C33E5B3966D9AB9356577ED69C6EF4777CB14AFD925D0FE6AF141B7C575AEDBB607049792E493B2ECAEDBF56DF521AFAB3F643FD8FF00E2D7ED97F14B4EF86BF0BF4B99D4CB6EFE21F11CB6D2CFA6F87AC279362CB308CA1B9BDB8DB20B1B049237B868E47965B7B68A6B88FE5E820B8BA9E0B5B689E7B9BA9E2B6B686252D24D3CEEB143146A1B25E49195100E4B1031CD7FA52FFC1127F607F0E7ECA5FB30F847C43AB69168FF00113C6B630EBFAF6A52408D70DA86A10C725CBACAC049E5C3F258D9F20C7676B0AF24B31F47C60F1127C0591508E5AA12CFB389D6A196BAAA3529E129508C1E2B1F3A528F2D4743DAD1A74294D3A73AF5A339C6A52A5569CB978138563C4B995478AE6596E0234EAE2F91B8CABCEA392A3858CD59C154E4A92A938BE68D3A7251719CE135F12787BFE0993FB367FC1227F645F8AFFB65F8B74FB6F12FC5DF841F0DB52D7BC37AD788BCABDBBBEF88DAA430E85E04D10BC8A2DB4EB3D73C6DAB685A4DD5A6956F029B4B890DC25C323337E0C7FC1BFF00FB36EA5FB4F7FC1402C7E25F8BA3B9D7B47F8511DFFC44F11EA9A8EEBA9350F166B73DD2D9DC5E4F3061717572CFABDE4F231327DA9A1998866527F64BFE0EB2FDAA3FE11FF865F007F63CD035211EA3F10B5EBDF8D7F11AD6DE5D9347E12F06B5CF877C07617B186FDF69FE20F156A1E22D59508F92FF00C056B2E41500FE73FF00C111FF00E0AB3FB0DFFC13B3E19F8BB40F8CBE14F8E17BF137E2378AC5DEBBE2BF067827C25AEF85B4AD0E1115A5846D7777E3FD27C473436D6D043717705A787EE25599AE4DAC572CEAB27E4391E038BB13E12711E7B42866DC43C53E2063638175B9AA62F194F20C2CEB50AB292A92738D09A8E674A9D3A5CB0E4C661BD95271513EE331C4E454B8E32ACB6AD4C16579370C5078854DA851C3CF32AD1A5562972AE59548B7849CE52BCB9B0F579E57BB3FB04FDA33FE0985FB177ED4BAF4FE28F8B3F047C17A9788AF04CF7FADE9BE1BF0EE95AB6A77772DBEE352D5354B3D2A2D4B53D5267C17D42FAF27BA2AA8A2550A31FCFF00FEDC5FF06BE783355D0759F1A7EC4FE31B9F0E78B2CADE7BC87E1778DAF67BBF0DEBCF1A193EC3A5EBD3B5D5FE897D3B656092E25B9D3436C8459DBAB99E3FDE7FDABFFE0A95FB2DFEC8FF00B2E7C36FDAEBC59A878ABE217C2AF8BFAC786B47F86D0FC30D2F48D4BC4FE2A7F14687AAF892D6FACF4AF157883C236F6F63A7E8FA35F5CEB0F7FA85A5C69F2886C9EDDAF6E2181BF2B7FE22AEFF00827E9201F839FB5E004E327C09F0848EB8EDF1C8F4FCEBF32E0AA3E2E61A34F37E11A3C435B0942BCF0D284673AD9755A986718D5C2D7C0E2AAFB0AAA9E94A49D2E6A4EF1A73A752178FD8710CF81AB39E073D9E554EBD4A51AAA4E31A78B842B6B0AD4F13461ED20E5A497BFCB356738CA0ECFF00839F89BF0C7C7FF067C7BE28F85FF14BC29ACF81FC7DE0CD567D17C4DE17D7ED1ECF53D2AFEDF69292C6D949ADE789E3BAB2BDB679ACEFECA682F6CA79ED2E219A4E12BFB4DFF82C47C38FD9A7FE0AA1FB05DAFF00C1517F6438AF2E3C73F0299B4BF89D6F7FA09D03C5FAB7C33B5BF8ACFC47A178DB48496EA36D7FE185DDFDBF8AB4CD56DF51BFD3BFE1127F14A5B5FEA703E96D69FC5C727D3F2EDEBF7BA7EBED5FDA7C07C5AB8C323FAE623073CBB38C062ABE559F657514E13C066D84E5588A5C9557B58D39C650AD4A352F38466E8CE52A94A6CFE7EE25C8FF00B0B31FABD2AEB1781C4D1A78DCB71917171C560ABA6E94F9A1EE39C5A9426E368C9C5548C6309C510D7F7F3FF06F0F85F41F8C5FB00E99E0AF891A79F14681A1F8B753D634086E2FB51B1BFF000F6ADA67893C416B63A9F87B59D2EEEC758D0350B6B29EE2C12FB47BFB2BA3A75D5E69CD2B595E5D4137F019F37B75F4FD7EF74FD7DABFD04FFE0D96CFFC3171F5FEDDF11F6FFA9AB5BED9FEB5F967D25547FD4AC9DDB55C518549BB3693CA7386D2764D26E316ECF5B2BEC7D97848DFFAC38E5776792D76D746D63B2FB3B775776ED77DCFE8E3C35E1AD0BC1DA0691E16F0B68D63A0F87740B0B7D3347D1F4B852D2C34EB0B5411C16D6D0478544451924E5E472D248CF23BB12B7B9F51F97FF65457F131FD0C7F313FF0758E7FE181BE0A7393FF000D75E0CC718FF9A3DF1BFB71FD2BF80BCB7EBE83B76FBDEC73DFAD7F7E7FF07587FC9837C14E73FF001975E0CE49CFFCD1EF8DFDC8FE8457F017BBFDA1D71D47FF0011D3DFA57F79FD1FBFE4DCE13FEC6B9AFF00E9E89FCD3E282FF8CAEB7FD8160BFF00486FB3EDFD6EBE8FFD903C2D6BE36FDA97F67FF0BDFA24967ABFC58F0645711C814C724706B36D76D1B824865736FB1948208241C8E2BFD6D7C1DA35B7877C27E1CD12CD560B6D2F45D36CE28D1004510DA4484018039604E3AE4D7F9127ECE3E3D83E17FC7EF833F10AEE558EC7C1FF00133C1BAE6A3231188F4DB2D76C9F5290F0BC258FDA1B39C715FE983FF0533FDB0ACFF660FF00826B7C61FDA03C3DABADB789BC43F0C6C3C23F09EEEDEE156E1FC7BF152CADFC37E17D4B4DC32F9D3F86C6AD73E319235704D8F872F1FEEA1AFCC7E909956639B71A7066030B094FFB57031CB7009A97B378EAB9A4A9D6D5276E58E27072AAD2F761CB296891F61E16E370B81E1FCFF135A518FD4B10F1789774A4B0F0C1A953EDBCA8D75056D657495DD8FE197FE0A01F13758FF829A7FC15CFC6F63E12B99B59F0C7887E2D68FF00027E1A3D9B9B8823F86DF0EEF0F86A5D6F4B20B15B1D75EC7C4DE3F45E9E6F8826384DDB4731FF0005333A1FC64FF828327ECE3F04B47D32DBC35F081BC01FB29785A2D0ED218A1D4FC53E1378F49F881AE5DADAAAC7733C7F10752F13E9E6FC190CDA1683A5B34BB2140BDDFF00C121B4AD37E0F2FED3FF00B7FF008B2CADAE342FD917E0F6B5A8F821752891F4ED5BE3278B123D0FE1EE8E4C8163325DF8A752F0DE953A8DCEB69AF4D2EC75520EE7FC1103E15699F137F6BFF17FED37F18EF67BAF873FB32783BC67F1EFE2778A35406EB75F5969DAB6BBA86A97B3CE42DCDD358DA6BFA84E5E4F31EEDE090B6E9013FA36331787E1CAB9DE618287B4CBBC31E13C270A6434B979D62B89F39A3839CD4631BC675D52591612AD94EA29E3715CD76E48F94C3E1EAE6B0CBB0B887CB8AE30CEEB67799CEEA2E964F80A95A306DBDA9B9FF6962217E54D61E85B4516767FF05CDF1F41E15F15FEC9FF00F04FDF065E4F73E1AFD8EBE07F876DBC41A7433190BFC59F8AFA7E91AF5FD84F0C64ADCCBA47826D7C0F1D9170D35A5DEB9ADDB2246D34DE6F89FF00C148F42F0FFECEDFB3FF00EC3FFB1AE9561610F8EF40F86B77FB40FC6ED42DE289AFA6F16FC52B992DBC1FE1EBD986D9229B43D274FF00126B82D98B79BA7F8D748949C471D7977ECF9E2BD03F6D3FF829ADAFC63FDA27C75E0BF877E19F887F1B75EF8D3E3DD73E2078B344F0C786345D363D726F1069DE1A8F57F11DEE9F606D74C53A5685A669E26691F49B0105BC0E9090BE8DE00B6D53FE0A99FF0005725D5ADACEE6E7C27F12FE348D6ECEC278DFFE24BF057E1EBD9697E0AD16F6165FDD476BE07F0FF863C3B77811A35CCF249B11A42B5584C0D3E178E4183C729BCAFC37E13C6F15E79887774B1DC519AD2C5528423525EE55AF04F3DC54129549C6A63305F6A5062AF899672F32AF867158CE2CCEF0D9265D4B453C364F829D09C9B82D614E4FFB3694AEA319468622F64A47F653FF000494FD88F4FF00877FF04A9B6F82DE35D34477DFB43FC37F1A4FE3FB2BB8B394F8A3E1DD4B4BBCB2B98DC7CC2D6C75692C8160331411E146315FE6BACB246CD1B0C3231461C1C321DACB9DDCE0820FE3D2BFD773E3BFC41F0DFECBBFB2CFC58F897712DBE9BE1EF821F05FC5FE26B746758A36FF008437C257D77A658C29F2892EB52BDB3B4B0B2810992E6F6EA08230D2CAA0FF009106E2792D927A927924F5272A4F27A9F5EF5F31F474C6E639AD5E3FCE31D294D6679AE5D8B94DDF9258FAEF35C463B92F7B3E5AF85E657D22E9A7A247B1E2AE1F0B82870C6030E945E0F058AA118E9CCB0D4BEA34B0FCDA77A55ACFF9B9BAEA2E5BF5F41DBB7DEF639EFD6BFD04FF00E0D96CFF00C3169EC7FB77C47DB3FF003356B7DB9FEB5FE7D7BBFDA1D71D47FF0011D3DFA57FA0A7FC1B2FFF00265A79FF0098EF88B9CE3FE66AD6FB81FD3F2AF4FE929FF244E4FF00F654E13FF5539C9C5E11AB71163BFEC4B5FF00F5372EF25FD7DCBFA55F9BFBC7FEF8A293FE05FF008FFF00F63457F131FD0E7F317FF0758FFC9837C14E00FF008CBAF06718383FF167BE37F6C03FCEBF80BFF80AFF00DF2DEFFECFD3F5F5E3FD07BFE0E93F057893C4DFF04EDF046BDA269D3EA1A77C3FFDA73E1FF89BC573408D20D2741D43C09F147C1D6FA9DC155C25AFFC245E27D074C776C013EA76FD98E3FCF7F6B7A7EA3FC6BFBC7E8F7529CFC3BA118CE32952CDF348548A69BA7394E954519A577193A7521349D9B8CA2D68D5FF009AFC51849715546D4929E070728369A528A8CA2DA7D52942516D697525BA638FFBA3FEF96F7FF67E9FE4F1FDD0FF00C1233E337ECD9FF0567FD8C3C29FB0E7ED75A7D8F8C7E257ECC9A8697AEE93E1AD6EF8C573E31F0B68BA76A9E1FF000BF8DB4C46612EA177A0699AF5E787B5F11892E2CE7B886EE7105B6AB6E24FE1736B7A7EA3FC6BD17E137C58F897F02BE21F85FE2BFC22F196B9E01F887E0CD4A2D57C37E29F0FDDFD9751D3EEA3051D183092DEF2C6EE0692D351D32FA0B9D3B53B19A7B1D42D6E6D279A17FB2F10B8221C6D93430F43193CB33ACB6B3C6E479AD2955A753098B70E4A94E7528B8D68E1B154FF007759D2973C251A55E31A92A31A73F0385B8865C3D8F955A94238CCBF170587CC705514671AF41494A338C6A274DD6A325CF4F9D72CAF3A6DC554728FFA80691FF049AFD88343F81BE2FF00D9D34EF843A6DBFC26F1CF89744F16F88FC32924A90DF6B9E1E9D2E34CBC92445495DA09A2864C48D20DD042463CA4DBF889FF0005A4F85FFB32FF00C12D3F600F18FC23FD983C2169F0F7C67FB6B78C349F879AAC3637321BD9BC03E1216BE28F887A94464FDEB5ADC5B59F867C15AA440BA3D9F8C8AED5DCED567FE09EFF00F0735E81F14753F87FF057F6BFF843AFE8FF00133C4BAC689E0FD37E2B7C1EB5B5D63C1FE20D4F54BA82C20D57C57E08D4B50B1D6BC1EA19C5C6AB75E1BBDF175A3379F736DA368F66AB6B17E40FFC1C3FFB4B5DFED63FF0517B0F82FE00BB935EF0BFC07D0BC35F07BC2B6B6AED25AEA9F127C693D97887C6F776D1B012457CDAA6ADE1EF04EA31B468CB75E0A5564DCA49FE65E07E07E30C1F88983CAB8BEA63A8E5D96CABF17660ABE633C565D8F8E5D529AC363E5255EA50AEE798AC239CF11155FD9D2A8AA4632A7251FD7B88F88B22C470AD7C6E430C3CF158C54F22C2BA5848D1C5E19E2A2DD6C2A5ECE356972E17DB28C69374F9E707072524DFB8FF00C110BFE08D3F05BF6E9F827E3FF8CBFB45E9DE233A3B78B1742F87CFA36B7AC68824B4D2A278357B965D36F6C96ED67D48CF6E5A612F93F60431141349BBFA97FD8A7FE0903FB20FEC27E36D47E227C1CF0C6A4DE31BFB17D3975CD7F53D4F5ABDB5B390832416B3EAB777D2DB452300D2476AF0472BA4524C923C51327C51F097FE0A1BFF0004EDFF008242FC05F869FB1E7C5AF89DADDAFC63F875F0EFC21ADF8CFC09E0DF871E34F126B2753F17787ECBC496F2DE6AF69A343E0EB7D5356B0D42D7557B3BDF135B5CC51DFC32DCAC29728C7F21BF6F4FF83A03E23FC50F0FEB7F0DBF61DF87FAD7C15D1B5782E34FBEF8D5F10A6D26F3E2A1B0B8568A5FF8437C2BA45CEADE19F036A0D1E5535FBBD77C5FA9C514E67D2A0F0FEAD6D6FA847863B2CF163C51CE332A9825C4385E14CEB1B2C4E0E199E331D80E1E8650EA46597D58E1AB4E347154D50850AFFEC586C44AAD66EBC60E52735AE1B19C13C1B97E12188FECBAD9DE5F8754ABBC250C3E2B34963942D8A84AB538CAA5193AB29D3BD7AD4D429DA9B95959FD15FF0007307FC14C7C389E135FF8278FC1BF105B6ABE21D5F52D1BC47FB4B6ADA4DD2CD6BE1ED2746BAB7D6FC27F0AA59E06656D7355D620D37C57E2BB50D1CBA3E9FA4E81A65C79EDAEEA76B61FC557FC057FEF96F7FF0067E9FAFAF17754D4B55D7353D475AD6B51BFD6359D62FAEF53D5B56D52F27D4353D5352BFB892EEFB51D46FEEE59AEAF6FAF6EA596E6EEEEE6596E2E6E249269A4791D98D0DADE9FA8FF001AFEAFE06E0EC070370F61722C0CFDB4A1296271D8C9439278EC7D65055F1328272E48F2C21468D3E693A787A34A129D4945CE7F89F11E7B89E23CD6B665895ECD4946961F0F1939470F86A6DBA74949A5CCEEE53A93B2E7A952A4946316A3177FC057FEF96F7FF67E9FAFAF1FE829FF0006CB7FC9969E07FC877C47C60E3FE46AD6FB633C7D3F2AFF003E92ADCF1DB9E4741F8D7FA1AFFC1B65E16D77C37FB16587F6D69F71A79D4AEF50D56CD678DE2792C756D7754D46C260AEA08171657305C2640CC722938CD7E4BF4959C170664B4DCE3ED27C4F879C6175CF2853CAB368CE4A3BB8C25569C64D5D45CE29FC51BFDBF847097FAC18F9A52708E4F563295AE94A58DC038C5BD939284DA5D7964D6CCFE8D79FEEAFFDF2DFE1453B69EE4FE07FFAC3FF00AFD68AFE283FA14E07E26FC33F04FC60F0178A3E1A7C44D0B4DF147833C63A54FA3EBFA16AB6D05E586A16371B4BC53DB5C24B0CAA1955C2C88CBB946411C57E1B6B3FF06E07FC13E751D4EF2FADBC217F610DD4F24EB676FAF789A0B7B712316F2A086DB5D8A08A25CE1238A28E340004455000FE813E4E3EF75F6FD78E9F9F7E2BC2BE1BFC703F117E2C7C7BF854DF087E35F8157E046B3E05D1D7E247C41F04C5E1EF85DF18FF00E138F09A78A8EA7F03FC52BAA5EBF8EB4AF0816FF8477C6D74FA7E93FD87E26034C58EEF2661EA65D9E67793AAAB29CE334CAD567175965D9862F04AAB8E9175561AB52551C536A2E57B5F438B179765F8F7078EC060F18E9DFD9BC56168621C2FAB5075A13E54EDAA8DAFD4FC5DFF00886D3F6013FF0032E6A9FF0085178B07F2F10D27FC4365FB007FD0B9AAFF00E149E2EFFE68ABFA18F938FBDD7DBF5E3A7E7DF8A3E4E3EF75F6FD78E9F9F7E2BD3FF5DB8CFF00E8AEE27FFC3FE6BFFCD671FF00ABBC3FFF00423C9FFF000DB83FFE53E5F9F767E167C27FF8200FEC51F06BE22F84FE27783343BCB5F13783B558B56D2A7B9D575ED42249911E2901B5D4B58BBB4632412CB1077819A22FE6445245571D29FF008214FEC691FED1577FB49DBE83A80F1BDD7C52BFF8B2CD7BAE788B5484F88F53F104FE23BC9658753D72EEDEE4C9A85D4F22BDC432149196650254465FDAEF938FBDD7DBF5E3A7E7DF8A3E4E3EF75F6FD78E9F9F7E2B96A71471355A95EB55E22CF6AD5C4E156071356A66F984EA623049D592C1D79CB10E5570AA55AB4961EA39524EAD57C97A92BF44327CA29C29D3A795E5D0851ADF59A5086070D1852C45A0BDBD38C69250AD6A74D7B58A53B420B9BDD8DBF1E3F6A3FF008221FEC41FB547C53D77E30F8C7E1F2D8F8E7C59709A878BF5EB4F12F8E1EFBC49A9C36B069F6F7576B278ACD959C167A659D8E9D61A7E976763616767670C105BC71C68A3E6FF00F886D3F601FF00A16F52FF00C28BC5FF00FCD257F42FF271F7BF21FAF1D3F3EFC51F271F7BAFB7EBC74FCFBF15B51E31E2EC3D2A5428714F11D0A1429C28D1A3473CCCE9D2A34A9C5429D2A54E18A8C29D3A708A842108A8C22946292491954C8723AB52756AE4D9554A95272A952A4F2FC24E752737CD39CE52A2E5294A4DCA52936E4DB6DB6DDFF9E7FF00886D3F602FFA16B53FFC28BC5BFD7C4B47FC4369FB00F7F0D6ABF41E23F1601FAF88C9FD6BFA18F938FBDD7DBF5E3A7E7DF8A3E4E3EF75F6FD78E9F9F7E2B4FF005DB8CFFE8AEE27FF00C3FE6BFF00CD64FF00ABBC3FFF00423C9FFF000D982FFE527F3FFA17FC1B99FB03683ABD86B16FE129AE66B0B88EE628753D4B5DD5AC99E321809EC354D66F2C6E5720109716D2A640CA9AFDAAF83FF06FC1BF053C2569E0FF000659C565A6DAA469F224685C448113E540AAAAAA30AA0600E98E94C5F8E9F089FE39C9FB342F8E74A3F1D61F85317C7197E1B6CBCFEDD8FE13CFE2E97C070F8DD9FEC9FD9C7497F17412E84145F1BCFB5A126D043FBD3EB3F271F7BAFB7EBC74FCFBF15E4E639C66F9BCE9D4CDB34CC7349D18CA346798E3B138D9D28C9A728D39626AD5708C9A4E4A2D26D26D3B23B70980C0E02328E070584C1C66D39C70B87A38753695939AA50829349B49CAED5D86D1FDF1FA7F8D140D9EA47D403FD0D15E71D63B737F74FF009FC2BF2E7F63AF84DFB407823FE0A05FF0563F88DF13344F14D8FC1AF8C5F10BF64BD4BF674D5358D7EDF52F0F6B7A2F83BF66FD33C31F11E6F0968F16AD7D37876DECBC7315C5A6AF0DCE9BA336A3A92B5F451DEC6C2E8FEA77969FDD147969FDD1401F951FF05A5F837F1EBF680FF82737C6CF84DFB3468DE31D73E30F8A3C55F001F40D37C05E293E0BF15CDA2E85FB44FC2AF10F8E65D2FC4C35BF0DCBA57D8FC09A57892F2EE5B7D66CAEA5B1B7B9B6B569AE268E09789FF825A7ECB3F12FF643F8ADFF000519F86527847C65E0BFD97352FDA7BC33E33FD91343F1378E755F1DE943C17E20F83DE0C3F11AF7C277DE24F14F8AFC55A769779F12ED35A96F74FD76F2D5CEADF6CBDB5B531DDBDC4FFB1BE5A7F7451E5A7F745007E68FFC1563E04FC72F8D5FB285FF00883F658BAD4ADBF6AAFD9CFE20F803F69CFD9DB4EB1D6E4D0ED3C6DF10FE0EEB69ADCFF0B7C44DFDA3A4D86ABE1CF8A9E109BC51E01BDD175DBF83C3F757BAF69B75AABC705809A2D8FF00825A7ECFFF0017BF678FD8D3E1DE91FB45EB3AB7887F699F8A7A9F8AFE3FFED25A86ADAC4BACC967F1A7E35EBD77E3AF16F856C67FB76A5616DA47C3E4D4F4FF00879A55A68974DA1A58F85619B4DDD0CFE6C9FA2DE5A7F7451E5A7F745007F369FF0005E6FD9B3F6B8F8F3E3CFD91356F869E15F8DBF157F658F0869DF1DACFE34FC27F803616DE25F1A9F8AFE24F0CE8B67F04BC79AAFC3FB9F8D5F0117C5DA468B75078874DD2BC403C7613E14EB7A8A78C5F49BD32C653F67FF624F027C55F85DFB23FECEDF0F7E3778ABC69E38F8ADE11F853E11D13C6DE25F88F7BA1EA9F106F357B4D3225FB2F8E758F0D6ABAFE83AEF8BB47B536DA3788FC43A5EBDADDA6BFABE9F79ACA6B3AA9BD6BFB8FAB3CB4FEE8A3CB4FEE8A00FE743FE0AD9FB07FC64FDB27F6ADF085CDA7C3BF889E39F811E1BFF8276FED93A3C11F85FE286BBE02F0F2FED57792F86F55FD9DADB52D37C3BE39F095DEB7AFA6B9653EA5E1C9357B4D47C3363A8D8DB5D6AB2412DB5A327EBAFEC15A37C64F0CFEC41FB1FF0086FF006878F5F4F8F5E1FF00D99BE06E87F19D3C53A9DBEB5E264F8A3A47C34F0D69FE3A5F116B36B77A8DBEADAE0F12DBEA5FDABAA43A85F47A85FF00DA2F05E5D79DE7BFD75E5A7F7451E5A7F745007E265CFEC4DE21D4BFE0BED6BFB6DDFF00C32F10C9F0B743FF00826F59781F43F8A89E32D52DF4187F6876F8D7E23D0EF3C332F862D7C550FDBA51F0475AB89C586A3E18B9F06C73DC47E208557C696D06A50FED5EE6FEE9FF003F8558F2D3FBA28F2D3FBA28020DCC7F87F5C7F31454FE5A7F7451400FA28A2800A28A2800A28A2800A28A2800A28A2800A28A2800A28A2803FFD9
I tried to convert it into bitmap image and i am getting error message. Can anyone please help me figure out the issue?
This is the code used for converting to image.
byte[] blob = string.getBytes();
Bitmap theImage = BitmapFactory.decodeByteArray(blob, 0, blob.length);
imageview.setImageBitmap(theImage);
**String is the data from image field of SQL database.
I Hope this will help,in imgByte you are getting a long String in format as shown by you
String imgByte ="your string in question";
try{
byte [] encodeByte= Base64.decode(imgByte,Base64.DEFAULT);
Bitmap bitmap= BitmapFactory.decodeByteArray(encodeByte, 0, encodeByte.length);
imageview.setImageBitmap(bitmap);
}catch(Exception e){
e.getMessage();
}
while Storing im encoding it
Bitmap photo = bitmap;
ByteArrayOutputStream bos = new ByteArrayOutputStream();
photo.compress(Bitmap.CompressFormat.PNG, 100, bos);
byte[] bArray = bos.toByteArray();
String encodedImage = Base64.encodeToString(bArray, Base64.DEFAULT);
db.upDateConatct(worldPopulation,worldPopulation.getRank(),encodedImage);
I have an Android application which sends an image to a web service. I want to send the same photo back from the web service to Android.
I made a test program to compare the base64 data that's sent from Android to the server and the base64 that's sent back from server to Android -- they are exactly equal.
I want to use the base 64 string to create a bitmap, so I tried this:
String image = client1.getBaseURI("restaurantFoods/OneFood/"
+ this.getID() + "/getImage");
byte[] decodedString = Base64.decode(image, Base64.DEFAULT);
Bitmap decodedByte = BitmapFactory.decodeByteArray(decodedString, 0,
decodedString.length);
if(decodedByte == null){
Log.d(this.getFoodItem().getName(), image);
Log.d("isNull", "Yes");
}
else{
Log.d("isNull", "No");}
I keep getting null because the log just prints "YES".
Can anyone please help?
If you want to know how I encode the image it is as follows:
private String getBase64(Bitmap bitmap) {
String imgString = Base64.encodeToString(getBytesFromBitmap(bitmap),
Base64.NO_WRAP);
return imgString;
}
private byte[] getBytesFromBitmap(Bitmap bitmap) {
ByteArrayOutputStream stream = new ByteArrayOutputStream();
bitmap.compress(CompressFormat.JPEG, 70, stream);
return stream.toByteArray();
}
Bitmap icon = BitmapFactory.decodeResource(this.getResources(),
R.drawable.pizza);
String iconBase64 = this.getBase64(icon);
Try this to bitmap;
public Bitmap convert(String img){
byte[] b = Base64.decode(img, Base64.DEFAULT);
return BitmapFactory.decodeByteArray(b, 0, b.length);
}
And this to String
public String convert(Bitmap bm, int quality){
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bm.compress(Bitmap.CompressFormat.JPEG, quality, baos);
byte[] byt = baos.toByteArray();
bm.recycle();
return Base64.encodeToString(byt, Base64.DEFAULT);
}
Really I don't see any real problems with your code, but these have worked for me so I suggest that you try them and see if that is actually your problem.
In my application I have faced a small issue in encoding and decoding images to String and pass it to web service.
After getting the bitmap image, I convert it into byte[] and encode to String value but in Some cases it shows error I don't know why it comes.
And one more doubt is Base64 class only supports to convert Bitmap image to String or any other tools available to do the same.
Thanks in advance...
In the case of OutOfMemoryError, below code helps me.
public String BitMapToString(Bitmap bitmap){
ByteArrayOutputStream baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,100, baos);
byte [] b=baos.toByteArray();
String temp=null;
try{
System.gc();
temp=Base64.encodeToString(b, Base64.DEFAULT);
}catch(Exception e){
e.printStackTrace();
}catch(OutOfMemoryError e){
baos=new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG,50, baos);
b=baos.toByteArray();
temp=Base64.encodeToString(b, Base64.DEFAULT);
Log.e("EWN", "Out of memory error catched");
}
return temp;
}
Basically what i did is : i catch OutofMemoryError and in that catch block i resize it by 50% and then i encode it to string.
Try My Below Sample Code Of Project
Bitmap bmp = (Bitmap) data.getExtras().get("data");
img.setImageBitmap(bmp);
btnadd.requestFocus();
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bmp.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] b = baos.toByteArray();
encodedImageString = Base64.encodeToString(b, Base64.DEFAULT);
byte[] bytarray = Base64.decode(encodedImageString, Base64.DEFAULT);
Bitmap bmimage = BitmapFactory.decodeByteArray(bytarray, 0,
bytarray.length);