从库中选择图像到imageview

时间:2021-10-26 00:21:28

I am facing a problem in selecting the image from a gallery and setting it into the imageview. Suppose I have two activities; mainActivity containing buttons for gallery and secondactivity containing the imageview in which the image has to be displayed.

我在从图库中选择图像并将其设置为图像视图时遇到问题。假设我有两个活动; mainActivity包含gallery和secondactivity的按钮,包含必须显示图像的imageview。

 b1.setOnClickListener(new OnClickListener() {
    public void onClick(View v) {
    Intent i = new Intent(
    Intent.ACTION_PICK, android.provider.MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
    startActivityForResult(i, RESULT_LOAD_IMAGE);
   }
   });

Please give me the individual code for both....

请给我两个人的代码....

5 个解决方案

#1


16  

here is the code to load an image from gallery:

这是从图库加载图像的代码:

public class ImageGalleryDemoActivity extends Activity {


    private static int RESULT_LOAD_IMAGE = 1;


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

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

        }


    }
}

#2


4  

with Picasso it can be done in single line and You dont need to make cursor query

与Picasso一起,它可以在单行中完成,你不需要进行游标查询

I have extended it for better understanding :-

我已将其扩展以便更好地理解: -

Pick Image

选择图片

    public void pickImage() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

    }

Load Image

加载图片

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {

                    Uri selectedImageURI = data.getData();

                    Picasso.with(MainActivity1.this).load(selectedImageURI).noPlaceholder().centerCrop().fit()
                            .into((ImageView) findViewById(R.id.imageView1));
                }

            }
        }

#3


1  

Try this.

尝试这个。

 b1.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));
        }
    }

Here some examples

这里有一些例子

http://viralpatel.net/blogs/pick-image-from-galary-android-app/

http://viralpatel.net/blogs/pick-image-from-galary-android-app/

http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/

http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/

#4


0  

To Pick Image from gallery, use this code.

要从库中选择图像,请使用此代码。

btn_imageSetter.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMAGE_PICKER);

    }
});

Now use this Method to Set the image to the ImageViewer

现在使用此方法将图像设置为ImageViewer

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

    if (requestCode == IMAGE_PICKER && resultCode == RESULT_OK && null != data) {
        try{
            final Uri uriImage = data.getData();
            final InputStream inputStream = getContentResolver().openInputStream(uriImage);
            final Bitmap imageMap = BitmapFactory.decodeStream(inputStream);
            iv_image.setImageBitmap(imageMap);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(context, "Image was not found", Toast.LENGTH_SHORT).show();
        }

    }


}

#5


0  

In my case work this solution

在我的情况下,这个解决方案

 private void OpenGallery(){
    Intent getImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getImageIntent .setType("image/*");
    startActivityForResult(getImageIntent , IMAGE_PICKER );
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode== IMAGE_PICKER  && resultCode == RESULT_OK) {
        Uri fullPhotoUri = data.getData();
        imageView.setImageURI(fullPhotoUri);
    }
}

You can share URI using putExtra("fullPhotoUri", fullPhotoUri.toString()) between activities.

您可以在活动之间使用putExtra(“fullPhotoUri”,fullPhotoUri.toString())共享URI。

#1


16  

here is the code to load an image from gallery:

这是从图库加载图像的代码:

public class ImageGalleryDemoActivity extends Activity {


    private static int RESULT_LOAD_IMAGE = 1;


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

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

        }


    }
}

#2


4  

with Picasso it can be done in single line and You dont need to make cursor query

与Picasso一起,它可以在单行中完成,你不需要进行游标查询

I have extended it for better understanding :-

我已将其扩展以便更好地理解: -

Pick Image

选择图片

    public void pickImage() {

        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), SELECT_PICTURE);

    }

Load Image

加载图片

 protected void onActivityResult(int requestCode, int resultCode, Intent data) {
            super.onActivityResult(requestCode, resultCode, data);
            if (resultCode == RESULT_OK) {
                if (requestCode == SELECT_PICTURE) {

                    Uri selectedImageURI = data.getData();

                    Picasso.with(MainActivity1.this).load(selectedImageURI).noPlaceholder().centerCrop().fit()
                            .into((ImageView) findViewById(R.id.imageView1));
                }

            }
        }

#3


1  

Try this.

尝试这个。

 b1.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));
        }
    }

Here some examples

这里有一些例子

http://viralpatel.net/blogs/pick-image-from-galary-android-app/

http://viralpatel.net/blogs/pick-image-from-galary-android-app/

http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/

http://www.coderzheaven.com/2012/04/20/select-an-image-from-gallery-in-android-and-show-it-in-an-imageview/

#4


0  

To Pick Image from gallery, use this code.

要从库中选择图像,请使用此代码。

btn_imageSetter.setOnClickListener(new View.OnClickListener() {
    @Override
    public void onClick(View v) {
        Intent intent = new Intent();
        intent.setType("image/*");
        intent.setAction(Intent.ACTION_GET_CONTENT);
        startActivityForResult(Intent.createChooser(intent, "Select Picture"), IMAGE_PICKER);

    }
});

Now use this Method to Set the image to the ImageViewer

现在使用此方法将图像设置为ImageViewer

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

    if (requestCode == IMAGE_PICKER && resultCode == RESULT_OK && null != data) {
        try{
            final Uri uriImage = data.getData();
            final InputStream inputStream = getContentResolver().openInputStream(uriImage);
            final Bitmap imageMap = BitmapFactory.decodeStream(inputStream);
            iv_image.setImageBitmap(imageMap);
        }
        catch (FileNotFoundException e) {
            e.printStackTrace();
            Toast.makeText(context, "Image was not found", Toast.LENGTH_SHORT).show();
        }

    }


}

#5


0  

In my case work this solution

在我的情况下,这个解决方案

 private void OpenGallery(){
    Intent getImageIntent = new Intent(Intent.ACTION_GET_CONTENT);
    getImageIntent .setType("image/*");
    startActivityForResult(getImageIntent , IMAGE_PICKER );
}

@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
    if (requestCode== IMAGE_PICKER  && resultCode == RESULT_OK) {
        Uri fullPhotoUri = data.getData();
        imageView.setImageURI(fullPhotoUri);
    }
}

You can share URI using putExtra("fullPhotoUri", fullPhotoUri.toString()) between activities.

您可以在活动之间使用putExtra(“fullPhotoUri”,fullPhotoUri.toString())共享URI。