Once I needed to display an image loading from URL in my Android
application. After referring some resources I was success. Here I post
what I did. This is for future references. I did this for Android 2.2
platform.
This Android application load an image from a server using HttpURLConnection and show it in image view when you click load image button.
(I have used image located here http://www.codeincloud.tk/play.png)
This is the java code
Add internet permission to Manifest file
This Android application load an image from a server using HttpURLConnection and show it in image view when you click load image button.
(I have used image located here http://www.codeincloud.tk/play.png)
Here is the layout file.
<?xml version="1.0" encoding="utf-8"?> <AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent" android:orientation="vertical" > <Button android:id="@+id/btn_imgload" android:layout_width="208dp" android:layout_height="wrap_content" android:layout_x="57dp" android:layout_y="322dp" android:text="Load Image" /> <ImageView android:id="@+id/imageview" android:layout_width="60dp" android:layout_height="60dp" android:layout_x="131dp" android:layout_y="179dp" /> </AbsoluteLayout>
This is the java code
import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; 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 LoadImageActivity extends Activity { ImageView image_view; Button btnLoadImg ; final static String imageLocation="http://www.codeincloud.tk/play.png"; //Use any image location. /** Called when the activity is first created. */ @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); image_view = (ImageView)findViewById(R.id.imageview); btnLoadImg = (Button)findViewById(R.id.btn_imgload); btnLoadImg.setOnClickListener(loadImage); } View.OnClickListener loadImage = new View.OnClickListener(){ public void onClick(View view) { loadImage(imageLocation); } }; Bitmap bitmap; void loadImage(String image_location){ URL imageURL = null; try { imageURL = new URL(image_location); } catch (MalformedURLException e) { e.printStackTrace(); } try { HttpURLConnection connection= (HttpURLConnection)imageURL.openConnection(); connection.setDoInput(true); connection.connect(); InputStream inputStream = connection.getInputStream(); bitmap = BitmapFactory.decodeStream(inputStream);//Convert to bitmap image_view.setImageBitmap(bitmap); } catch (IOException e) { e.printStackTrace(); } } }
Add internet permission to Manifest file
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="tutorial.imageload" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="8" /> <uses-permission android:name="android.permission.INTERNET"/> <application android:icon="@drawable/ic_launcher" android:label="@string/app_name" > <activity android:name=".LoadImageActivity" android:label="@string/app_name" > <intent-filter> <action android:name="android.intent.action.MAIN" /> <category android:name="android.intent.category.LAUNCHER"/> </intent-filter> </activity> </application> </manifest>
jianbin1205
ReplyDeletecheap uggs
north face outlet online
coach outlet online
uggs outlet
snapback hats
true religion outlet,true religion jeans,true religion kids,true religion jeans sale,true religion jeans for men,true religion jacket,true religion sale
cheap oakley sunglasses
moncler coats
ugg boots on sale
cyber monday
kobe bryants shoes 2015
polo ralph lauren uk
moncler outlet
lululemon outlet
prada outlet
chanel handbags
ugg uk outlet
toms outlet
canada goose outlet
the north face jackets
new england patriots
chicago blackhawks
michael kors outlet sale
new york giants
air force one shoes
nike air max 90
ugg boots
air jordan 11 free shipping
michael kors handbags
nike trainers uk
air jordan shoes
kansas city chiefs
chicago bulls
Really wonderful information,easily observe all provided content. i find more new information,i like that kind of information.these information was more effective content.
ReplyDeleteOracle SQL Training in Chennai
In This World of Communication and quick moving business through web and web it turns out to be extremely important to spare the ideal opportunity for showcasing and advancement of your items and business. So there is a simple answer for every one of the business people to advance their new items over the business sector with the assistance of Bulk SMS Technology.sms plugin
ReplyDeleteBurberry Outlet
ReplyDeleteOakley Eyeglasses Michael Kors Outlet Coach Factory Outlet Coach Outlet Online Coach Purses Kate Spade Outlet Toms Shoes North Face Outlet Coach Outlet Gucci Belt North Face Jackets Oakley Sunglasses Toms Outlet North Face Outlet Nike Outlet Nike Hoodies Tory Burch Flats Marc Jacobs Handbags Jimmy Choo Shoes Jimmy Choos
Burberry Belt Tory Burch Boots Louis Vuitton Belt Ferragamo Belt Marc Jacobs Handbags Lululemon Outlet Christian Louboutin Shoes True Religion Outlet Tommy Hilfiger Outlet
Michael Kors Outlet Coach Outlet Red Bottoms Kevin Durant Shoes New Balance Outlet Adidas Outlet Coach Outlet Online Stephen Curry Jersey
Great work, Thanks for your wonderful post.It is really very helpful for us and I have gathered some important information from this blog.
ReplyDeleteAndroid Training in Chennai
Thanks for sharing this information
ReplyDeleteandroid Introduction
Andriod mobile app development
questions regarding android
android
This is an awesome post.Really very informative and creative contents. These concept is a good way to enhance the knowledge.I like it and help me to development very well.Thank you for this brief explanation and very nice information.Well, got a good knowledge.
ReplyDeleteapple iphone service center in chennai | Mac book pro service center in chennai | ipod service center in chennai | apple ipad service center in chennai
ReplyDeleteAwesome post with lots of data and I have bookmarked this page for my reference. Share more ideas frequently.
aws training in chennai | aws training in annanagar | aws training in omr | aws training in porur | aws training in tambaram | aws training in velachery
This comment has been removed by the author.
ReplyDeleteoff white hoodie
ReplyDeleteyeezy boost 350 v2
palm angels hoodie
kobe shoes
golden goose sneakers
goyard bag
off white
air jordan
goyard bag
off white t shirt