Android的ImagePicker [英] Android ImagePicker

查看:183
本文介绍了Android的ImagePicker的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写了一个code接Android中的图像和复制某些图片水库/可绘制文件夹中创建的文件夹绘制后,没有重复的内容到其他文件夹绘制

I wrote a code to pick images in Android and copied certain pics to res/drawable folder after creating the drawable folder, without duplicating the contents to other drawable folders.

下面是我的code。

Here's my code.

activiy_main.xml

activiy_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

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

            }


        }

    }



}

在运行,当我点击按钮来加载图像,我得到一个消息 找不到媒体

On running, when I click on the button to load images, I get a message "NO MEDIA FOUND".

这里有什么不对吗?我复制了这些图像资源/文件夹绘制,而不是其他的绘制文件夹。是不是错了?

What's wrong here? I've copied the images to res/drawable folder and not to other drawable folders. Is that wrong?

推荐答案

找不到媒体意味着在您的画廊没有画面。通过将任何图像在你的设备(手机),将​​解决这个问题。

NO MEDIA FOUND means there is no picture in your gallery. By placing any image in your device (phone) will solve this issue.

您使用以下code加载/挑选图像。

You are using following code to load/pick images.

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

code将不会加载从RES /绘制图像。它会从你的设备/画廊加载图像。

This code will not load images from res/drawable. It will load images from your device/gallery.

如果您想从绘制文件夹载入图像,想挑选一个。然后,它是你的责任(写作code)加载图像,并挑选(选择)有一个

If you want to load images from drawable folder and want to pick one. Then it is your responsibility (writing code) to load images and pick (select) one

这篇关于Android的ImagePicker的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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