Skip to content

Commit

Permalink
Merge pull request #2252 from hejianwens/master
Browse files Browse the repository at this point in the history
#6 #975第六次实验
  • Loading branch information
zengsn authored May 26, 2018
2 parents 3d072f5 + c8521ef commit c6c37f9
Show file tree
Hide file tree
Showing 5 changed files with 165 additions and 0 deletions.
30 changes: 30 additions & 0 deletions Soft1614080902305/shiyan6/#6 第六次实验.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
一.实验目的

掌握Android网络访问方法;

二.实验内容

从网络下载一个文件(图片、MP3、MP4);

保存到手机,在应用中使用文件;

将应用运行结果截图。

三.实验步骤

1.先创建一个新的Activity负责下载图片

2.创建按钮,并为之加一个监听器启动线程负责下载图片

3.在xml文件上创建好ImageView,button和输入框

4.在Activity线程上完善下载代码
四.实验截图

![screen](https://github.com/hejianwens/android-labs-2018/blob/master/Soft1614080902305/shiyan6/%E4%B8%8B%E8%BD%BD%E5%89%8D%E7%95%8C%E9%9D%A2.png?raw=true)

![screen](https://github.com/hejianwens/android-labs-2018/blob/master/Soft1614080902305/shiyan6/%E4%B8%8B%E8%BD%BD%E5%AE%8C%E6%88%90%E5%90%8E%E7%95%8C%E9%9D%A2.png?raw=true)


五 .实验体会
通过 这次实验。学会了启动匿名 Thread,并调用其 start() 启动运行线程,在这个线程中进行图像下载并解码成 Bitmap,然后通过 Handler 向 UI 线程发送消息以通知下载结果。这都是在匿名 Thead 中处理的。主线程也就是 UI 线程收到消息之后,会分发给 Handler,在它的 handleMessage 方法中根据消息 id 来处理下载结果,要么成功要么失败,并相应地更新 UI,传给imageView把图片显示出来
102 changes: 102 additions & 0 deletions Soft1614080902305/shiyan6/Soft1614080902305Activity3.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,102 @@
package edu.hzuapps.androidlabs.soft1614080902305;

import android.os.Bundle;
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 java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.MalformedURLException;
import java.net.URL;

import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class Soft1614080902305Activity3 extends AppCompatActivity {
private EditText editText;
private Button button;
private ImageView imageView;
private Bitmap bitmap;

Handler h;

{
h= new Handler() {
public void handleMessage(Message msg) {
if (msg.what == 111) {
imageView.setImageBitmap(bitmap);
}
}

;
};
}

protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_soft16140809023053);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
setContentView(R.layout.activity_soft16140809023053);
editText = (EditText) findViewById(R.id.imagepath);
button = (Button) findViewById(R.id.upload);
imageView = (ImageView) findViewById(R.id.imageView);
button.setOnClickListener(new OnClickListener() {
public void onClick(View arg0) {
new Thread(t).start();
}
});
}

//为了下载图片资源,开辟一个新的子线程
Thread t=new Thread(){
public void run() {
//下载图片的路径
String iPath=editText.getText().toString();
try {
//对资源链接
URL url=new URL(iPath);
//打开输入流
InputStream inputStream=url.openStream();
//对网上资源进行下载转换位图图片
bitmap=BitmapFactory.decodeStream(inputStream);
h.sendEmptyMessage(111);
inputStream.close();

//再一次打开
inputStream=url.openStream();
File file=new File(Environment.getExternalStorageDirectory()+"/DCIM/");
FileOutputStream fileOutputStream=new FileOutputStream(file);
int hasRead=0;
while((hasRead=inputStream.read())!=-1){
fileOutputStream.write(hasRead);
}
fileOutputStream.close();
inputStream.close();
} catch (MalformedURLException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
};
};


}

33 changes: 33 additions & 0 deletions Soft1614080902305/shiyan6/activity_soft16140809023053.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout
xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="match_parent"
android:layout_height="match_parent">
<ImageView
android:id="@+id/imageView"

android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_x="3dp"
android:layout_y="141dp"
/>

<EditText
android:id="@+id/imagepath"
android:layout_width="fill_parent"
android:layout_height="74dp"
android:layout_alignBottom="@id/user"
android:layout_toRightOf="@id/user"
android:layout_x="0dp"
android:layout_y="0dp"

/>

<Button
android:id="@+id/upload"
android:layout_width="411dp"
android:layout_height="68dp"
android:layout_x="3dp"
android:layout_y="70dp"
android:text="下载"
/>
</AbsoluteLayout>
Binary file added Soft1614080902305/shiyan6/下载前界面.png
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.

0 comments on commit c6c37f9

Please sign in to comment.