社区首页 >问答首页 >如何实现URL中的开关案例映像

如何实现URL中的开关案例映像
EN

Stack Overflow用户
提问于 2016-05-22 06:24:50
回答 1查看 155关注 0票数 0

每次单击Switcase中的按钮时,我都会尝试获取图像,但它不起作用。我也没有错误,所以我真的不知道问题出在哪里。我想我已经有了最重要的东西。

Java

代码语言:javascript
代码运行次数:0
复制
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;


public class MainActivity extends AppCompatActivity {



/**
 * ATTENTION: This was auto-generated to implement the App Indexing API.
 * See https://g.co/AppIndexing/AndroidStudio for more information.
 */
 //   private GoogleApiClient client;
    private ImageView iv;
    private int a;
    private Bitmap bitmap;
 @Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
 //   iv = (ImageView) findViewById(R.id.imageView1);

   // bitmap = getBitmapFromURL("http://icons.iconarchive.com/icons    /crountch/one-piece-jolly-roger/72/Luffys-flag-2-icon.png");
   //     iv.setImageBitmap(bitmap);
    // ATTENTION: This was auto-generated to implement the App Indexing API.
    // See https://g.co/AppIndexing/AndroidStudio for more information.
  //  client = new GoogleApiClient.Builder(this).addApi(AppIndex.API).build();
}


 public void buttonOnClick(View v)  {
    // do something when the button is clicked
    Button button = (Button) v;

    a = (byte) (Math.random() * 2);

    switch (a) {
        case 1:
            iv = (ImageView) findViewById(R.id.imageView1);

            bitmap = getBitmapFromURL("http://icons.iconarchive.com/icons/crountch/one-piece-jolly-roger/72/Luffys-flag-2-icon.png");
            iv.setImageBitmap(bitmap);
        break;
      //  iv.setImageBitmap(bitmap);
        case 2:

        break;
    }
}
public Bitmap getBitmapFromURL(String src) {
    try {
        URL url = new URL(src);
        HttpURLConnection connection = (HttpURLConnection) url
                .openConnection();
        connection.setDoInput(true);
        connection.connect();
        InputStream input = connection.getInputStream();
        Bitmap myBitmap = BitmapFactory.decodeStream(input);
        return myBitmap;

    } catch (Exception e) {
        // TODO: handle exception
        e.printStackTrace();
        return null;
    }

}

}

XML

代码语言:javascript
代码运行次数:0
复制
<TextView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Image text"
    android:id="@+id/textView"
    android:layout_below="@+id/textView2"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="82dp" />

<Button
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="Image"
    android:id="@+id/button"
    android:onClick="buttonOnClick"
    android:layout_below="@+id/textView"
    android:layout_centerHorizontal="true"
    android:layout_marginTop="41dp" />

<ImageView
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/imageView1"
    android:layout_alignParentBottom="true"
    android:layout_alignRight="@+id/textView"
    android:layout_alignEnd="@+id/textView"
    android:layout_marginBottom="106dp" />

</RelativeLayout>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-05-22 06:39:03

你在试图在主线程上建立网络。

如果你读了你的日志,你就得到了“android.os.NetworkOnMainThreadException”

毕加索是一个伟大的下载图片和放置在您的意见。

请参阅:https://stackoverflow.com/a/3090802/5781216

票数 -1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/37375556

