Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#6 #1020 实验六 #2657

Merged
merged 6 commits into from
Jun 2, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions soft1614080902442/app/java/soft1614080902442main5Activity.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package edu.hzuapps.androidlabs.soft1614080902442;
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 soft1614080902442main5Activity extends Activity{
private EditText editText;
private Button button;
private ImageView imageView;

private Bitmap bitmap;
//手柄更新的作用
Handler handler=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_soft1614080902442main5);

//初始化组件
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();//启动线程t
}
});
}

//为了下载图片资源,开辟一个新的子线程,线程名字为t
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);
handler.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();
}
};
};

}
16 changes: 7 additions & 9 deletions soft1614080902442/app/mainfests/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -9,21 +9,19 @@
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".soft1614080902442main1Activity">
<activity android:name=".soft1614080902442main5Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".soft1614080902442main2Activity"
android:label="@string/title_activity_soft1614080902442main2"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".soft1614080902442main3Activity"
android:label="@string/title_activity_soft1614080902442main3"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>
<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" />

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
<?xml version="1.0" encoding="utf-8"?>

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical" >

<EditText
android:id="@+id/imagepath"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="110dp"
android:text="请输入Picture的url地址"
android:textColor="#7D26CD" />

<Button
android:id="@+id/upload"
android:layout_width="wrap_content"
android:layout_height="37dp"
android:layout_alignParentBottom="true"
android:layout_centerHorizontal="true"
android:layout_marginBottom="55dp"
android:text="显示图片"
android:textColor="#7D26CD" />

<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="272dp"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true" />

</RelativeLayout>
34 changes: 34 additions & 0 deletions soft1614080902442/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
# 实验六

## 一.实验目的

掌握Android网络访问方法;

## 二.实验内容

(1)从网络下载一个文件(图片、MP3、MP4)
(2)保存到手机,在应用中使用文件
(3)将应用运行结果截图

## 三.实验步骤

1.首先确定实验内容为从网上下载一张图片,之后新建一个activity

2.在AndroidManifest.xml-中加入uses-permission的设置代码段

3.在activity-soft1614080902442main5里,先设置一个edit文本框来输入图片网址,然后设置按钮显示图片来打开从网上下载的图片

4.在java文件中先导入所需的包,设置对显示图片按钮的监听,再运行线程T

5.运行,从网上找图片的网址,复制,之后在文本框上paste该网址,之后点击显示图片,可以看到图片显示了出来

## 四.实验截图

![screen](https://github.com/cruiji/android-labs-2018/blob/master/soft1614080902442/实验六图一.jpg)
![screen](https://github.com/cruiji/android-labs-2018/blob/master/soft1614080902442/实验六图二.jpg)


## 五.实验体会

这次实验,通过在网上学习的一些如何网络访问的方法,可是试了一两种之后代码都会出现一些错误无法运行,最后跟舍友学习了另一种如何访问的方法后,确实可以访问成功,
通过该次试验,学习到了很多新知识,接下来会自己去了解其他代码无法运行的原因,再接再厉
Binary file added soft1614080902442/实验六图一.jpg
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 soft1614080902442/实验六图二.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.