/ Published in: Java
This code for downloading a img
Expand |
Embed | Plain Text
//MainActivity.java package com.v3; 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.os.Message; import android.util.Log; import android.view.View; import android.widget.Button; import android.widget.ImageView; public class MainActivity extends Activity { ImageView imView; /** Called when the activity is first created. */ @Override public void onCreate(Bundle icicle) { super.onCreate(icicle); setContentView(R.layout.main); bt3.setOnClickListener(getImgListener); imView = (ImageView)findViewById(R.id.imview); Log.i("img already downloaded","img"); } { // TODO Auto-generated method stub //i tried to randomize the file download, in my server i put 4 files with name like //png0.png, png1.png, png2.png so different file is downloaded in button press int i =r.nextInt(4); downloadFile(imageUrl); Log.i("im url",imageUrl); } }; Bitmap bmImg; //Message msg = Message.obtain();//n try { // TODO Auto-generated catch block e.printStackTrace(); } try { conn.setDoInput(true); conn.connect(); int length = conn.getContentLength(); Log.i("im connected","Download"); bmImg = BitmapFactory.decodeStream(is); imView.setImageBitmap(bmImg); // TODO Auto-generated catch block e.printStackTrace(); } } } //main.xml <?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" > <Button android:id="@+id/get_imagebt" android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="Get an image" android:layout_gravity="center" /> <ImageView android:layout_width="wrap_content" android:id="@+id/imview" android:layout_height="wrap_content" android:src="@drawable/icon" android:layout_gravity="center"/> </LinearLayout> //menifest.xml <?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="com.v3" android:versionCode="1" android:versionName="1.0"> <uses-sdk android:minSdkVersion="8" /> <application android:icon="@drawable/icon" android:label="@string/app_name"> <activity android:name=".MainActivity" 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> <uses-permission android:name="android.permission.INTERNET"></uses-permission> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"></uses-permission> <uses-permission android:name="android.permission.READ_PHONE_STATE"></uses-permission> </manifest>
You need to login to post a comment.
