Display image url

Put your problem here if it does not fit any of the other categories.

Display image url

Postby langen » Tue Aug 21, 2012 5:34 pm

Hi!.

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:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  3.    android:orientation="vertical"
  4.    android:layout_width="fill_parent"
  5.    android:layout_height="fill_parent"
  6.    >
  7. <TextView  
  8.    android:layout_width="fill_parent"
  9.    android:layout_height="wrap_content"
  10.    />
  11.     <Button
  12.    android:id="@+id/get_imagebt"
  13.    android:layout_width="wrap_content"
  14.    android:layout_height="wrap_content"
  15.    android:text="Show Image"
  16.    android:layout_gravity="center"
  17.    />  
  18.     <ImageView
  19.    android:id="@+id/imview"
  20.    android:layout_width="wrap_content"
  21.    android:layout_height="wrap_content"
  22.    android:layout_gravity="center"
  23.    />
  24. </LinearLayout>
Parsed in 0.003 seconds, using GeSHi 1.0.8.4


MainActivity:
Syntax: [ Download ] [ Hide ]
Using java Syntax Highlighting
  1. package com.bild.visa;
  2.  
  3. import java.io.IOException;
  4. import java.io.InputStream;
  5. import java.net.HttpURLConnection;
  6. import java.net.MalformedURLException;
  7. import java.net.URL;
  8. import java.util.Random;
  9.  
  10. import android.app.Activity;
  11. import android.graphics.Bitmap;
  12. import android.graphics.BitmapFactory;
  13. import android.os.Bundle;
  14. import android.view.View;
  15. import android.widget.Button;
  16. import android.widget.ImageView;
  17.  
  18. public class MainActivity extends Activity {
  19.      
  20.  
  21.      ImageView imView;
  22.      String imageUrl="http://2.bp.blogspot.com/-4JD46Iw0Sas/TV14aYeZILI/AAAAAAAAAck/wlUyUW03URY/s1600/android.jpg";
  23.      Random r= new Random();
  24.     /** Called when the activity is first created. */
  25.     @Override
  26.     public void onCreate(Bundle icicle) {
  27.         super.onCreate(icicle);
  28.         setContentView(R.layout.activity_main);
  29.        
  30.         Button bt3= (Button)findViewById(R.id.get_imagebt);
  31.         bt3.setOnClickListener(getImgListener);
  32.         imView = (ImageView)findViewById(R.id.imview);
  33.     }    
  34.  
  35.     View.OnClickListener getImgListener = new View.OnClickListener()
  36.     {
  37.  
  38.           @Override
  39.           public void onClick(View view) {
  40.                downloadFile(imageUrl);
  41.           }
  42.      
  43.     };
  44.  
  45.    
  46.     Bitmap bmImg;
  47.     void downloadFile(String fileUrl){
  48.           URL myFileUrl =null;          
  49.           try {
  50.                myFileUrl= new URL(fileUrl);
  51.           } catch (MalformedURLException e) {
  52.                // TODO Auto-generated catch block
  53.                e.printStackTrace();
  54.           }
  55.           try {
  56.                HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
  57.                conn.setDoInput(true);
  58.                conn.connect();
  59.                InputStream is = conn.getInputStream();
  60.                
  61.                bmImg = BitmapFactory.decodeStream(is);
  62.                imView.setImageBitmap(bmImg);
  63.           } catch (IOException e) {
  64.                // TODO Auto-generated catch block
  65.                e.printStackTrace();
  66.           }
  67.      }
  68. }
Parsed in 0.038 seconds, using GeSHi 1.0.8.4


Manifest:
Syntax: [ Download ] [ Hide ]
Using xml Syntax Highlighting
  1. <uses-permission android:name="android.permission.INTERNET"/>
Parsed in 0.000 seconds, using GeSHi 1.0.8.4
langen
Once Poster
Once Poster
 
Posts: 1
Joined: Tue Aug 21, 2012 5:26 pm

Top

Return to Other Coding-Problems

Who is online

Users browsing this forum: No registered users and 29 guests