阅读谷歌个人资料图片java.io.FileNotFoundException

时间:2019-09-02 20:10:19

标签: java android inputstream filenotfoundexception

我尝试在登录后阅读Google用户的个人资料图片,但是遇到输入流打开错误。我不想使用Glide或Picasso库。 我的代码很标准:

受保护的void onActivityResult(int requestCode,int resultCode,Intent data){         super.onActivityResult(requestCode,resultCode,data);

    // Result returned from launching the Intent from GoogleSignInClient.getSignInIntent(...);
    if (requestCode == RC_SIGN_IN) {
        // The Task returned from this call is always completed, no need to attach
        // a listener.
        Task<GoogleSignInAccount> completedTask = GoogleSignIn.getSignedInAccountFromIntent(data);
            try {
                final GoogleSignInAccount account = completedTask.getResult(ApiException.class);

                String email = account.getEmail();
                String deviceID = Settings.Secure.getString(LoginActivity.this.getContentResolver(), Settings.Secure.ANDROID_ID);
                Uri personPhotoURL = Uri.parse(account.getPhotoUrl().toString());

                try {
                    ContentResolver resolver = getBaseContext().getContentResolver();
                    InputStream imageStream = resolver.openInputStream(personPhotoURL);
                    Bitmap selectedImage = BitmapFactory.decodeStream(imageStream);
                    String encodedImage = encodeImage(selectedImage);
                } catch (IOException e) {
                    e.printStackTrace();
                }

Uri显示正确的Url(将其插入浏览器时将加载图片),但是imageStream的开头将引发“ java.io.FileNotFoundException:无内容提供程序:https://lh3.googleusercontent.com/a-/AAuE .....”。 >

我该如何解决这个问题?

1 个答案:

答案 0 :(得分:0)

URL是https URL,类似于此网页的URL。 ContentResolver无法处理这些问题。

使用图像加载库(例如Glide或Picasso)从URL加载图像。或者,使用OkHttp执行Web请求。