Android: write bytes to image file on SD Card -
i'm trying create image file on sd-card, building bytes server sending towards me after calling web-service (basically: download file). managed "something" on client-side, , try write bytes file, using:
fileoutputstream fout = null; bufferedoutputstream bos = null; try { fout = new fileoutputstream(returnedfile); bos = new bufferedoutputstream(fout); bos.write(bytestowrite); } catch (filenotfoundexception e) { e.printstacktrace(); } catch (exception e) { e.printstacktrace(); } { try { if (bos != null) { bos.close(); fout.close(); } } catch (ioexception e) { e.printstacktrace(); } }
but image file broken (its size > 0kb, broken). ended opening file on computer text editor, , saw of initial file data (before being sent), differs final one. i'm guessing there kind of encoding misstakening or that. appreciate idea of how make work ( download image file web server, , open in on phone). ps. can change or info server configuration, configured friend of mine. ps2. should able not download images, kind of file.
i think it's better encode image base64 in server, example in php can :
$type = pathinfo($path, pathinfo_extension); $data = file_get_contents($path); $base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
and in android decode base64 string image file:
fileoutputstream fos = null; try { if (base64imagedata != null) { fos = context.openfileoutput("imagename.png", context.mode_private); byte[] decodedstring = android.util.base64.decode(base64imagedata, android.util.base64.default); fos.write(decodedstring); fos.flush(); fos.close(); } } catch (exception e) { } { if (fos != null) { fos = null; } }
Comments
Post a Comment