package com.example.exp14_5_1;import java.io.File;import java.io.FileOutputStream;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.os.Bundle;import android.os.Handler;import android.os.Message;import android.view.Menu;import android.view.MenuItem;import android.view.View;import android.view.View.OnClickListener;import android.widget.Button;import android.widget.EditText;import android.widget.Toast;public class MainActivity extends Activity {private Button  button ;private EditText urlText;private Handler handler;private boolean   flag=false;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);setContentView(R.layout.activity_main);urlText=(EditText)findViewById(R.id.editText_url);button=(Button)findViewById(R.id.button_go);button.setOnClickListener(new OnClickListener() {@Overridepublic void onClick(View v) {// TODO Auto-generated method stubnew Thread (new Runnable() {@Overridepublic void run() {// TODO Auto-generated method stubString urlStr = urlText.getText().toString();URL url;try {url = new URL(urlStr);HttpURLConnection  urlConn = (HttpURLConnection)url.openConnection();InputStream is = urlConn.getInputStream();if(is!=null){String expandName = urlStr.substring(urlStr.lastIndexOf(".")+1, urlStr.length()).toLowerCase();String fileName = urlStr.substring(urlStr.lastIndexOf("/")+1, urlStr.lastIndexOf("."));File file =new File("/sdcard/pictures/"+fileName+"."+expandName);FileOutputStream fos = new  FileOutputStream(file);byte[] by = new byte[1024];while(true){int len=is.read(by);if(len<0){break;}else{fos.write(by, 0, len);}}}is.close();urlConn.disconnect();flag=true;} catch (MalformedURLException e) {// TODO Auto-generated catch blockflag=false;e.printStackTrace();} catch (IOException e) {// TODO Auto-generated catch blockflag=false;e.printStackTrace();}Message m = handler.obtainMessage();handler.sendMessage(m);}}).start();}});handler = new Handler(){  @Overridepublic void handleMessage(Message msg) {// TODO Auto-generated method stubsuper.handleMessage(msg);if(flag==true){Toast.makeText(MainActivity.this, "下载成功", Toast.LENGTH_SHORT).show();}else{Toast.makeText(MainActivity.this, "下载失败", Toast.LENGTH_SHORT).show();}}};}}
<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"    tools:context="${relativePackage}.${activityClass}" >    <Button        android:id="@+id/button_go"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignParentRight="true"        android:layout_alignParentTop="true"        android:text="下載" />    <EditText        android:id="@+id/editText_url"        android:layout_width="wrap_content"        android:layout_height="wrap_content"        android:layout_alignBottom="@+id/button_go"        android:layout_alignParentLeft="true"        android:layout_toLeftOf="@+id/button_go"        android:ems="10"        android:lines="3"        android:text="http://192.168.0.165:8080/bbs/images/head1.jpg"        android:inputType="textPostalAddress" /></RelativeLayout>

图片放置在http://192.168.0.165:8080/bbs/images/head1.jpg,在tomcat里,

图片下载在mnt/sdcard/pictures下,在DDMS FileExplorer下可以看到


更多相关文章

  1. android---------ndk中的各个版本的下载地址。
  2. Android源码下载
  3. Android应用开发相关下载资源(2016/07/24更新)
  4. Android文件下载使用Http协议
  5. Android下载网络图片到本地
  6. Android(安卓)2.2.2到Android(安卓)4.2.2源码下载地址
  7. Android下载网络图片到本地
  8. Android(安卓)Studio 1.5.1最新版下载地址
  9. Android源码下载

随机推荐

  1. android 日志文件输出
  2. android的常用控件总结【安卓入门五】
  3. 2019-03-06 水波纹
  4. Android(安卓)SQLite总结[转载]
  5. android下使用icon
  6. Android做了些什么?
  7. 我的Android进阶之旅------>Android视频
  8. Android maxLines maxLength属性
  9. 改变Android屏幕默认启动方向为横向
  10. android listview 设置点击效果selector