0

I try to get a image every time I click on the button in a Switcase but it isn't working. I also don't have an error so I really don't know what the problem is. I think I have the most important things already.

Java

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

<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>
OneCricketeer
  • 179,855
  • 19
  • 132
  • 245
Z. Joe
  • 233
  • 2
  • 4
  • 10

1 Answers1

-1

You're trying to network on the main thread.

If you read your logcat you got 'android.os.NetworkOnMainThreadException'

Picasso is a great for both downloading images and placing them in your views.

See: https://stackoverflow.com/a/3090802/5781216

Community
  • 1
  • 1
ctrlbox
  • 46
  • 6
  • awh, that -1 hurt.. Wasn't perfectly written.., only trying to get 15 points so I get to upvote the poeple that help me! Sorry.. – ctrlbox May 22 '16 at 14:46
  • If you would like to be upvoted, please provide fully detailed answers instead of linking to others. Thanks and welcome to StackOverflow! – OneCricketeer May 23 '16 at 16:05