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

#5 #6第五和第六次实验和报告。 #2636

Merged
merged 4 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
Original file line number Diff line number Diff line change
Expand Up @@ -54,4 +54,5 @@ public boolean onOptionsItemSelected(MenuItem item) {

return super.onOptionsItemSelected(item);
}

}
Original file line number Diff line number Diff line change
@@ -1,13 +1,89 @@
package edu.hzuapps.androidlabs.com1614080901214;

import android.support.v7.app.AppCompatActivity;
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.support.v7.app.AppCompatActivity;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

public class ThreeActivity extends AppCompatActivity {

@Override
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_three);


editText=(EditText) findViewById(R.id.editText2);
button=(Button) findViewById(R.id.button3);
imageView=(ImageView) findViewById(R.id.imageView5);


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);
handler.sendEmptyMessage(111);
inputStream.close();


inputStream=url.openStream();
File file=new File(Environment.getExternalStorageDirectory()+"/haha.gif");
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();
}
};
};
}
Original file line number Diff line number Diff line change
@@ -1,33 +1,112 @@
package edu.hzuapps.androidlabs.com1614080901214;

import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.design.widget.Snackbar;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.Toolbar;

import java.io.BufferedInputStream;
import java.io.BufferedReader;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.io.OutputStreamWriter;

import edu.hzuapps.androidlabs.*;

import static android.provider.Telephony.Mms.Part.FILENAME;


public class jumpActivity extends AppCompatActivity {
public static final String DIRECTORY = "demo";
public static final String FILENAME = "demo.txt";

public static final String TAG = jumpActivity.class.getSimpleName();
private Button button;

@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_jump);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);

button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Intent intent = new Intent(jumpActivity.this, ThreeActivity.class);
startActivity(intent);
String text = ((EditText) findViewById(R.id.editText)).getText().toString();
save(text);

}


});
((Button) findViewById(R.id.button2)).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
String text = ((EditText) findViewById(R.id.editText)).getText().toString();
read();
deleteFile(FILENAME);
Intent intent = new Intent(jumpActivity.this, ThreeActivity.class);
startActivity(intent);


}
}
);
}
public void save(String text) {
FileOutputStream out = null;
BufferedWriter writer = null;
try {
out = openFileOutput("demo.txt", ContextWrapper.MODE_APPEND);
writer = new BufferedWriter(new OutputStreamWriter(out));
writer.write(text);
} catch (IOException e) {
e.printStackTrace();
} finally {
try {
if (writer != null) {
writer.close();
}
} catch (IOException e) {
e.printStackTrace();
}
}
Toast.makeText(jumpActivity.this, "已保存", Toast.LENGTH_SHORT).show();
}


public void read() {
String content = "";
try {
FileInputStream fis = openFileInput("demo.txt");
byte[] buffer = new byte[fis.available()];
fis.read(buffer);
content = new String(buffer);
fis.close();
} catch (Exception e) {
e.printStackTrace();
}
Toast.makeText(jumpActivity.this, "查询结果:" + content, Toast.LENGTH_SHORT).show();

}



}
17 changes: 13 additions & 4 deletions Com1614080901214/app/src/main/res/layout/activity_jump.xml
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,44 @@
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".jumpActivity">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:orientation="horizontal">


app:layout_constraintBaseline_toBaselineOf="@+id/editText"

<EditText
android:id="@+id/editText"
android:layout_width="252dp"
android:layout_width="281dp"
android:layout_height="wrap_content"
android:layout_marginTop="16dp"
android:layout_weight="1"
android:ems="10"
android:hint="请输入你要查询天气的城市"
android:inputType="textPersonName"
app:layout_constraintEnd_toStartOf="@+id/button">

</EditText>


<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:onClick="sendMessage"
android:text="查询">
android:text="保存">

</Button>
app:layout_constraintBaseline_toBaselineOf="@+id/editText"

<Button
android:id="@+id/button2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_weight="1"
android:text="查询" />
</LinearLayout>

</android.support.design.widget.CoordinatorLayout>
40 changes: 31 additions & 9 deletions Com1614080901214/app/src/main/res/layout/activity_three.xml
Original file line number Diff line number Diff line change
@@ -1,16 +1,38 @@
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
<RelativeLayout 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:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".ThreeActivity">
<TextView
android:id="@+id/textview_01"
android:orientation="vertical"
android:background="@drawable/tupian">

<EditText
android:id="@+id/editText2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:ems="10"
android:inputType="textPersonName"
android:text="输入url" />

<Button
android:id="@+id/button3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:drawableTop="@drawable/jd"
tools:layout_editor_absoluteX="0dp"
tools:layout_editor_absoluteY="0dp" />
android:layout_alignParentTop="true"
android:layout_centerHorizontal="true"
android:layout_marginTop="87dp"
android:text="下载图片" />

<ImageView
android:id="@+id/imageView5"
android:layout_width="328dp"
android:layout_height="299dp"
android:layout_alignParentBottom="true"
android:layout_alignParentStart="true"
android:layout_marginBottom="37dp"
android:layout_marginStart="33dp" />

</RelativeLayout>

</android.support.constraint.ConstraintLayout>
24 changes: 24 additions & 0 deletions Com1614080901214/report5.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
一、实验目的
掌握在Android App中存储数据;

二、实验要求
1.根据选题要求使用文件存储

2.将应用产生的数据保存到文件存储中;

3.说明使用的文件存储方式:内部 or 外部;

4.将运行结果截图。

三、实验步骤

1.新增了两个类,一个用来实现新建歌单收藏功能,输入收藏单名,将输入的数据存储到文件中。另一个类用来从文件中读取收藏单名并显示出来。

2.新增了这两个类相应的页面布局。

四、实验结果
https://github.com/quanquanCc/android-labs-2018/blob/master/Com1614080901214/%E5%AE%9E%E9%AA%8C5%E6%88%AA%E5%9B%BE.png
https://github.com/quanquanCc/android-labs-2018/blob/master/Com1614080901214/%E5%AE%9E%E9%AA%8C5%E6%88%AA%E5%9B%BE2.png

五、实验体会
通过这次实验我掌握了如何在在Android App中存储数据,也掌握了将应用产生的数据保存到文件存储中,也加深了对布局设计的理解
11 changes: 11 additions & 0 deletions Com1614080901214/report6.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
第六次实验报告
Android网络编程
一、实验步骤
1.修改AndroidManifest,加入必要的连接网络的权限。
2.创建activity_tp.java,其功能为完成图片的下载。
3.创建activity_tp.xml文件,实现图片的下载和显示
二、实验结果截图
https://github.com/quanquanCc/android-labs-2018/blob/master/Com1614080901214/%E5%AE%9E%E9%AA%8C6%E6%88%AA%E5%9B%BE.jpg
三、实验体会
通过本次实验,我认识到当设置连接超时,如果网络不好,Android系统在超过默认时间会收回资源中断操作.还有就是在对大文件的操作时,要将文件写到SDCard上面,
不要直接写到手机内存上。
Binary file added Com1614080901214/实验5截图.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 Com1614080901214/实验5截图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 Com1614080901214/实验6截图.jpg
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.