Posted By

variable3 on 05/04/11


Tagged


Versions (?)

image downloading from internet


 / Published in: Java
 

This code for downloading a img

  1. //MainActivity.java
  2. package com.v3;
  3.  
  4. import java.io.IOException;
  5. import java.io.InputStream;
  6. import java.net.HttpURLConnection;
  7. import java.net.MalformedURLException;
  8. import java.net.URL;
  9. import java.util.Random;
  10.  
  11. import android.app.Activity;
  12. import android.graphics.Bitmap;
  13. import android.graphics.BitmapFactory;
  14. import android.os.Bundle;
  15. import android.os.Message;
  16. import android.util.Log;
  17. import android.view.View;
  18. import android.widget.Button;
  19. import android.widget.ImageView;
  20.  
  21. public class MainActivity extends Activity {
  22.  
  23.  
  24. ImageView imView;
  25. String imageUrl="http://variable3.com/files/images/email-sig.jpg";
  26. Random r= new Random();
  27. /** Called when the activity is first created. */
  28. @Override
  29. public void onCreate(Bundle icicle) {
  30. super.onCreate(icicle);
  31. setContentView(R.layout.main);
  32.  
  33. Button bt3= (Button)findViewById(R.id.get_imagebt);
  34. bt3.setOnClickListener(getImgListener);
  35.  
  36. imView = (ImageView)findViewById(R.id.imview);
  37. Log.i("img already downloaded","img");
  38. }
  39.  
  40. View.OnClickListener getImgListener = new View.OnClickListener()
  41. {
  42.  
  43. public void onClick(View view) {
  44. // TODO Auto-generated method stub
  45.  
  46. //i tried to randomize the file download, in my server i put 4 files with name like
  47. //png0.png, png1.png, png2.png so different file is downloaded in button press
  48. int i =r.nextInt(4);
  49. downloadFile(imageUrl);
  50. Log.i("im url",imageUrl);
  51. }
  52.  
  53. };
  54.  
  55.  
  56. Bitmap bmImg;
  57. void downloadFile(String fileUrl){
  58. URL myFileUrl =null;
  59.  
  60. //Message msg = Message.obtain();//n
  61. try {
  62. myFileUrl= new URL(fileUrl);
  63. } catch (MalformedURLException e) {
  64. // TODO Auto-generated catch block
  65. e.printStackTrace();
  66. }
  67. try {
  68. HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
  69. conn.setDoInput(true);
  70. conn.connect();
  71. int length = conn.getContentLength();
  72. InputStream is = conn.getInputStream();
  73. Log.i("im connected","Download");
  74. bmImg = BitmapFactory.decodeStream(is);
  75.  
  76.  
  77.  
  78. imView.setImageBitmap(bmImg);
  79. } catch (IOException e) {
  80. // TODO Auto-generated catch block
  81. e.printStackTrace();
  82. }
  83. }
  84.  
  85. }
  86. //main.xml
  87. <?xml version="1.0" encoding="utf-8"?>
  88. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
  89. android:orientation="vertical"
  90. android:layout_width="fill_parent"
  91. android:layout_height="fill_parent"
  92. >
  93.  
  94. <Button
  95. android:id="@+id/get_imagebt"
  96. android:layout_width="wrap_content"
  97. android:layout_height="wrap_content"
  98. android:text="Get an image"
  99. android:layout_gravity="center"
  100. />
  101. <ImageView
  102. android:layout_width="wrap_content"
  103. android:id="@+id/imview"
  104. android:layout_height="wrap_content"
  105. android:src="@drawable/icon"
  106. android:layout_gravity="center"/>
  107.  
  108.  
  109. </LinearLayout>
  110. //menifest.xml
  111. <?xml version="1.0" encoding="utf-8"?>
  112. <manifest xmlns:android="http://schemas.android.com/apk/res/android"
  113. package="com.v3"
  114. android:versionCode="1"
  115. android:versionName="1.0">
  116. <uses-sdk android:minSdkVersion="8" />
  117.  
  118. <application android:icon="@drawable/icon" android:label="@string/app_name">
  119. <activity android:name=".MainActivity"
  120. android:label="@string/app_name">
  121. <intent-filter>
  122. <action android:name="android.intent.action.MAIN" />
  123. <category android:name="android.intent.category.LAUNCHER" />
  124. </intent-filter>
  125. </activity>
  126.  
  127. </application>
  128.  
  129. <uses-permission android:name="android.permission.INTERNET"></uses-permission>
  130.  
  131. <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission>
  132.  
  133. <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission>
  134. </manifest>

Report this snippet  

You need to login to post a comment.