0

我必须实现一个应用程序,一旦用户从手机图库部分打开他/她的任何图像,就将图像上传到服务器上。我的问题是如何获取图像路径?提前致谢

4

1 回答 1

0

首先调用此意图从图库中选择图像:

private static final int SELECT_PHOTO = 100;

Intent photoPickerIntent = new Intent(Intent.ACTION_PICK);
photoPickerIntent.setType("image/*");
startActivityForResult(photoPickerIntent, SELECT_PHOTO);

完成从图库中选择图像后,它将自动调用此onActivityResult方法:

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

        switch (requestCode) {
        case SELECT_PHOTO:
            if (resultCode == RESULT_OK) {
                Uri selectedImage = imageReturnedIntent.getData();
                try {
                    yourImageView.setImageBitmap(decodeUri(selectedImage));
                } catch (FileNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

您将在selectedImage变量中获得图像路径。

谢谢。

于 2013-01-07T10:56:23.997 回答