首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android ImagePicker

Android ImagePicker
EN

Stack Overflow用户
提问于 2014-06-29 05:34:52
回答 2查看 5K关注 0票数 2

我编写了一段代码,用于在Android中选择图像,并在创建可绘制文件夹后将某些图片复制到res/ pick文件夹中,而不会将内容复制到其他可绘制文件夹。

这是我的密码。

activity_main.xml

代码语言:javascript
复制
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:orientation="vertical" 
    android:id="@+id/container"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    tools:context="com.example.imagepicker.MainActivity"
    tools:ignore="MergeRootFrame" >

    <Button
        android:id="@+id/btnPick"
        android:onClick="pickImage"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="40dp"
        android:text="Load Image" />

    <ImageView
        android:id="@+id/imgView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_above="@+id/btnPick"
        android:layout_alignParentLeft="true"
        android:layout_alignParentRight="true"
        android:layout_alignParentTop="true"
        android:src="@drawable/ic_launcher" />

</RelativeLayout>

MainActivity.java

代码语言:javascript
复制
package com.example.imagepicker;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;

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;

public class MainActivity extends Activity {
    //Image loading result to pass to startActivityForResult method
    public static final int LOAD_IMAGE_RESULTS = 1;
    private Bitmap bitmap;

    //GUI components
    private Button button; //the button
    private ImageView image; //the imageview

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        //Find references to the GUI objects
        button = (Button)findViewById(R.id.btnPick);
        image = (ImageView)findViewById(R.id.imgView);

        //Set button's onClick listener object
        button.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {
                //create the intent for ImageGallery
                Intent i = new Intent(Intent.ACTION_PICK);
                i.setType("image/*");

                //Start new activity with the LOAD_IMAGE_RESULT to handle back the result when the image is picked from the Image Gallery
                startActivityForResult(i, LOAD_IMAGE_RESULTS);      
            }
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);

        InputStream ImageStream = null;

        /*Checking if the activity that was triggered was the ImageGallery
          If so then requestCode will match the LOAD_IMAGE_RESULTS value
          If the resultCode is RESULT_OK &
          There is some data that we know that image was picked*/
        if (requestCode == LOAD_IMAGE_RESULTS && resultCode == Activity.RESULT_OK && data != null) {
            try {
                //Let's read the picked image -its URI
                Uri pickedImage = data.getData();

                //Let's read the image path using content resolver
                ImageStream = getContentResolver().openInputStream(pickedImage);

                //Now let's set the GUI ImageView data with data read from the picked file
                Bitmap selectedImage = BitmapFactory.decodeStream(ImageStream);
            }

            catch(FileNotFoundException e) {
                e.printStackTrace();
            }

            finally {
                if (ImageStream != null) {
                    try {
                        ImageStream.close();
                    } catch(IOException e) {
                        e.printStackTrace();
                    }
                }
            }
        }
    }
}

在运行时,当我单击按钮加载图像时,会收到一条消息:"NO MEDIA FOUND“。

这里怎么了?我已将图像复制到res/drawable文件夹,而不是复制到其他可绘制文件夹。这样做不对吗?

EN

回答 2

Stack Overflow用户

发布于 2014-06-29 06:59:57

没有媒体找到意味着你的画廊里没有照片。通过在你的设备(电话)中放置任何图像可以解决这个问题。

您正在使用下面的代码加载/挑选图像。

代码语言:javascript
复制
//create the intent for ImageGallery
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");

代码将不会从res/加载图像。它将从您的设备/图库加载图像。

如果您想从可绘图文件夹加载图像并想要选择一个。然后,您就有责任(编写代码)加载图像并选择一个。

票数 2
EN

Stack Overflow用户

发布于 2015-03-09 09:32:27

您已经设置了属性android:onClick=" pickImage“,但是您的代码中没有方法pickImage。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24473459

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档