如何从手机的照片库访问图像? [英] How to access an image from the phone's photo gallery?

查看:233
本文介绍了如何从手机的照片库访问图像?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以任何机会,没有人知道如何访问手机的照片库? 我想提出一个应用程序,采用植物叶子的图片, 分析图像,以确定它是否是确定。我们都希望我们 可以给用户服用的叶子的照片或使用的图像两个选择 该用户已经采取叶。然而,我们得到了拍照的一部分,但我们不这样做 知道如何访问照片库。

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.

推荐答案

您必须使用内置的意图启动图库应用程序。在此之后,在你的 onActivityResult(),获得所选图像的路径和加载图像到的ImageView

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>

您的活动

 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();
     }
    }
    }
    }

这篇关于如何从手机的照片库访问图像?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