Android SD card Image Browsing

Source: Internet
Author: User

In activityaction, there is a "action_get_content" String constant, which allows users to select a specific type of data and return the URI of the data. we can use this constant and set the type to "image/*" to obtain all images in the Android mobile phone.

 

 

Main. xml:

  1. <? XML version = "1.0" encoding = "UTF-8"?>
  2. <Linearlayout xmlns: Android = "http://schemas.android.com/apk/res/android"
  3. Android: Orientation = "vertical"
  4. Android: layout_width = "fill_parent"
  5. Android: layout_height = "fill_parent"
  6. >
  7. <Textview
  8. Android: layout_width = "fill_parent"
  9. Android: layout_height = "wrap_content"
  10. Android: text = "@ string/hello"
  11. />
  12. <Button
  13. Android: Id = "@ + ID/B01"
  14. Android: layout_width = "fill_parent"
  15. Android: layout_height = "wrap_content"
  16. />
  17. <Imageview
  18. Android: Id = "@ + ID/iv01"
  19. Android: layout_width = "fill_parent"
  20. Android: layout_height = "wrap_content"
  21. />
  22. </Linearlayout>

 

 

Lesson_01_pic.java:

  1. Package com. yfz;
  2. Import java. Io. filenotfoundexception;
  3. Import Android. App. activity;
  4. Import Android. content. contentresolver;
  5. Import Android. content. intent;
  6. Import Android. Graphics. Bitmap;
  7. Import Android. Graphics. bitmapfactory;
  8. Import android.net. Uri;
  9. Import Android. OS. Bundle;
  10. Import Android. util. log;
  11. Import Android. View. view;
  12. Import Android. widget. Button;
  13. Import Android. widget. imageview;
  14. Public class lesson_01_pic extends activity {
  15. /** Called when the activity is first created .*/
  16. @ Override
  17. Public void oncreate (bundle savedinstancestate ){
  18. Super. oncreate (savedinstancestate );
  19. Setcontentview (R. layout. Main );
  20. Button button = (button) findviewbyid (R. Id. B01 );
  21. Button. settext ("select image ");
  22. Button. setonclicklistener (New button. onclicklistener (){
  23. @ Override
  24. Public void onclick (view v ){
  25. Intent intent = new intent ();
  26. /* Enable pictures image type to set to image */
  27. Intent. settype ("image /*");
  28. /* Use the intent. action_get_content action */
  29. Intent. setaction (intent. action_get_content );
  30. /* Return to the current screen after obtaining the photo */
  31. Startactivityforresult (intent, 1 );
  32. }
  33. });
  34. }
  35. @ Override
  36. Protected void onactivityresult (INT requestcode, int resultcode, intent data ){
  37. If (resultcode = result_ OK ){
  38. Uri uri = data. getdata ();
  39. Log. E ("Uri", Uri. tostring ());
  40. Contentresolver Cr = This. getcontentresolver ();
  41. Try {
  42. Bitmap bitmap = bitmapfactory. decodestream (Cr. openinputstream (URI ));
  43. Imageview = (imageview) findviewbyid (R. Id. iv01 );
  44. /* Set bitmap to imageview */
  45. Imageview. setimagebitmap (Bitmap );
  46. } Catch (filenotfoundexception e ){
  47. Log. E ("exception", E. getmessage (), e );
  48. }
  49. }
  50. Super. onactivityresult (requestcode, resultcode, data );
  51. }
  52. }

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.