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&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;
}
}