From c08e289d9ae74809b97d6755341e4e137a0f8258 Mon Sep 17 00:00:00 2001 From: zhanghw <32947528+quanquanCc@users.noreply.github.com> Date: Sat, 2 Jun 2018 14:18:38 +0800 Subject: [PATCH 1/3] #5.6 MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 实验5.6 --- .../Com1614080901214Activity.java | 1 + .../com1614080901214/ThreeActivity.java | 80 ++++++++++++++- .../com1614080901214/jumpActivity.java | 93 ++++++++++++++++-- .../app/src/main/res/layout/activity_jump.xml | 17 +++- .../src/main/res/layout/activity_three.xml | 40 ++++++-- Com1614080901214/report5.md | 43 ++++++++ Com1614080901214/report6.md | 43 ++++++++ ...\351\252\2145\346\210\252\345\233\276.png" | Bin 0 -> 45025 bytes ...351\252\2145\346\210\252\345\233\2762.png" | Bin 0 -> 656832 bytes ...\351\252\2146\346\210\252\345\233\276.jpg" | Bin 0 -> 138421 bytes 10 files changed, 295 insertions(+), 22 deletions(-) create mode 100644 Com1614080901214/report5.md create mode 100644 Com1614080901214/report6.md create mode 100644 "Com1614080901214/\345\256\236\351\252\2145\346\210\252\345\233\276.png" create mode 100644 "Com1614080901214/\345\256\236\351\252\2145\346\210\252\345\233\2762.png" create mode 100644 "Com1614080901214/\345\256\236\351\252\2146\346\210\252\345\233\276.jpg" diff --git a/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/Com1614080901214Activity.java b/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/Com1614080901214Activity.java index f0f231725..1b9df5ad9 100644 --- a/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/Com1614080901214Activity.java +++ b/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/Com1614080901214Activity.java @@ -54,4 +54,5 @@ public boolean onOptionsItemSelected(MenuItem item) { return super.onOptionsItemSelected(item); } + } diff --git a/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/ThreeActivity.java b/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/ThreeActivity.java index bce05c05a..c8088ed4a 100644 --- a/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/ThreeActivity.java +++ b/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/ThreeActivity.java @@ -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(); + } + }; + }; } diff --git a/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/jumpActivity.java b/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/jumpActivity.java index 00ed89faf..79aa86946 100644 --- a/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/jumpActivity.java +++ b/Com1614080901214/app/src/main/java/edu/hzuapps/androidlabs/com1614080901214/jumpActivity.java @@ -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(); + + } + + + } \ No newline at end of file diff --git a/Com1614080901214/app/src/main/res/layout/activity_jump.xml b/Com1614080901214/app/src/main/res/layout/activity_jump.xml index 63594d5ba..a600cf8eb 100644 --- a/Com1614080901214/app/src/main/res/layout/activity_jump.xml +++ b/Com1614080901214/app/src/main/res/layout/activity_jump.xml @@ -5,17 +5,21 @@ android:layout_width="match_parent" android:layout_height="match_parent" tools:context=".jumpActivity"> + + app:layout_constraintBaseline_toBaselineOf="@+id/editText" + - - app:layout_constraintBaseline_toBaselineOf="@+id/editText" +