我尝试在登录后读取 Google 用户的个人资料图片,但是遇到输入流打开错误。我不想使用 Glide 或 Picasso 库。 我的代码非常标准:
protected 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: No contentprovider: https://lh3.googleusercontent.com/a-/AAuE....."”。
如何解决这个问题?
最佳答案
该 URL 是一个 https
URL,就像此网页的 URL。 ContentResolver
不处理这些。
使用图像加载库(例如 Glide 或 Picasso)从 URL 加载图像。或者,使用 OkHttp 执行 Web 请求。
关于java - 读取谷歌个人资料图片java.io.FileNotFoundException,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57762230/