如何从设备在Android的Java应用程序中的图像应用程序、图像、设备、Java

2023-09-12 21:46:31 作者:青春染指流年

在我的应用程序,我要上传的图片。

In my application I want to upload the image.

有关,我必须得在Android设备从画廊的图片。

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

我怎样写code,完成此?

How do I write code that accomplishes this?

推荐答案

提起的意图与行动为 ACTION_GET_CONTENT 和类型设置为图像/ * 。这将启动照片选择器活动。当用户选择一个图像,您可以使用 onActivityResult 回调得到的结果。

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

是这样的:

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