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

Asw*_*wan 10 android

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

为此我必须从Android设备中的图库中获取图像.

如何编写实现此目的的代码?

Sam*_*muh 36

使用Action as提高Intent ACTION_GET_CONTENT并将类型设置为" image/*".这将启动照片选择器活动.当用户选择图像时,您可以使用onActivityResult回调来获取结果.

就像是:

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);
        }
}
Run Code Online (Sandbox Code Playgroud)