Local picture selection (Open Media Library, select picture)

Source: Internet
Author: User

What I want to achieve in this survey is: after clicking the pictures button, get all the pictures in the phone, select a picture, and display it in ImageView.

Application: Image selection when uploading pictures, similar to "browse".

Effect:

All the pictures are listed, including the table of contents.

In activity ACTION There is a "action_get_content" string constant that allows the user to select a specific type of data and return the URI of that data. We use this constant and then set the type to "image/*", You can get all the image in your Android phone.

Main.xml:

[XHTML] view plaincopyprint? 
  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:

[Java] view plaincopyprint? 
  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 Picture");
  22. Button.setonclicklistener (New Button.onclicklistener () {
  23. @Override
  24. public void OnClick (View v) {
  25. Intent Intent = new Intent ();
  26. /* Turn on pictures screen type set to image */
  27. Intent.settype ("image/*");
  28. /* Use intent.action_get_content this ACTION */
  29. Intent.setaction (intent.action_get_content);
  30. /* Return to this screen after obtaining a 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 = (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. }

Well, that's going to be so much.

Local picture selection (Open Media Library, select picture)

Related Article

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.