Advanced Android Learning notes 13 get all local photos

Source: Internet
Author: User

This is a usage of intent.

In Activityaction there is a "action_get_content" string constant that lets the user select a specific type of data.

Intent.settype ("image/*"); Select all the pictures locally.

Returns the URI of the data. We use this constant to generate the bitmap bitmap for the picture, and then add it to the picture control (ImageView).

Select the picture you want:

Main.xml

[Java]View PlainCopy
  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. <button
  8. android:id="@+id/button"
  9. Android:layout_width="Fill_parent"
  10. android:layout_height="Wrap_content"
  11. />
  12. <imageview
  13. android:id="@+id/image"
  14. Android:layout_width="Fill_parent"
  15. Android:scaletype="Fitxy"
  16. android:layout_height="Wrap_content"
  17. />
  18. </LinearLayout>


[Java]View PlainCopy
  1. Package xiaosi.image;
  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.view.View.OnClickListener;
  13. Import Android.widget.Button;
  14. Import Android.widget.ImageView;
  15. Public class Imageactivity extends Activity {
  16. /** Called when the activity is first created. * /
  17. Private button button = null;
  18. private ImageView ImageView = null;
  19. @Override
  20. public void OnCreate (Bundle savedinstancestate) {
  21. super.oncreate (savedinstancestate);
  22. Setcontentview (R.layout.main);
  23. Button = (button) Findviewbyid (R.id.button);
  24. Button.settext ("select Picture");
  25. Button.setonclicklistener (new Buttonlistener ());
  26. }
  27. private class Buttonlistener implements onclicklistener{
  28. public void OnClick (View v)
  29. {
  30. Intent Intent = new Intent ();
  31. / * Turn on pictures screen type set to image * /
  32. Intent.settype ("image/*");
  33. /* Use intent.action_get_content this ACTION */
  34. Intent.setaction (intent.action_get_content);
  35. / * Return to this screen after obtaining a photo * /
  36. Startactivityforresult (Intent, 1);
  37. }
  38. }
  39. @Override
  40. protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
  41. if (ResultCode = = RESULT_OK) {
  42. Uri uri = Data.getdata ();
  43. LOG.E ("uri", Uri.tostring ());
  44. Contentresolver contentresolver = this.getcontentresolver ();
  45. try {
  46. Bitmap Bitmap = Bitmapfactory.decodestream (Contentresolver.openinputstream (URI));
  47. ImageView = (ImageView) Findviewbyid (r.id.image);
  48. / * Set Bitmap to ImageView * /
  49. Imageview.setimagebitmap (bitmap);
  50. }
  51. catch (FileNotFoundException e) {
  52. LOG.E ("Exception", E.getmessage (), E);
  53. }
  54. }
  55. Super.onactivityresult (Requestcode, ResultCode, data);
  56. }
  57. }


Source code: Click to open the link

Advanced Android Learning notes 13 get all local photos

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.