如何在android java应用程序中从设备获取图像

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2227209/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-20 05:03:36  来源:igfitidea点击:

how to get the images from device in android java application

android

提问by Aswan

In my application I want to upload the image.

在我的应用程序中,我想上传图像。

For that I have to get images from gallery in android device.

为此,我必须从 android 设备的图库中获取图像。

How do I write code that accomplishes this?

我如何编写代码来实现这一点?

回答by Samuh

Raise an Intent with Action as ACTION_GET_CONTENTand set the type to "image/*". This will start the photo picker Activity. When the user selects an image, you can use the onActivityResultcallback to get the results.

使用 Action 提升 IntentACTION_GET_CONTENT并将类型设置为“ image/*”。这将启动照片选择器活动。当用户选择图像时,您可以使用onActivityResult回调来获取结果。

Something like:

就像是:

Intent photoPickerIntent = new Intent(Intent.ACTION_GET_CONTENT);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, 1);

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

        Bitmap mBitmap = null;
        mBitmap = Media.getBitmap(this.getContentResolver(), chosenImageUri);
        }
}