diff --git a/android-labs-2018 b/android-labs-2018 new file mode 160000 index 000000000..4cb432fa0 --- /dev/null +++ b/android-labs-2018 @@ -0,0 +1 @@ +Subproject commit 4cb432fa0607c734a8152173ee91698f7cee4af1 diff --git a/soft1613071002206/app/src/main/AndroidManifest.xml b/soft1613071002206/app/src/main/AndroidManifest.xml new file mode 100644 index 000000000..ab3d70e0c --- /dev/null +++ b/soft1613071002206/app/src/main/AndroidManifest.xml @@ -0,0 +1,29 @@ + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Main2Activity.java b/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Main2Activity.java new file mode 100644 index 000000000..db28b4407 --- /dev/null +++ b/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Main2Activity.java @@ -0,0 +1,98 @@ +package edu.hzuapps.androidlabs2018.soft1613071002206; + +import android.os.Bundle; +import android.support.design.widget.FloatingActionButton; +import android.support.design.widget.Snackbar; +import android.support.v7.app.AppCompatActivity; +import android.support.v7.widget.Toolbar; +import android.view.View; +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.view.View; +import android.view.View.OnClickListener; +import android.widget.Button; +import android.widget.EditText; +import android.widget.ImageView; + +public class Main2Activity extends AppCompatActivity { + private EditText editText; + private Button button; + private ImageView imageView; + private Bitmap bitmap; + + Handler h; + + { + h= 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_main2); + + editText = (EditText) findViewById(R.id.url); + button = (Button) findViewById(R.id.button2); + imageView = (ImageView) findViewById(R.id.imageView); + 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); + h.sendEmptyMessage(111); + inputStream.close(); + + //再一次打开 + inputStream=url.openStream(); + File file=new File(Environment.getExternalStorageDirectory()+"/DCIM/"); + 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/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Main3Activity.java b/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Main3Activity.java new file mode 100644 index 000000000..cc9848396 --- /dev/null +++ b/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Main3Activity.java @@ -0,0 +1,204 @@ +package edu.hzuapps.androidlabs2018.soft1613071002206; + +import android.content.Context; +import android.os.Bundle; +import android.os.Environment; + +import android.support.v7.app.AppCompatActivity; +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 java.io.BufferedInputStream; +import java.io.BufferedReader; +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 edu.hzuapps.androidlabs2018.soft1613071002206.R; + +public class Main3Activity extends AppCompatActivity { + + public static final String DIRECTORY = "demo"; + public static final String FILENAME = "file_demo.txt"; + + public static final String TAG = Main3Activity.class.getSimpleName(); + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main3); + + ((Button) findViewById(R.id.btn2)).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View view) { + String text = ((EditText) findViewById(R.id.edit)).getText().toString(); + saveTextIntoInternalStorage(text); + } + }); + } + + // 将文字保存到内部存储 + private void saveTextIntoInternalStorage(String text) { + // 获取内部存储目录 + File dir = this.getFilesDir(); + //File dir = getCacheDir(); + + if (dir.isDirectory()) { +// dir.mkdir(); +// dir.mkdirs(); + } + + if (dir.isFile()) { + // D:/Abc.txt , -> D:/Abc1.txt ->D:/abc.txt + } + + File file = new File(dir, FILENAME); + +// try { +// File = File.createTempFile(FILENAME, null, dir); +// } catch (IOException e) { +// e.printStackTrace(); +// } + + if (file.exists()) { // 判断文件是否存在 + Log.i(TAG, file.getAbsolutePath()); + Log.i(TAG, file.length() + ""); // bytes*1024=kb *1024 MB + Log.i(TAG, file.isFile() + ""); + file.canRead(); + file.canWrite(); + file.canExecute(); + + file.getFreeSpace(); + file.getTotalSpace(); + } + + FileOutputStream fos = null; // 字节流 | char | cn : gbk 2 bytes, utf8 3 bytes + + try { // 使用API打开输出流 + fos = openFileOutput(FILENAME, MODE_PRIVATE); + //FileOutputStream fos = new FileOutputStream(file); + fos.write(text.getBytes()); // 写入内容 + fos.close(); // 关闭流 + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } finally { + try { + fos.close(); + } catch (IOException e) { + e.printStackTrace(); + } + } + + FileReader reader = null; // char + + try { + reader = new FileReader(file.getAbsoluteFile()); + BufferedReader bReader = new BufferedReader(reader); + String line = bReader.readLine(); + Log.i(TAG, "从文件读取的内容: " + line); + bReader.close(); + reader.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + // 显示结果 + showResult(file.getAbsolutePath()); + + // 删除文件 + // file.delete(); + // deleteFile(FILENAME); + } + + // 将文字保存到外部存储 + private void saveTextIntoExternalStorage(String text) { + if (!isExternalStorageWritable()) { + Log.e(TAG, "外部存储不可写!"); + return; + } + + File dir = getPublicExtStorageDir(DIRECTORY, Environment.DIRECTORY_DOWNLOADS); + File file = new File(dir, FILENAME); + + try { + FileOutputStream fos = new FileOutputStream(file); + fos.write(text.getBytes()); + fos.close(); + } catch (FileNotFoundException e) { + e.printStackTrace(); + } catch (IOException e) { + e.printStackTrace(); + } + + // 显示结果 + showResult(file.getAbsolutePath()); + } + + /* Checks if external storage is available for read and write */ + private boolean isExternalStorageWritable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state)) { + return true; + } + return false; + } + + /* Checks if external storage is available to at least read */ + private boolean isExternalStorageReadable() { + String state = Environment.getExternalStorageState(); + if (Environment.MEDIA_MOUNTED.equals(state) || + Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { + return true; + } + return false; + } + + // 创建公开的外部存储目录(App卸载时不会删除) + private File getPublicExtStorageDir(String dirName, String type) { + if (type == null) { // 指定文件类型 + type = Environment.DIRECTORY_PICTURES; + } + File dir = new File(Environment // + .getExternalStoragePublicDirectory(type), dirName); + if (!dir.mkdirs()) { + Log.e(TAG, "目录无法创建!"); + } + + long freeSpace = dir.getFreeSpace(); + Log.i(TAG, "剩余空间大小: " + (freeSpace / 1024 / 1024) + "MB"); + long totalSpace = dir.getTotalSpace(); + Log.i(TAG, "总空间大小: " + (totalSpace / 1024 / 1024) + "MB"); + + return dir; + } + + // 创建私有的外部存储目录(App卸载时会一同删除) + private File getPrivateExtStorageDir(Context context, String dirName, String type) { + if (type == null) { // 指定文件类型 + type = Environment.DIRECTORY_PICTURES; + } + File file = new File(context // + .getExternalFilesDir(type), dirName); + if (!file.mkdirs()) { + Log.e(TAG, "目录无法创建!"); + } + return file; + } + + private void showResult(String result) { + ((TextView) findViewById(R.id.text1)) // + .setText(result.toCharArray(), 0, result.length()); + } + +} diff --git a/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/MainActivity.java b/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/MainActivity.java new file mode 100644 index 000000000..c79e6495a --- /dev/null +++ b/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/MainActivity.java @@ -0,0 +1,37 @@ +package edu.hzuapps.androidlabs2018.soft1613071002206; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.content.Intent; +import android.view.View; +import android.widget.ArrayAdapter; +import android.widget.Button; +import android.widget.ListView; + +public class MainActivity extends AppCompatActivity { + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + Button btn1=(Button)findViewById(R.id.btn1); + btn1.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent=new Intent(MainActivity.this,Main3Activity.class); + startActivity(intent); + } + }); + ((Button)findViewById(R.id.btn3)).setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent=new Intent(MainActivity.this,Main2Activity.class); + startActivity(intent); + } + }); + + + + } + +} diff --git a/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Soft1613071002206.java b/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Soft1613071002206.java new file mode 100644 index 000000000..275117376 --- /dev/null +++ b/soft1613071002206/app/src/main/java/edu/hzuapps/androidlabs2018/soft1613071002206/Soft1613071002206.java @@ -0,0 +1,35 @@ +package edu.hzuapps.androidlabs2018.soft1613071002206; + +import android.support.v7.app.AppCompatActivity; +import android.os.Bundle; +import android.content.Intent; +import android.view.View; +import android.widget.Button; + + + +public class Soft1613071002206 extends AppCompatActivity { + private Button btn; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_soft1613071002206); + btn = (Button) findViewById(R.id.开始); + btn.setOnClickListener((View.OnClickListener) new MyButtonListener()); + +} + + + class MyButtonListener implements View.OnClickListener { + public void onClick(View v){ + Intent intent = new Intent(); + intent.setClass(Soft1613071002206.this,MainActivity.class); + + Soft1613071002206.this.startActivity(intent); + } + } + + + } + diff --git a/soft1613071002206/app/src/main/res/drawable/ic_launcher_background.xml b/soft1613071002206/app/src/main/res/drawable/ic_launcher_background.xml new file mode 100644 index 000000000..d5fccc538 --- /dev/null +++ b/soft1613071002206/app/src/main/res/drawable/ic_launcher_background.xml @@ -0,0 +1,170 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/soft1613071002206/app/src/main/res/drawable/shiyan5.png b/soft1613071002206/app/src/main/res/drawable/shiyan5.png new file mode 100644 index 000000000..82af013c7 Binary files /dev/null and b/soft1613071002206/app/src/main/res/drawable/shiyan5.png differ diff --git a/soft1613071002206/app/src/main/res/drawable/textview_01.jpg b/soft1613071002206/app/src/main/res/drawable/textview_01.jpg new file mode 100644 index 000000000..59d747419 Binary files /dev/null and b/soft1613071002206/app/src/main/res/drawable/textview_01.jpg differ diff --git a/soft1613071002206/app/src/main/res/layout/activity_main.xml b/soft1613071002206/app/src/main/res/layout/activity_main.xml new file mode 100644 index 000000000..8f00c7ee5 --- /dev/null +++ b/soft1613071002206/app/src/main/res/layout/activity_main.xml @@ -0,0 +1,35 @@ + + + + + +