-
Notifications
You must be signed in to change notification settings - Fork 334
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #2283 from 545072985/master
- Loading branch information
Showing
8 changed files
with
149 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
70 changes: 70 additions & 0 deletions
70
soft1614080902204/app/src/main/java/com/example/gwl20/soft1614080902204/HttpThread.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
package com.example.gwl20.soft1614080902204; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Environment; | ||
import android.os.Handler; | ||
import android.widget.ImageView; | ||
import android.content.Context; | ||
import android.os.Environment; | ||
|
||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.MalformedURLException; | ||
import java.net.URL; | ||
|
||
public class HttpThread extends Thread { | ||
private String url; | ||
private ImageView imageView; | ||
private Handler handler; | ||
public HttpThread(String url, ImageView imageView,Handler handler){ | ||
this.url=url; | ||
this.imageView=imageView; | ||
this.handler=handler; | ||
} | ||
@Override | ||
public void run(){ | ||
try { | ||
URL httpUrl=new URL(url); | ||
HttpURLConnection conn=(HttpURLConnection)httpUrl.openConnection(); | ||
conn.setReadTimeout(5000); | ||
conn.setRequestMethod("GET"); | ||
conn.setDoInput(true); | ||
InputStream in=conn.getInputStream(); | ||
FileOutputStream out=null; | ||
File downloadFile=null; | ||
String fileName=String.valueOf(System.currentTimeMillis()); | ||
if(Environment.getExternalStorageState().equals(Environment.MEDIA_MOUNTED)){ | ||
File appDir=Environment.getExternalStorageDirectory(); | ||
downloadFile=new File(appDir,fileName); | ||
out=new FileOutputStream(downloadFile); | ||
} | ||
// String storePath = Environment.getExternalStorageDirectory().getAbsolutePath() + File.separator + "demo"; | ||
// File appDir = new File(storePath); | ||
// if (!appDir.exists()) { | ||
// appDir.mkdir(); } | ||
// downloadFile=new File(appDir,fileName); | ||
// out=new FileOutputStream(downloadFile); | ||
|
||
byte [] b=new byte[4*1024]; | ||
int len; | ||
if(out!=null) { | ||
while ((len = in.read(b)) != -1) { | ||
out.write(b,0,len); | ||
} | ||
} | ||
final Bitmap bitmap= BitmapFactory.decodeFile(downloadFile.getAbsolutePath()); | ||
handler.post(new Runnable() { | ||
@Override | ||
public void run() { | ||
imageView.setImageBitmap(bitmap); | ||
} | ||
}); | ||
} catch (java.io.IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
# 第六次实验 | ||
|
||
## 1.实验目标 | ||
|
||
(1)掌握Android网络访问方法; | ||
|
||
(2)理解XML和JSON表示数据的方法。 | ||
|
||
## 2.实验内容 | ||
(1)从网络下载一个文件(图片、MP3、MP4); | ||
|
||
(2)保存到手机,在应用中使用文件; | ||
|
||
(3)将应用运行结果截图。 | ||
|
||
## 3.实验步骤 | ||
(1)打开已建好的项目soft1614080902204 | ||
|
||
(2)在新建一个下载线程.java文件httpThread.java,用来下载图片,新建一个下载Button用来触发下载线程; | ||
|
||
(3)在httpThread文件中用URL和HttpURLConnection来连接图片所在的网络地址获得图片数据的输入流 | ||
|
||
(3)在本地创建一个文件并设置一个输出流用来保存图片数据,并用Bitmap形式保存图片并展示在ImageView中 | ||
|
||
(4)用handler.post来刷新ui; | ||
|
||
(5)完成并运行成功 | ||
|
||
(6)用git上交 | ||
|
||
## 4.实验结果 | ||
![实验截图](https://github.com/545072985/android-labs-2018/blob/master/soft1614080902204/sy6/tupian6.1.png) | ||
|
||
![实验结果](https://github.com/545072985/android-labs-2018/blob/master/soft1614080902204/sy6/tupian6.2.png) | ||
|
||
## 5.实验体会 | ||
这次实验让我学会了简用URL的相关用法来进行网络连接下载图片,并对文件创建和输入输出流有了更深一的理解。 |
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.