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?
- <?xml version= "1.0" encoding= "Utf-8"?>
- <linearlayout xmlns:android= "Http://schemas.android.com/apk/res/android"
- android:orientation= "Vertical"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Fill_parent"
- >
- <textview
- Android:layout_width= "Fill_parent"
- android:layout_height= "Wrap_content"
- android:text= "@string/hello"
- />
- <button
- Android:id= "@+id/b01"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Wrap_content"
- />
- <imageview
- Android:id= "@+id/iv01"
- Android:layout_width= "Fill_parent"
- android:layout_height= "Wrap_content"
- />
- </LinearLayout>
Lesson_01_pic.java:
[Java] view plaincopyprint?
- Package COM.YFZ;
- Import java.io.FileNotFoundException;
- Import android.app.Activity;
- Import Android.content.ContentResolver;
- Import android.content.Intent;
- Import Android.graphics.Bitmap;
- Import Android.graphics.BitmapFactory;
- Import Android.net.Uri;
- Import Android.os.Bundle;
- Import Android.util.Log;
- Import Android.view.View;
- Import Android.widget.Button;
- Import Android.widget.ImageView;
- public class Lesson_01_pic extends Activity {
- /** called when the activity is first created. */
- @Override
- public void OnCreate (Bundle savedinstancestate) {
- Super.oncreate (savedinstancestate);
- Setcontentview (R.layout.main);
- Button button = (button) Findviewbyid (R.ID.B01);
- Button.settext ("select Picture");
- Button.setonclicklistener (New Button.onclicklistener () {
- @Override
- public void OnClick (View v) {
- Intent Intent = new Intent ();
- /* Turn on pictures screen type set to image */
- Intent.settype ("image/*");
- /* Use intent.action_get_content this ACTION */
- Intent.setaction (intent.action_get_content);
- /* Return to this screen after obtaining a photo */
- Startactivityforresult (Intent, 1);
- }
- });
- }
- @Override
- protected void Onactivityresult (int requestcode, int resultcode, Intent data) {
- if (ResultCode = = RESULT_OK) {
- Uri uri = Data.getdata ();
- LOG.E ("uri", Uri.tostring ());
- Contentresolver CR = This.getcontentresolver ();
- try {
- Bitmap Bitmap = Bitmapfactory.decodestream (Cr.openinputstream (URI));
- ImageView ImageView = (ImageView) Findviewbyid (R.ID.IV01);
- /* Set Bitmap to ImageView */
- Imageview.setimagebitmap (bitmap);
- } catch (FileNotFoundException e) {
- LOG.E ("Exception", E.getmessage (), E);
- }
- }
- Super.onactivityresult (Requestcode, ResultCode, data);
- }
- }
Well, that's going to be so much.
Local picture selection (Open Media Library, select picture)