Android ImagePicker

时间:2014-06-29 05:34:53

标签: android imageview

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

这是我的代码。

activity_main.xml中

<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

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文件夹而不是其他可绘制文件夹。这是错的吗?

2 个答案:

答案 0 :(得分:1)

未找到媒体表示您的图片库中没有图片。通过在设备(手机)中放置任何图像都可以解决此问题。

您正在使用以下代码加载/选择图片。

//create the intent for ImageGallery
Intent i = new Intent(Intent.ACTION_PICK);
i.setType("image/*");

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

如果您想从drawable文件夹加载图片并想要选择一个。然后你负责(编写代码)加载图像并选择(选择)一个

答案 1 :(得分:0)

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