Original: http://wuxiaolong.me/2015/08/10/Drawable-to-Bitmap/Drawable bitmapdrawable turn Bitmap
1 2 3 4
|
Resources res = getresources (); drawable drawable = res.getdrawable (r.drawable.myimage); bitmapdrawable BD = (bitmapdrawable) D; Bitmap BM = Bd.getbitmap ();
|
1 2 3 4 5 6 7 8 9 10 11 12
|
Publicstatic Bitmap drawabletobitmap (drawable drawable) { Drawable.getintrinsicwidth (), Drawable.getintrinsicheight (), drawable.getopacity ()! = Pixelformat.opaque? Bitmap.Config.ARGB_8888 Bitmap.Config.RGB_565); Canvas canvas = new Canvas (bitmap); //canvas.setbitmap (bitmap); Drawable.setbounds (0, 0, drawable.getintrinsicwidth (), Drawable.getintrinsicheight ()); Drawable.draw (canvas); return bitmap; /span> |
Bitmap Turn drawable
1 2 3 4 5 6 7 8
|
XXX Based on your situation to obtain Bitmapdrawable bd=bitmapdrawable (BM); Btimapdrawable is a subclass of drawable, and ultimately directly uses the BD object.
Mpicpath//local picture path into bitmap format Bitmap pic = bitmapfactory.decodefile (this.mpicpath); Image.setimagebitmap (pic); Turn into bitmap format
|
String to InputStream mutually convert string to InputStream
1 2 3
|
"String and InputStream convert each other"; InputStream In_nocode = new Bytearrayinputstream (Str.getbytes ()); InputStream In_withcode = new Bytearrayinputstream (Str.getbytes ("UTF-8"));
|
InputStream to String
这里提供几个方法。
Method 1:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21st
|
Public StringConvertstreamtostring(InputStream is) { BufferedReader reader =New BufferedReader (New InputStreamReader (IS)); StringBuilder SB =New StringBuilder (); String line =Null try { while (line = Reader.readline ())! = null) { Sb.append (line + "/n "); } catch (IOException e) { E.printstacktrace (); finally { try { Span class= "line" > Is.close (); } catch (IOException e) { E.printstacktrace (); return sb.tostring (); } /span> |
Method 2:
1 2 3 4 5 6 7 8
|
public String inputstream2string (InputStream in) throws ioexception { new stringbuffer (); byte[] b = new byte[4096]; for (int n; (n = in.read (b))! =-1;) { Out.append (new String (b, 0, n)); return out.tostring (); } /span> |
Method 3:
1 2 3 4 5 6 7 8
|
Public Static String inputstream2string
int i=- while ((I=is.read ())!=-
}
|
Bitmap and byte[] Mutual transfer bitmap→byte[]
1 2 3 4
|
Byte[] Bitmap2bytes (Bitmap BM) { New Bytearrayoutputstream (); , BAOs); return Baos.tobytearray (); }
|
Byte[]→bitmap
1 2 3 4 5 6 7 8 |
Bytes2bimap(byte[] b) { if (b.length!=0) { 0, B.length); } else { Null } }
|
"Turn" Drawable/bitmap, String/inputstream, bitmap/byte[]