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

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

Stack Overflow用户
提问于 2016-05-22 14: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 14:39:03

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

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

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

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

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

https://stackoverflow.com/questions/37375556

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档