复制
相关文章
Team Foundation Server 2010 – Basic Installation
Microsoft Team Foundation Server 2010在安装上非常简单,除了在安装的容易程度上做了很大的简化,提供了向导式的配置,它还针对微软一个早年广为使用的Visual SourceSafe,提供一个非常简单的安装和设置好的安装示范– Basic Configuration,让Visual SourceSafe的使用者通过简单的安装后,马上就可以使用Microsoft Team Foundation Server 2010。 通过一个标准的安装步骤来说明,希望大家能快速的了解 Bas
张善友
2018/01/19
8290
Team Foundation Server 2010 – Basic Installation
TFS(Team Foundation Server)介绍和入门
在本文的两个部分中,我将介绍Team Foundation Server的一些核心特征,重点介绍在本产品的日常应用中是怎样将这些特性结合在一起使用的。
全栈程序员站长
2021/12/09
6K0
DevOps工具介绍连载(31)——Microsoft Team Foundation Server(TFS)
Team Foundation Server(TFS)是一种为 Microsoft 产品提供 源代码管理、数据收集、报告和项目跟踪,而为协作软件开发的项目。它是Microsoft应用程序生命周期管理(ALM)工具的核心协作平台,简单的说它是管理和开发软件项目的整个生命周期的平台工具。
顾翔
2020/04/10
1.8K0
DevOps工具介绍连载(31)——Microsoft Team Foundation Server(TFS)
让Team Foundation Server/TFS自动记住用户名密码解决方案
在使用Team Foundation Server(以下简称TFS) 的时候,在每次打开Visual Studio TFS时候,需要输入用户名和秘密,比较麻烦。 现提供一种方法可以解决这个问题: 依次
磊哥
2018/05/08
1.1K0
让Team Foundation Server/TFS自动记住用户名密码解决方案
SQL server合法标识符
~~ 数据库名是一个标识符,表名也是一个标识符,在SQL SERVER中标识符分为两类:
且陶陶
2023/04/12
5780
Core Foundation & Foundation
Core Foundation 是一组 C 语言接口,Foundation 用 Objective-C 封装了 Core Foundation 的 C 组件,并实现了额外了组件供开发人员使用。而 Core Foundation 也有一些 Foundation 没能彻底封装的功能,这些功能是 Core Foundation 特有的。Core Foundation 有 Foundation 没有的功能,比如 CFDictionary 的 Key 元素无需实现 NSCoping 协议、CFArray 可以不进行对象引用计数等、CFRunloop 提供了比 NSRunloop 更加细致化的 Api、利用 CFStringTransform 将中文转为拼音。反过来,Foundation 也有 Core Foundation 无法胜任的工作,最大的来说就是自动引用计数功能,还有比如 NSBundle 在 Core Foundation 中也没有。
CoderStar
2022/08/24
5380
Core Foundation & Foundation
nginx配置多个server
location = /uaapi { } 请求路径只能写127.0.0.1:8989/uaapi
全栈程序员站长
2022/07/21
2K0
nginx配置多个server
配置phpmyadmin管理多个server
//修改librarise/config.default.php $cfg['AllowArbitraryServer'] = true; //默认是false,改成true 最后添加 $hosts = array( '1'=>array('host'=>'127.0.0.1','user'=>'root','password'=>'123456'), '2'=>array('host'=>'127.0.0.2','user'=>'root','password'=>'123456') ); for($
苦咖啡
2018/04/28
9970
nginx简单配置多个server
注意:LZ一直出现访问不了,折腾了许久,是因为服务器www.pigaudio.com或120.77.223.7只开了默认的80端口,而8088端口并未开,所以只需要登陆你的服务账号添加一个8088即可,比如你的服务器是阿里云购买的,则需要登陆阿里云加一个8088,还有问题就是,如果你服务器打开了网络防火墙也是访问也是不行的,关了即可。
全栈程序员站长
2022/11/16
5.4K0
nginx简单配置多个server
nginx 代理多个服务器——多个server方式
上一篇文章(http://blog.csdn.net/wild46cat/article/details/52840125)介绍了nginx的基本配置和使用方法,并且简单的介绍了一下如何利用nginx结合tomcat进行使用,达到反向代理的作用。现在我们要使用nginx达到这样的一个目的,能够代理多个服务器。
Java架构师必看
2021/05/14
6.9K0
nginx 代理多个服务器——多个server方式
vs2008连接tfs2013_VS连接SQL网页登录
3.Install Visual Studio 2008 SP1
全栈程序员站长
2022/11/09
7600
通过位运算,实现单字段标识多个状态位
可能经常有如下这种需求: 需要一张表,来记录学员课程的通过与否. 课程数量不确定,往往很多,且会有变动,随时可能新增一门课.
fliter
2023/06/18
4430
通过位运算,实现单字段标识多个状态位
TFS2010 Team Project Collections
Team Foundation Server 2010有一个改进比较大的地方就是Team Project Collections,在TFS 2010 在Team Project之上,多了一个Colle
张善友
2018/01/19
9380
TFS2010  Team Project Collections
Foundation-String
最近写完了Swift 3.0教程 ,在接下来这段时间,继续写Foundation 的教程,帮助大家更加深入,系统的学习Foundation 框架,可能会持续一段时间,希望有兴趣的朋友加个关注!
酷走天涯
2018/09/14
9530
tfs安装教程_tfs4全称
索引: 介绍 安装操作系统 准备安装过程中的需要的用户账户 安装IIS 7.0 安装SQL Server 2008 安装SQL Server 2008 SP1补丁 安装TFS 2010 配置TFS 2010(以高级配置为例) 配置TFS Build服务 安装序列号
全栈程序员站长
2022/11/09
1.6K0
python找到多个字典中的公共键
python找到多个字典中的公共键 生成字典 from random import randint,sample s = 'abcdefg' s1 = {x:randint(1,4) for x in sample(s,randint(3,6))} s2 = {x:randint(1,4) for x in sample(s,randint(3,6))} s3 = {x:randint(1,4) for x in sample(s,randint(3,6))} 方案1:循环判断 res = [] fo
听城
2018/04/27
4.1K0
nginx配置多个server监听80端口
有时候需要部署很多个服务时,如果不想让域名的后面带上端口号这个问题应该怎么做呢,实际也是有这样的场景的,本小节来做一个学习
在水一方
2022/06/14
5.8K0
nginx配置多个server监听80端口
Foundation-NSLocale
localizedStringForKey("name", defaultValue: "xj",bundleName:"bundle")
酷走天涯
2018/09/14
3070
Foundation-NSLocale
DevOps Foundation总结
一晃马上就要考DOF讲师了,春节这几天看着这本全英文的考试参考书自然是泪流满面,虽然云层的英文真不咋地,但是看帮助手册还是没啥问题,可是这本书里面的大量的俚语和莫名其妙的比喻,啥都不说了。
TestOps
2022/04/07
3450
Foundation-RunLoop
介绍 1.RunLoop 类提供一些接口管理输入源对象 2.RunLoop 对象处理像键盘,鼠标等事件,以及Port和NSConnection 对象,还处理Timer 事件 *使用须知 1.不能自己创建或者管理RunLoop对象,因为每个线程都会在需要的时候自动创建属于自己的RunLoop 对象,我们可以通过current()方法进入当前线程的run loop 2.RunLoop 没有考虑线程安全,所以前往不要在其他线程调用当前线程的RunLoop,会造成意想不到的错误 ---- 方法深入研
酷走天涯
2018/09/14
7150

相似问题

从url中获取id号(分开),并使用它来选择id号(使用jquery)

45

如何获取类名并使用它?

12

获取具有特定类并具有不等于

36

VBA -如何检索列号并使用它填充另一列?

32

从输入中获取变量并使用它来更改css类

33
添加站长 进交流群

领取专属 10元无门槛券

AI混元助手 在线答疑

扫码加入开发者社群
关注 腾讯云开发者公众号

洞察 腾讯核心技术

剖析业界实践案例

扫码关注腾讯云开发者公众号
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档