android从网上下载图片
16lz
2021-01-23
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下可以看到
更多相关文章
- Android图片上传工具类
- Android 自定义控件之图片裁剪
- android访问网络图片
- android checkbox 定制(修改checkbox 的图片)
- 【Android】简单图片浏览器
- android图像处理系列之三--图片色调饱和度、色相、亮度处理
- Android使用xml自定义图片
- Android 图片缩放与旋转