简体   繁体   English

如何在Android App中从图库中挑选图像

[英]How To Pick Image From Gallery in Android App

i am trying to make an app in android that integrate Image Gallery with my app. 我正在尝试在Android中制作一个将Image Gallery与我的应用程序集成的应用程序。
Following are the tasks my app dose : 以下是我的应用程序执行的任务:

1.First screen shows user with and Image view and a button to load Picture. 1.第一个屏幕向用户显示和图像视图以及一个加载图片的按钮。
2.On click of “Load Picture” button, user will be redirected to Android's Image Gallery where she can select one image. 2.单击“加载图片”按钮后,用户将被重定向到Android的图片库,她可以在其中选择一张图片。
3.Once the image is selected, the image will be loaded in Image view on main screen. 3.选择图像后,图像将被加载到主屏幕的图像视图中。

in addition to these three above mentioned tasks i need my app to perform following tasks : 除了上述三个任务,我还需要我的应用执行以下任务:

4.When the image is selected ,it is shown on main screen , when i go for more "load picture" option the previously selected image get disappeared ,but i need my app not to hide the previously selected image ,infact the all selected images it shows in a horizontally scrolling list on the main screen .and when i select any of them from a horizontal scrolling list they shown on the main screen . 4.当选择图像时,它显示在主屏幕上,当我选择更多的“加载图片”选项时,先前选择的图像消失了,但是我需要我的应用程序不隐藏先前选择的图像,对所有选定的图像进行实作它显示在主屏幕上的水平滚动列表中。当我从水平滚动列表中选择任何一个时,它们都显示在主屏幕上。

for this my xml code is : 为此,我的xml代码是:

main.xml : 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">
    <ImageView
        android:id="@+id/imgView"
        android:layout_width="fill_parent"
        android:layout_height="wrap_content"
        android:layout_weight="1"></ImageView>
    <Button
        android:id="@+id/buttonLoadPicture"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_weight="0"
        android:text="Load Picture"
        android:layout_gravity="center"></Button>
</LinearLayout>

java code : Java代码:

package com.ohile.imagegallerydemo;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.provider.MediaStore;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.os.Bundle;
import android.app.Activity;
import android.view.Menu;

public class ImageGallery extends Activity {

    private static int RESULT_LOAD_IMAGE = 1;


    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_image_gallery);

        Button buttonLoadImage = (Button) findViewById(R.id.buttonLoadPicture);
        buttonLoadImage.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View arg0) {

                Intent i = new Intent(
                        Intent.ACTION_PICK,
                        android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);

                startActivityForResult(i, RESULT_LOAD_IMAGE);
            }
        });
    }


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

        if (requestCode == RESULT_LOAD_IMAGE && resultCode == RESULT_OK && null != data) {
            Uri selectedImage = data.getData();
            String[] filePathColumn = { MediaStore.Images.Media.DATA };

            Cursor cursor = getContentResolver().query(selectedImage,
                    filePathColumn, null, null, null);
            cursor.moveToFirst();

            int columnIndex = cursor.getColumnIndex(filePathColumn[0]);
            String picturePath = cursor.getString(columnIndex);
            cursor.close();

            ImageView imageView = (ImageView) findViewById(R.id.imgView);
            imageView.setImageBitmap(BitmapFactory.decodeFile(picturePath));

        }


    }
}

You can use ListView (Horizontal Scroll) or GridView to Display your all selected Images. 您可以使用ListView(水平滚动)或GridView显示所有选定的图像。 You can get idea from this tutorial . 您可以从本教程中了解

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM