-
Notifications
You must be signed in to change notification settings - Fork 332
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 #2629 from JingZXian/master
#6 第6次实验加报告!
- Loading branch information
Showing
6 changed files
with
170 additions
and
2 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
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,34 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<manifest xmlns:android="http://schemas.android.com/apk/res/android" | ||
package="edu.hzuapps.androidlabs.com1614080901230"> | ||
<!-- 获取权限 --> | ||
<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" | ||
android:label="@string/app_name" | ||
android:roundIcon="@mipmap/ic_launcher_round" | ||
android:supportsRtl="true" | ||
android:theme="@style/AppTheme"> | ||
|
||
<activity android:name=".ConnectwebActivity"> | ||
<intent-filter> | ||
<action android:name="android.intent.action.MAIN" /> | ||
|
||
<category android:name="android.intent.category.LAUNCHER" /> | ||
</intent-filter> | ||
</activity> | ||
<activity | ||
android:name=".SecondActivity" | ||
android:label="@string/title_activity_second" | ||
android:theme="@style/AppTheme.NoActionBar"></activity> | ||
|
||
</application> | ||
|
||
</manifest> |
77 changes: 77 additions & 0 deletions
77
...01230/app3/src/main/java/edu/hzuapps/androidlabs/com1614080901230/ConnectwebActivity.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,77 @@ | ||
package edu.hzuapps.androidlabs.com1614080901230; | ||
|
||
import android.graphics.Bitmap; | ||
import android.graphics.BitmapFactory; | ||
import android.os.Bundle; | ||
import android.os.Handler; | ||
import android.os.Message; | ||
import android.support.v7.app.AppCompatActivity; | ||
import android.view.View; | ||
import android.widget.Button; | ||
import android.widget.ImageView; | ||
|
||
import java.io.InputStream; | ||
import java.net.HttpURLConnection; | ||
import java.net.URL; | ||
|
||
public class ConnectwebActivity extends AppCompatActivity { | ||
ImageView imageView; | ||
private String url = "http://p5.so.qhimgs1.com/bdr/200_200_/t01e021424fab162fab.jpg"; | ||
//在消息队列中实现对控件的更改 | ||
private Handler handle = new Handler() { | ||
public void handleMessage(Message msg) { | ||
switch (msg.what) { | ||
case 0: | ||
Bitmap bmp=(Bitmap)msg.obj; | ||
imageView.setImageBitmap(bmp); | ||
break; | ||
} | ||
}; | ||
}; | ||
|
||
@Override | ||
protected void onCreate(Bundle savedInstanceState) { | ||
super.onCreate(savedInstanceState); | ||
setContentView(R.layout.activity_connect_web); | ||
imageView = (ImageView) findViewById(R.id.my_view); | ||
Button btn=(Button) findViewById(R.id.btn5); | ||
|
||
btn.setOnClickListener(new View.OnClickListener() { | ||
@Override | ||
public void onClick(View v) { | ||
//新建线程加载图片信息,发送到消息队列中 | ||
new Thread(new Runnable() { | ||
@Override | ||
public void run() { | ||
// TODO Auto-generated method stub | ||
Bitmap bmp = getURLimage(url); | ||
Message msg = new Message(); | ||
msg.what = 0; | ||
msg.obj = bmp; | ||
handle.sendMessage(msg); | ||
} | ||
}).start(); | ||
}}); | ||
} | ||
|
||
|
||
//加载图片 | ||
public Bitmap getURLimage(String url) { | ||
Bitmap bmp = null; | ||
try { | ||
URL myurl = new URL(url); | ||
// 获得连接 | ||
HttpURLConnection conn = (HttpURLConnection) myurl.openConnection(); | ||
conn.setConnectTimeout(6000);//设置超时 | ||
conn.setDoInput(true); | ||
conn.setUseCaches(false);//不缓存 | ||
conn.connect(); | ||
InputStream is = conn.getInputStream();//获得图片的数据流 | ||
bmp = BitmapFactory.decodeStream(is); | ||
is.close(); | ||
} catch (Exception e) { | ||
e.printStackTrace(); | ||
} | ||
return bmp; | ||
} | ||
} |
35 changes: 35 additions & 0 deletions
35
com1614080901230/app3/src/main/res/layout/activity_connect_web.xml
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,35 @@ | ||
<?xml version="1.0" encoding="utf-8"?> | ||
<android.support.constraint.ConstraintLayout | ||
xmlns:android="http://schemas.android.com/apk/res/android" | ||
xmlns:app="http://schemas.android.com/apk/res-auto" | ||
xmlns:tools="http://schemas.android.com/tools" | ||
android:id="@+id/activity_connect_web" | ||
android:layout_width="match_parent" | ||
android:layout_height="match_parent" | ||
tools:context="edu.hzuapps.androidlabs.com1614080901230.ConnectwebActivity"> | ||
|
||
<ImageView | ||
android:layout_width="wrap_content" | ||
android:layout_height="wrap_content" | ||
android:id="@+id/my_view"/> | ||
|
||
<Button | ||
android:id="@+id/btn5" | ||
android:layout_width="150dp" | ||
android:layout_height="150dp" | ||
android:layout_alignParentBottom="true" | ||
android:layout_centerHorizontal="true" | ||
android:layout_marginBottom="8dp" | ||
android:layout_marginEnd="8dp" | ||
android:layout_marginLeft="8dp" | ||
android:layout_marginRight="8dp" | ||
android:layout_marginStart="8dp" | ||
android:layout_marginTop="8dp" | ||
android:text="下载图片" | ||
app:layout_constraintBottom_toBottomOf="parent" | ||
app:layout_constraintEnd_toEndOf="parent" | ||
app:layout_constraintStart_toStartOf="parent" | ||
app:layout_constraintTop_toTopOf="parent" /> | ||
> | ||
|
||
</android.support.constraint.ConstraintLayout> |
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,22 @@ | ||
# 第六次实验 | ||
## 一.实验目的 | ||
1.掌握Android网络访问方法; | ||
2.理解XML和JSON表示数据的方法。 | ||
|
||
## 二.实验内容 | ||
1.通过url下载一张照片 | ||
2.将照片显示到ImagView上 | ||
3.实验截图 | ||
|
||
## 三.实验步骤 | ||
1.通过新建一个新线程,加载图片 | ||
2.将加载的图片信息发送到消息队列中 | ||
3.注意点:网络图片通过转化inputStream流,在转化为bitMap,最后将bitMap传给imageView | ||
4.在消息队列中进行UI修改 | ||
5.然后下载图片,把它作为背景图 | ||
6.运行代码截图并用git上交相关文件。 | ||
|
||
## 四.实验截图 | ||
 | ||
## 五.实验体会 | ||
这次实验运用了许多的网络编程和Java的thread多线程的知识,通过百度的查找,学会一点一点的java中线程的用法。同时还用到实验五的做法,加深了存储编程的印象,更加熟悉了对文件输入输出流的操作。让我知道怎样简单地在安卓上获取网络上的资源,并保存在本地空间。 |