Skip to content

Commit

Permalink
Merge pull request #2283 from 545072985/master
Browse files Browse the repository at this point in the history
#6 #882 第六次实验+报告
  • Loading branch information
zengsn authored May 26, 2018
2 parents 0aac5e0 + 3bcdb83 commit bf9d4a3
Show file tree
Hide file tree
Showing 8 changed files with 149 additions and 10 deletions.
9 changes: 8 additions & 1 deletion soft1614080902204/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.gwl20.soft1614080902204">

<uses-permission android:name="android.permission.INTERNET" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_WIFI_STATE" />
<uses-permission android:name="android.permission.CHANGE_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />

<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
Expand All @@ -23,7 +30,7 @@
<activity
android:name=".soft1614080902204CActivity"
android:label="@string/title_activity_soft1614080902204_c"
android:theme="@style/AppTheme.NoActionBar"></activity>
android:theme="@style/AppTheme.NoActionBar" />
</application>

</manifest>
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();
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,17 @@

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
Expand All @@ -18,13 +21,16 @@
public class soft1614080902204BActivity extends AppCompatActivity {

private Button bu,jiandao,shitou;
private ImageView beijin;
private Handler handler=new Handler();
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft1614080902204_b);
bu=(Button)findViewById(R.id.button_bu);
jiandao=(Button)findViewById(R.id.button_jiandao);
shitou=(Button)findViewById(R.id.button_shitou);
beijin=(ImageView)findViewById(R.id.imageView_beijin);
bu.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Expand All @@ -49,7 +55,12 @@ public void onClick(View v) {
startActivityForResult(intent,1);
}
});

((Button)findViewById(R.id.button_xiazai)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
new HttpThread("http://5b0988e595225.cdn.sohucs.com/images/20170904/d03b058017fc42568e4b595f40879ba1.jpeg",beijin,handler).start();
}
});
}
@Override

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,18 +14,31 @@
tools:layout_editor_absoluteY="0dp"
android:background="#F5fffa">
<ImageView
android:id="@+id/imageView_beijin"
android:layout_width="match_parent"
android:layout_height="250dp"
android:background="@drawable/timg"
/>
<TextView
android:background="@drawable/timg"/>
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:text="请选择要出的拳"
android:textColor="#636363"
android:textStyle="bold"
android:textSize="28sp"
android:background="#F5fffa"/>
android:orientation="horizontal">
<TextView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="请选择要出的拳"
android:textColor="#636363"
android:textStyle="bold"
android:textSize="28sp"
android:background="#F5fffa"/>
<Button
android:id="@+id/button_xiazai"
android:layout_marginLeft="60dp"
android:layout_marginRight="20dp"
android:layout_width="match_parent"
android:layout_height="45dp"
android:text="下载背景"/>
</LinearLayout>


<LinearLayout
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions soft1614080902204/app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@
<string name="app_name">soft1614080902204</string>
<string name="title_activity_soft1614080902204_b">soft1614080902204BActivity</string>
<string name="title_activity_soft1614080902204_c">soft1614080902204CActivity</string>
<string name="title_activity_soft1614080902204_d">soft1614080902204DActivity</string>
</resources>
37 changes: 37 additions & 0 deletions soft1614080902204/report6.md
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的相关用法来进行网络连接下载图片,并对文件创建和输入输出流有了更深一的理解。
Binary file added soft1614080902204/sy6/tupian6.1.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added soft1614080902204/sy6/tupian6.2.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit bf9d4a3

Please sign in to comment.