无法从 google 个人资料图片 url 获取位图

cannot get bitmap from google profile picture url

我有一个应用程序,我在其中尝试从通过 google 集成获得的 link 中检索图像。但是,每当我尝试将图片转换为位图时,它总是给我一个错误。代码和错误如下:

代码:

if (google_user_gallery_pic.isEmpty() == true)
{
    if (google_user_pic.isEmpty() == false)
    {
        onPost = null;
        onPost2 = null;
        onPost = getBitmapFromURL(google_user_pic);
        onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
    }

    else
    {
        onPost = null;
        onPost2 = null;
        onPost = BitmapFactory.decodeFile(google_user_gallery_pic);
        if (onPost == null)
        {
            onPost2 = BitmapFactory.decodeResource(getResources(), R.drawable.the_smallperson);
        }

        else
        {
            onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
        }
    }
}

else
{
    onPost = null;
    onPost2 = null;
    onPost = BitmapFactory.decodeFile(google_user_gallery_pic);
    if (onPost == null)
    {
        onPost2 = BitmapFactory.decodeResource(getResources(), R.drawable.the_smallperson);
        //onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
    }

    else
    {
        onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
    }
}

错误:

E/BitmapFactory: Unable to decode stream: java.io.FileNotFoundException: https:/lh3.googleusercontent.com/-HW5Tk9h1V2I/AAAAAAAAAAI/AAAAAAAADA8/7UbMeHbyFLM/photo.jpg: open failed: ENOENT (No such file or directory)

url:

https://lh3.googleusercontent.com/-HW5Tk9h1V2I/AAAAAAAAAAI/AAAAAAAADA8/7UbMeHbyFLM/photo.jpg

此 url 在一页上有效,但在另一页上无效。这两个页面都是片段活动。任何帮助将不胜感激,谢谢!

尝试将 if 和 else 条件反转为如下所示:

if (google_user_gallery_pic.isEmpty() == false)
{
    if (google_user_pic.isEmpty() == false)
    {
        onPost = null;
        onPost2 = null;
        onPost = getBitmapFromURL(google_user_pic);
        onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
    }

    else
    {
        onPost = null;
        onPost2 = null;
        onPost = BitmapFactory.decodeFile(google_user_gallery_pic);
        if (onPost == null)
        {
            onPost2 = BitmapFactory.decodeResource(getResources(), R.drawable.the_smallperson);
        }

        else
        {
            onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
        }
    }
}

else
{
    onPost = null;
    onPost2 = null;
    onPost = BitmapFactory.decodeFile(google_user_gallery_pic);
    if (onPost == null)
    {
        onPost2 = BitmapFactory.decodeResource(getResources(), R.drawable.the_smallperson);
        //onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
    }

    else
    {
        onPost2 = onPost2.createScaledBitmap(onPost, 100, 100, false);
    }
}

因为给定了权限和其他任何东西而不是这个代码片段,所以除了这个之外没有任何问题

尝试使用 picasso 从 URL 加载图像位图。

 private Target mTarget;   //declare variable

void loadImage(Context context, String url) {

    final ImageView imageView = (ImageView) findViewById(R.id.image);

    mTarget = new Target() {
        @Override
        public void onBitmapLoaded (final Bitmap bitmap, Picasso.LoadedFrom from){
            //Do something on the bitmap
            imageView.setImageBitmap(bitmap); //set the bitmap
        }

        @Override
        public void onBitmapFailed(Drawable errorDrawable) {

        }

        @Override
        public void onPrepareLoad(Drawable placeHolderDrawable) {

        }
    };

    Picasso.with(context)
            .load(url)
            .into(mTarget);
            ..error(R.drawable.the_smallperson) // will be displayed if the image cannot be loaded
}

在gradle中添加:

compile 'com.squareup.picasso:picasso:2.5.2'