0

I am trying to generate a QR code in my app by sending a HTTP request to a QR generator API and placing the image into an imageview. I'm just getting a blank screen however. I've tried a couple of different solutions that I've found but no luck. Here is the code that I am using. I've also added the internet permission in the manifest

activity_main.xml:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

    <ImageView android:id="@+id/qrImageView"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:layout_centerVertical="true" />

</RelativeLayout>

MainActivity.java

public class MainActivity extends Activity {

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        ImageView imageview = (ImageView) findViewById(R.id.qrImageView);

        String qrURL = "http://api.qrserver.com/v1/create-qr-code/?data=thisisatest&amp;size=300x300";
        String src = "QRGenerator";

        Drawable qrCode=null;
        try {
            qrCode = drawable_from_url(qrURL, src);
        } catch (MalformedURLException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        } 

        imageview.setImageDrawable(qrCode); 
    }

        Drawable drawable_from_url(String qurl, String src_name) throws MalformedURLException, IOException {
            URL url = new URL(qurl);
            HttpURLConnection connection = (HttpURLConnection) url.openConnection();
            InputStream is = connection.getInputStream();
            Drawable d = Drawable.createFromStream(is, src_name);
            return d;
        }

}
adohertyd
  • 2,689
  • 19
  • 58
  • 78

2 Answers2

1

You'll have to download the image firstly:

public static Bitmap loadBitmap(String url) {
    Bitmap bitmap = null;
    InputStream in = null;
    BufferedOutputStream out = null;

    try {
        in = new BufferedInputStream(new URL(url).openStream(), IO_BUFFER_SIZE);

        final ByteArrayOutputStream dataStream = new ByteArrayOutputStream();
        out = new BufferedOutputStream(dataStream, IO_BUFFER_SIZE);
        copy(in, out);
        out.flush();

        final byte[] data = dataStream.toByteArray();
        BitmapFactory.Options options = new BitmapFactory.Options();
        //options.inSampleSize = 1;

        bitmap = BitmapFactory.decodeByteArray(data, 0, data.length,options);
    } catch (IOException e) {
        Log.e(TAG, "Could not load Bitmap from: " + url);
    } finally {
        closeStream(in);
        closeStream(out);
    }

    return bitmap;
}

Then use the Imageview.setImageBitmap to set bitmap into the ImageView

Artyom Kiriliyk
  • 2,513
  • 1
  • 17
  • 21
0

I've solved it. I was running it on an emulator that had no internet connection, so it obviously couldn't contact the API. I ran it on my phone and it worked. I'll leave the question up for reference for others. Thanks for the input

adohertyd
  • 2,689
  • 19
  • 58
  • 78