Hallo!
Ik ben sinds kort begonnen met het ontdekken van Android. Heb ervaring in programmeren in Actionscript 3, PHP en Jquery. Ik probeer nu dus te beginnen met Andriod en bekijk scripts van anderen om het een en ander te ontdekken. Ik heb een script dat een afbeelding zou moeten downloaden van het internet. Helaas krijg ik steeds een exception. Ik weet dat er iets mis gaat met het downloaden maar weet niet precies wat de fout is en hoe ik dit beter zou kunnen achterhalen. Ik hoop dat mensen hier mij wat meer kunnen vertellen. Hier even mijn code.
.java file:
Code:package my.first.app; import Android.app.Activity; import Android.os.Bundle; import java.io.IOException; import java.io.InputStream; import java.io.InputStreamReader; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; import Android.graphics.Bitmap; import Android.graphics.BitmapFactory; import Android.widget.ImageView; import Android.widget.Toast; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.Element; import org.w3c.dom.Node; import org.w3c.dom.NodeList; public class MyFirstAppActivity extends Activity { ImageView img; private InputStream OpenHttpConnection(String urlString) throws IOException{ InputStream in = null; int response = -1; URL url = new URL(urlString); URLConnection conn = url.openConnection(); if (!(conn instanceof HttpURLConnection)) throw new IOException("Not an HTTP connection"); try{ HttpURLConnection httpConn = (HttpURLConnection) conn; httpConn.setAllowUserInteraction(false); httpConn.setInstanceFollowRedirects(true); httpConn.setRequestMethod("GET"); httpConn.connect(); response = httpConn.getResponseCode(); if (response == HttpURLConnection.HTTP_OK) { in = httpConn.getInputStream(); } } catch (Exception ex){ throw new IOException("Error connecting WTF"); //throw new IOException("Error connecting WTF", ex.getCause()); } return in; } private Bitmap DownloadImage(String URL){ Bitmap bitmap = null; InputStream in = null; try { in = OpenHttpConnection(URL); bitmap = BitmapFactory.decodeStream(in); in.close(); } catch (IOException e1) { Toast.makeText(this, e1.getLocalizedMessage(), Toast.LENGTH_LONG).show(); e1.printStackTrace(); } return bitmap; } /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); //---download an image--- Bitmap bitmap = DownloadImage("http://www.ecolijargroup.cn/Content/img/Company/content_lorenzo.jpg"); img = (ImageView) findViewById(R.id.img); img.setImageBitmap(bitmap); } }
.xml file:
Code:<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:Android="http://schemas.Android.com/apk/res/Android" Android:orientation="vertical" Android:layout_width="fill_parent" Android:layout_height="fill_parent" > <ImageView Android:id="@+id/img" Android:layout_width="fill_parent" Android:layout_height="200px" Android:layout_gravity="center" /> </LinearLayout>




LinkBack URL
About LinkBacks


Met citaat reageren

