Skip to content

Commit

Permalink
Merge pull request #2621 from ccxm/master
Browse files Browse the repository at this point in the history
#6 第六次实验
zengsn authored Jun 2, 2018
2 parents 23f6761 + 1f5f4b6 commit 27c0eab
Showing 9 changed files with 123 additions and 1 deletion.
Binary file added soft1614080902419/6.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 soft1614080902419/6.2.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 soft1614080902419/6.3.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 soft1614080902419/6.4.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 soft1614080902419/6.5.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion soft1614080902419/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -2,6 +2,13 @@
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="edu.hzuapps.androidlabs.Soft1614080902419">

<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"
@@ -16,7 +23,14 @@
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name=".Second_Activity"></activity>
<activity android:name=".Second_Activity"
android:label="SecondActivity">
<intent-filter>
<action android:name="com.litreily.SecondActivity"/>

<category android:name="android.intent.category.DEFAULT"/>
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -1,28 +1,50 @@
package edu.hzuapps.androidlabs.Soft1614080902419;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStreamWriter;
import java.net.MalformedURLException;
import java.net.URL;

public class Second_Activity extends AppCompatActivity implements View.OnClickListener {
TextView t[] = new TextView[18];
TextView counting,result;

public TextView text;
private ImageView imageview;
private Bitmap bitmap;
Handler handler=new Handler(){
public void handleMessage(Message msg) {
if(msg.what==123){
imageview.setImageBitmap(bitmap);
}
};
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.second_activity);
this.RegisterEvent();
}
public void RegisterEvent(){
imageview=(ImageView) findViewById(R.id.image);
text = (TextView)findViewById(R.id.address);
counting = (TextView) findViewById(R.id.counting);
result = (TextView) findViewById(R.id.result);
String str = "R.id.n_0";
@@ -58,6 +80,7 @@ public void onClick(View view) {
String s = counting.getText().toString();
result.setText(s);
counting.setText(str1);
new Thread(download).start();
}
else if(t[i]==view){
String str = t[i].getText().toString();
@@ -87,4 +110,29 @@ public void save(String str,String str1){
}
}
}
Thread download = new Thread(){
public void run() {
String Path=text.getText().toString();//下载图片的路径
try {
URL url=new URL(Path);//对资源链接
InputStream inputStream=url.openStream();
bitmap= BitmapFactory.decodeStream(inputStream);//转换成位图图片
handler.sendEmptyMessage(123);
inputStream.close();
inputStream=url.openStream();
File file=new File(Environment.getExternalStorageDirectory()+"./DCIM/t1.jpg");
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();
}
}
};
}
14 changes: 14 additions & 0 deletions soft1614080902419/app/src/main/res/layout/second_activity.xml
Original file line number Diff line number Diff line change
@@ -247,4 +247,18 @@
android:textSize="30dp" />
</LinearLayout>

<TextView
android:id="@+id/address"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_x="0dp"
android:layout_y="0dp"
android:text="http://d7.yihaodianimg.com/N11/M07/B7/B5/ChEwoFkalXSAfX4kAABZ29FoNLc09100_600x600.jpg" />

<ImageView
android:id="@+id/image"
android:layout_width="182dp"
android:layout_height="177dp"
android:layout_x="87dp"
android:layout_y="185dp" />
</AbsoluteLayout>
46 changes: 46 additions & 0 deletions soft1614080902419/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
# 第六次实验
## 1.实验目的

1.掌握Android网络访问方法

2.理解XML和JSON表示数据的方法

## 2.实验要求

1.从网络下载一个文件

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

3.将应用运行结果截图

## 3.实验步骤

1.打开实验五的工程项目

2.AndroidManifest.xml中添加能够写入读出内存空间权限和网络连接权限的代码,其代码如下图:

![Image text](https://github.com/ccxm/android-labs-2018/blob/master/soft1614080902419/6.1.png)

3.在second_activity.xml中添加一个TextView,address用来存放图片的地址,一个ImageView,image用来显示图片,其代码如下图:

![Image text](https://github.com/ccxm/android-labs-2018/blob/master/soft1614080902419/6.2.png)

4.在Second_Activity.java中创建一个线程download,用来下载、存储及显示图片,其代码如下:

![Image text](https://github.com/ccxm/android-labs-2018/blob/master/soft1614080902419/6.3.png)

5.编译运行

6.打开git bash,将文件夹push上github,向老师提交pull request.

## 4.实验截图
按"="号前

![Image text](https://github.com/ccxm/android-labs-2018/blob/master/soft1614080902419/6.4.png)

按"="号后

![Image text](https://github.com/ccxm/android-labs-2018/blob/master/soft1614080902419/6.5.png)

## 6.实验心得
做好实验后,运行的时候一直按"="号,可是一直都没有图片,也没有报错,没有抛出异常,加了许多打印语句进去看看是否执行了下载的语句,发现确实又执行了。后来请教了同学,他看了代码也没有问题,然后突然发现我用的是github的图片。发现问题的所在,他说他之前用GitHub的图片也不行,后来再百度上找了一张,运行,按"=",可以了,图片出来了。

0 comments on commit 27c0eab

Please sign in to comment.