Sharedpreferences (3)-accessing images

Source: Internet
Author: User
Mainactivity is as follows:
Package CC. SP; import Java. io. bytearrayinputstream; import Java. io. bytearrayoutputstream; import android. OS. bundle; import android. util. base64; import android. view. view; import android. view. view. onclicklistener; import android. widget. button; import android. widget. imageview; import android. app. activity; import android. content. context; import android. content. sharedpreferences; import android. content. sharedpreferences. editor; import android. graphics. bitmap; import android. graphics. bitmap. compressformat; import android. graphics. bitmapfactory;/*** demo Description: * using sharedpreferences to access images *** references: * 1 http://blog.csdn.net/tangnengwu/article/details/37881087 * 2 http://blog.csdn.net/lfdfhl/article/details/37742775 * 3 http://blog.csdn.net/lfdfhl/article/details/17998469 * 4 http://blog.csdn.net/lfdfhl/article/details/10961459 * Thank you very much */public class mainactivity extends activity {private button msavebutton; private button mgetbutton; private imageview mimageview; @ overrideprotected void oncreate (bundle savedinstancestate) {super. oncreate (savedinstancestate); setcontentview (R. layout. main); Init ();} private void Init () {msavebutton = (button) findviewbyid (R. id. savebutton); msavebutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view) {savebitmaptosharedpreferences () ;}}); mgetbutton = (button) findviewbyid (R. id. getbutton); mgetbutton. setonclicklistener (New onclicklistener () {@ overridepublic void onclick (view) {getbitmapfromsharedpreferences () ;}}); mimageview = (imageview) findviewbyid (R. id. imageview);}/*** convert bitmap to a string and save it to sharedpreferences. ** Note: * do not select compressformat when compressing an image to the output stream. JPEG and PNG. Otherwise, the image has a black background. * For details, see reference 2 */private void savebitmaptosharedpreferences () {Bitmap bitmap = bitmapfactory. decoderesource (getresources (), R. drawable. ic_launcher); // Step 1: compress bitmap to bytearrayoutputstreambytearrayoutputstream bytearrayoutputstream (); bitmap. compress (compressformat. PNG, 80, bytearrayoutputstream); // Step 2: Convert the data in the byte array output stream to the string stringbyte [] bytearray = bytearrayoutputstream. tobytearray (); string imagestring = new string (base64.encodetostring (bytearray, base64.default); // Step 3: Keep the string to inclusharedpreferences = getsharedpreferences ("testsp", context. mode_private); Editor editor = sharedpreferences. edit (); Editor. putstring ("image", imagestring); Editor. commit ();}/*** retrieve bitmap */private void getbitmapfromsharedpreferences () {sharedpreferences = getsharedpreferences ("testsp", context. mode_private); // Step 1: Obtain the bitmapstring imagestring = sharedpreferences in the string format. getstring ("image", ""); // Step 2: Use base64 to convert the string to bytearrayinputstreambyte [] bytearray = base64.decode (imagestring, base64.default ); bytearrayinputstream = new bytearrayinputstream (bytearray); // Step 3: Use bytearrayinputstream to generate bitmapbitmap bitmap = bitmapfactory. decodestream (bytearrayinputstream); mimageview. setimagebitmap (Bitmap );}}

Main. XML is as follows:
<Relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns: Tools = "http://schemas.android.com/tools" Android: layout_width = "match_parent" Android: layout_height = "match_parent"> <button Android: id = "@ + ID/savebutton" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "Save the image to sharedpreferences" Android: layout_centerhorizontal = "true" Android: layout_margintop = "25dip"/> <button Android: Id = "@ + ID/getbutton" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: text = "get an image from sharedpreferences" Android: layout_centerhorizontal = "true" Android: layout_margintop = "80dip"/> <imageview Android: id = "@ + ID/imageview" Android: layout_width = "wrap_content" Android: layout_height = "wrap_content" Android: layout_centerinparent = "true"/> </relativelayout>


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.