public static byte[] Getimagefromnetbyurl (String strurl) {try {URL url = new URL (strurl); HttpURLConnection conn = (httpurlconnection) url.openconnection (); Conn.setrequestmethod ("GET"); Conn.setconnecttimeout (5 * 1000); InputStream instream = Conn.getinputstream ();//Get Picture data by the input stream byte[] btimg = Readinputstream (instream);//Get the image of the binary System data return btimg; } catch (Exception e) {e.printstacktrace (); } return null; }
Since the reading takes a certain amount of time, it cannot be read in a byte array, so it is necessary to determine whether to read public static byte[] Readinputstream (InputStream instream) throws exception{
Store all byte arrays read Bytearrayoutputstream OutStream = new Bytearrayoutputstream (); byte[] buffer = new byte[1024]; int len = 0; while ((Len=instream.read (buffer))! =-1) {outstream.write (buffer, 0, Len); } instream.close (); return Outstream.tobytearray (); } private static string Getimagestr (String imgurl) {byte[] data = null; try {data =getimagefromnetbyurl (imgurl);; } catch (Exception e) {e.printstacktrace (); } Base64encoder encoder = new Base64encoder (); return Encoder.encode (data); }
Read images from remote paths for Base64 transcoding