【问题标题】:Importing image from internet into Bitmap将图像从互联网导入位图
【发布时间】:2021-06-19 17:58:24
【问题描述】:

我对位图有疑问。 我调用方法:

mStickerListener.onStickerClick(
                                    BitmapFactory.decodeResource(getResources(),
                                            Integer.parseInt(stickerList.get(getLayoutPosition()))));

但问题是我的stickerList.get(getLayoutPosition())))返回字符串值(链接),因为我用毕加索显示图像,所以我得到了异常:

    java.lang.NumberFormatException: For input string: "https://firebasestorage.googleapis.com/v0/b/templatestest-38e3f.appspot.com/o/templates%2Fanimals_templates%2Fdog.png?alt=media&token=3e31962f-df24-493a-91c9-273f3496fa89"
      

请帮我解决!

【问题讨论】:

    标签: android bitmap picasso


    【解决方案1】:

    首先,您收到的异常是您正在尝试转换字符串“https://firebasestorage.googleapis.com/v0/b/templatestest-38e3f.appspot.com/o/templates%2Fanimals_templates%2Fdog .png?alt=media&token=3e31962f-df24-493a-91c9-273f3496fa89" 转为整数

    您正在使用 BitmapFactory.decodeResource 的方法将资源的 id 作为输入而不是 url。

    您需要使用以下代码从 url 获取位图:

     try {
        URL url = new URL("https://firebasestorage.googleapis.com/v0/b/templatestest-38e3f.appspot.com/o/templates%2Fanimals_templates%2Fdog.png?alt=media&token=3e31962f-df24-493a-91c9-273f3496fa89");
        Bitmap image = BitmapFactory.decodeStream(url.openConnection().getInputStream());
    } catch(IOException e) {
        System.out.println(e);
    }
    

    此外,在 AndroidManifest.xml 中添加 Internet 权限,因为您将访问 Internet 以从 url 获取流。

    <uses-permission android:name="android.permission.INTERNET" />
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2014-01-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多