如何从手机相册中访问图片?

21

请问有谁知道如何访问手机的相册吗?我正在开发一个应用程序,它可以拍摄植物叶片的照片并分析图像,确定它是否是确定的。我们希望可以为用户提供两个选项:拍摄叶片照片或使用用户已经拍摄的叶片图像。然而,我们已经实现了照片拍摄部分,但是不知道如何访问相册。

2个回答

40
你必须使用内置的Intents启动相册应用程序。之后,在你的onActivityResult()中获取所选图像的路径并将其加载到你的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();
     }
    }
    }
    }

输入图像描述


19

不要忘记将以下权限添加到AndroidManifest.xml文件中:

<uses-permission android:name="android.permission.MANAGE_DOCUMENTS" />
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />

我正在尝试将上传的图像作为背景,你知道怎么做吗? - nothingness
2
如果您已经拥有WRITE_EXTERNAL_STORAGE,则只需要READ_EXTERNAL_STORAGE。您确定MANAGE_DOCUMENTS有意义吗?https://developer.android.com/reference/android/Manifest.permission.html说:“此权限仅应由平台文档管理应用程序请求。此权限无法授予第三方应用程序。” - The incredible Jan

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接