"Turn" Drawable/bitmap, String/inputstream, bitmap/byte[]

Source: Internet
Author: User

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[]

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.