Skip to content

Commit

Permalink
Merge pull request #2619 from Im-a-programmer/master
Browse files Browse the repository at this point in the history
#6 #1439  第六次实验及报告
  • Loading branch information
zengsn authored Jun 2, 2018
2 parents 315d3a8 + 5c4aa18 commit ce3a2e0
Show file tree
Hide file tree
Showing 9 changed files with 208 additions and 5 deletions.
Binary file added com1614080901140/6.0.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 com1614080901140/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 com1614080901140/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.
16 changes: 13 additions & 3 deletions com1614080901140/app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
@@ -1,21 +1,31 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.com1614080901140">
package="edu.hzuapps.androidlabs.com1614080901140">


<uses-permission android:name="android.permission.INTERNET" />
<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="edu.hzuapps.andriodlabs.com1614080901140.Com1614080901140Activity">
<activity android:name=".Com1614080901140Activity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity android:name="edu.hzuapps.andriodlabs.com1614080901140.SecondActivity"></activity>
<activity
android:name=".SecondActivity"
android:label="@string/title_activity_second"
android:theme="@style/AppTheme.NoActionBar" />
<activity
android:name=".ThirdActivity"
android:label="@string/title_activity_third"
android:theme="@style/AppTheme.NoActionBar"></activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,12 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStreamWriter;

import android.content.Intent;
import android.widget.Toast;

public class SecondActivity extends AppCompatActivity {

private Button nextbtn;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Expand All @@ -43,7 +44,14 @@ public void onClick(View v) {
readsy5();
}
});

nextbtn=(Button)findViewById(R.id.btn_t);
nextbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent=new Intent(SecondActivity.this,ThirdActivity.class);
startActivity(intent);
}
});

}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,115 @@
package edu.hzuapps.androidlabs.com1614080901140;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import java.io.File;
import java.io.FileOutputStream;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import android.os.AsyncTask;
import android.os.Environment;
import android.os.Handler;
import android.os.Message;
import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.Toast;


public class ThirdActivity extends AppCompatActivity implements OnClickListener{

Button content;
ImageView image;
Bitmap bitmap;
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_third);
content=(Button)findViewById(R.id.content);
image=(ImageView)findViewById(R.id.image);

content.setOnClickListener(this);;
image.setOnClickListener(this);;
}

public Bitmap GetImageInputStream(String imageurl){
URL url;
HttpURLConnection connection=null;
Bitmap bitmap=null;
try {
url = new URL(imageurl);
connection=(HttpURLConnection)url.openConnection();
connection.setConnectTimeout(6000);
connection.setDoInput(true);
connection.setUseCaches(false);
InputStream inputStream=connection.getInputStream();
bitmap=BitmapFactory.decodeStream(inputStream);
inputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
return bitmap;
}

public void onClick(View v) {
switch (v.getId()) {
case R.id.content:

new Task().execute("http://img.szhome.com/images/sznews/2012/07/20120703105442315.JPG");
break;

case R.id.image:

SavaImage(bitmap, Environment.getExternalStorageDirectory().getPath()+"/Test");
Toast.makeText(getBaseContext(), "图片保存", Toast.LENGTH_SHORT).show();
break;

default:
break;
}
}

Handler handler=new Handler(){
public void handleMessage(android.os.Message msg) {
if(msg.what==0x123){
image.setImageBitmap(bitmap);
}
};
};


class Task extends AsyncTask<String, Integer, Void>{

protected Void doInBackground(String... params) {
bitmap=GetImageInputStream((String)params[0]);
return null;
}

protected void onPostExecute(Void result) {
super.onPostExecute(result);
Message message=new Message();
message.what=0x123;
handler.sendMessage(message);
}

}

public void SavaImage(Bitmap bitmap, String path){
File file=new File(path);
FileOutputStream fileOutputStream=null;
if(!file.exists()){
file.mkdir();
}
try {
fileOutputStream=new FileOutputStream(path+"/"+System.currentTimeMillis()+".png");
bitmap.compress(Bitmap.CompressFormat.JPEG, 100,fileOutputStream);
fileOutputStream.close();
} catch (Exception e) {
e.printStackTrace();
}
}
}
12 changes: 12 additions & 0 deletions com1614080901140/app/src/main/res/layout/activity_second.xml
Original file line number Diff line number Diff line change
Expand Up @@ -49,4 +49,16 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintTop_toTopOf="parent"
app:layout_constraintVertical_bias="0.134" />

<Button
android:id="@+id/btn_t"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="17dp"
android:layout_marginEnd="30dp"
android:layout_marginRight="30dp"
android:text="铁路路线"/>
</RelativeLayout>
33 changes: 33 additions & 0 deletions com1614080901140/app/src/main/res/layout/activity_third.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="edu.hzuapps.androidlabs.com1614080901140.ThirdActivity"
android:orientation="vertical"
>

<ImageView
android:id="@+id/image"
android:layout_width="fill_parent"
android:layout_height="450dp"
android:layout_alignParentLeft="true"
android:layout_alignParentStart="true"
android:layout_alignParentTop="true"
android:layout_marginTop="22dp" />

<Button
android:id="@+id/content"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentBottom="true"
android:layout_alignParentEnd="true"
android:layout_alignParentRight="true"
android:layout_marginBottom="20dp"
android:layout_marginEnd="68dp"
android:layout_marginRight="68dp"
android:background="#ff8c00"
android:text="路线查看" />

</RelativeLayout>

25 changes: 25 additions & 0 deletions com1614080901140/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
实验六

一.实验目的
掌握Android网络访问方法;

二.实验内容
从网络下载一个文件(图片、MP3、MP4);
保存到手机,在应用中使用文件;
将应用运行结果截图;

三.实验步骤
1.修改AndroidManifest.xml文件,获取连接网络的权限;
2.重新建立一个activity文件,在相应的xml文件中加入button,点击就会下载相应的图片;
3.实现图片的下载

四.实验截图
![screen](https://github.com/Im-a-programmer/android-labs-2018/blob/master/com1614080901140/6.2.png)
![screen](https://github.com/Im-a-programmer/android-labs-2018/blob/master/com1614080901140/6.1.png)
![screen](https://github.com/Im-a-programmer/android-labs-2018/blob/master/com1614080901140/6.0.png)


五.实验体会
这次实验的难点在于设置app访问网络的权限以及下载图片的功能实现,刚开始时输入几个图片链接发现在apk文件上都下载不下来还以为是代码出错,
后来再重试几次发现是图片问题,有的图片能下载成功,有一些则不能,虽然现在还搞不懂是什么原因造成的,但在以后的自主学习中我相信这些问题都会被解决的。

0 comments on commit ce3a2e0

Please sign in to comment.