标签:des android style blog http io os 使用 java
作者:卿笃军
原文地址:http://blog.csdn.net/qingdujun/article/details/39271479
URL对象中前而几个方法都非常容易理解,而该对象提供的openStream()可以读取该 URL资源的InputStream,通过该方法可以非常方便地读取远程资源。
下面的程序示范如何通过URL类读取远程资源:
1)只显示网络图片
2)显示并下载网络图片
1)只显示网络图片
a) activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingLeft="@dimen/activity_horizontal_margin" android:paddingRight="@dimen/activity_horizontal_margin" android:paddingTop="@dimen/activity_vertical_margin" tools:context="dujun.king.urlgetimage.MainActivity" > <ImageView android:id="@+id/imageView1" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:layout_centerHorizontal="true" android:layout_marginBottom="196dp" android:src="@drawable/ic_launcher" /> </RelativeLayout>
b)MainActivity.java
package dujun.king.urlgetimage; import java.io.InputStream; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; public class MainActivity extends Activity { Bitmap bitmap; ImageView imageview; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what==0x9527) { //显示从网上下载的图片 imageview.setImageBitmap(bitmap); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview=(ImageView)findViewById(R.id.imageView1); //创建并启动一个新线程用于从网络上下载图片 new Thread(){ @Override public void run() { try { //创建一个url对象 URL url=new URL("http://www.baidu.com/img/bdlogo.png"); //打开URL对应的资源输入流 InputStream is= url.openStream(); //从InputStream流中解析出图片 bitmap = BitmapFactory.decodeStream(is); // imageview.setImageBitmap(bitmap); //发送消息,通知UI组件显示图片 handler.sendEmptyMessage(0x9527); //关闭输入流 is.close(); } catch (Exception e) { e.printStackTrace(); } } }.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }c)AndroidMainifest.xml
<?xml version="1.0" encoding="utf-8"?> <manifest xmlns:android="http://schemas.android.com/apk/res/android" package="dujun.king.urlgetimage" android:versionCode="1" android:versionName="1.0" > <uses-sdk android:minSdkVersion="15" android:targetSdkVersion="15" /> <!-- 访问Internet权限 --> <uses-permission android:name="android.permission.INTERNET"/> <application android:allowBackup="true" android:icon="@drawable/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" > <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> </manifest>
2)如果我们要显示并下载网络图片,只需要将MainActivity.java文件修改如下:
package dujun.king.urlgetimage; import java.io.InputStream; import java.io.OutputStream; import java.net.URL; import android.app.Activity; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.os.Bundle; import android.os.Handler; import android.os.Message; import android.view.Menu; import android.view.MenuItem; import android.widget.ImageView; public class MainActivity extends Activity { Bitmap bitmap; ImageView imageview; Handler handler=new Handler(){ @Override public void handleMessage(Message msg) { if (msg.what==0x9527) { //显示从网上下载的图片 imageview.setImageBitmap(bitmap); } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageview=(ImageView)findViewById(R.id.imageView1); //创建并启动一个新线程用于从网络上下载图片 //这里必须新起一个线程,Android后面的版本中,不允许在主线程中联网 new Thread(){ @Override public void run() { try { //创建一个url对象 URL url=new URL("http://www.baidu.com/img/bdlogo.png"); //打开URL对应的资源输入流 InputStream is= url.openStream(); //从InputStream流中解析出图片 bitmap = BitmapFactory.decodeStream(is); // imageview.setImageBitmap(bitmap); //发送消息,通知UI组件显示图片 handler.sendEmptyMessage(0x9527); //关闭输入流 is.close(); //------------------------------ //下载网络图片 //再次打开资源流 is = url.openStream(); //打开手机对应的输出流 //存放在手机中,并命名为baidulogo.png OutputStream os = openFileOutput("baidulogo.png", MODE_WORLD_READABLE); byte[] buff = new byte[1024]; int len = 0; //因为网络下载一般不可能一次下载完毕,我们将每次下载好的有效数据写入 while ((len = is.read(buff)) > 0) { os.write(buff,0,len); } //关闭流 is.close(); os.close(); } catch (Exception e) { e.printStackTrace(); } } }.start(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.main, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }
参考文献:疯狂Android讲义(第2版) 李刚编著
原文地址:http://blog.csdn.net/qingdujun/article/details/39271479
标签:des android style blog http io os 使用 java
原文地址:http://blog.csdn.net/qingdujun/article/details/39271479