Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
196 views
in Technique[技术] by (71.8m points)

android - How to access an image from the phone's photo gallery?

By any chance, does anyone know how to access the phone's photo gallery? I am making an application that takes a picture of a plant leaf and analyzes the image to determine whether or not it is determine. We were hoping that we could give the user two options of taking the picture of the leaf or using an image of a leaf that the user has already taken. However, we got the picture taking part, but we do not know how to access the photo gallery.

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

You have to launch the Gallery App using the built-in Intents. After that, on your onActivityResult(), get the path of the selected image and load your image into your ImageView

main.xml

<?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/loadimage"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 android:text="Load Image"
 />
<TextView
 android:id="@+id/targeturi"
 android:layout_width="fill_parent"
 android:layout_height="wrap_content"
 />
<ImageView
 android:id="@+id/targetimage"
 android:layout_width="fill_parent"
 android:layout_height="fill_parent"
 />
</LinearLayout>

Your Activity

 package com.exercise.AndroidSelectImage;

    import java.io.FileNotFoundException;

    import android.app.Activity;
    import android.content.Intent;
    import android.graphics.Bitmap;
    import android.graphics.BitmapFactory;
    import android.net.Uri;
    import android.os.Bundle;
    import android.view.View;
    import android.widget.Button;
    import android.widget.ImageView;
    import android.widget.TextView;

    public class AndroidSelectImage extends Activity {

    TextView textTargetUri;
    ImageView targetImage;

     /** Called when the activity is first created. */
     @Override
     public void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.main);
         Button buttonLoadImage = (Button)findViewById(R.id.loadimage);
         textTargetUri = (TextView)findViewById(R.id.targeturi);
         targetImage = (ImageView)findViewById(R.id.targetimage);

         buttonLoadImage.setOnClickListener(new Button.OnClickListener(){

     @Override
     public void onClick(View arg0) {
      // TODO Auto-generated method stub
      Intent intent = new Intent(Intent.ACTION_PICK,
        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
      startActivityForResult(intent, 0);
     }});
     }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
    // TODO Auto-generated method stub
    super.onActivityResult(requestCode, resultCode, data);

    if (resultCode == RESULT_OK){
     Uri targetUri = data.getData();
     textTargetUri.setText(targetUri.toString());
     Bitmap bitmap;
     try {
      bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(targetUri));
      targetImage.setImageBitmap(bitmap);
     } catch (FileNotFoundException e) {
      // TODO Auto-generated catch block
      e.printStackTrace();
     }
    }
    }
    }

enter image description here


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...