I'm trying to develop an app that display an image with a specific url. So far it is working in my emulator(2.2) but not on my phone (4.0.3). I dont know why but I hope someone can help me
. The code is copied but modify for my work. Layout:
Using xml Syntax Highlighting
- <?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"
- >
- <TextView
- android:layout_width="fill_parent"
- android:layout_height="wrap_content"
- />
- <Button
- android:id="@+id/get_imagebt"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:text="Show Image"
- android:layout_gravity="center"
- />
- <ImageView
- android:id="@+id/imview"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_gravity="center"
- />
- </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4
MainActivity:
Using java Syntax Highlighting
- package com.bild.visa;
- import java.io.IOException;
- import java.io.InputStream;
- import java.net.HttpURLConnection;
- import java.net.MalformedURLException;
- import java.net.URL;
- import java.util.Random;
- import android.app.Activity;
- import android.graphics.Bitmap;
- import android.graphics.BitmapFactory;
- import android.os.Bundle;
- import android.view.View;
- import android.widget.Button;
- import android.widget.ImageView;
- public class MainActivity extends Activity {
- ImageView imView;
- String imageUrl="http://2.bp.blogspot.com/-4JD46Iw0Sas/TV14aYeZILI/AAAAAAAAAck/wlUyUW03URY/s1600/android.jpg";
- Random r= new Random();
- /** Called when the activity is first created. */
- @Override
- public void onCreate(Bundle icicle) {
- super.onCreate(icicle);
- setContentView(R.layout.activity_main);
- Button bt3= (Button)findViewById(R.id.get_imagebt);
- bt3.setOnClickListener(getImgListener);
- imView = (ImageView)findViewById(R.id.imview);
- }
- View.OnClickListener getImgListener = new View.OnClickListener()
- {
- @Override
- public void onClick(View view) {
- downloadFile(imageUrl);
- }
- };
- Bitmap bmImg;
- void downloadFile(String fileUrl){
- URL myFileUrl =null;
- try {
- myFileUrl= new URL(fileUrl);
- } catch (MalformedURLException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- try {
- HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
- conn.setDoInput(true);
- conn.connect();
- InputStream is = conn.getInputStream();
- bmImg = BitmapFactory.decodeStream(is);
- imView.setImageBitmap(bmImg);
- } catch (IOException e) {
- // TODO Auto-generated catch block
- e.printStackTrace();
- }
- }
- }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4
Manifest:

