Skip to content

Commit

Permalink
Merge pull request #1912 from 517865058/master
Browse files Browse the repository at this point in the history
实验8大作业
  • Loading branch information
zengsn authored Jun 11, 2019
2 parents a0c97a8 + dbcbffa commit 2a4b708
Show file tree
Hide file tree
Showing 16 changed files with 1,358 additions and 0 deletions.
24 changes: 24 additions & 0 deletions students/soft1714080902301/json/test.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,30 @@
{
"spname": "商品2",
"spprice": "价格2"
},
{
"spname": "商品3",
"spprice": "价格3"
},
{
"spname": "商品4",
"spprice": "价格4"
},
{
"spname": "商品5",
"spprice": "价格5"
},
{
"spname": "商品6",
"spprice": "价格6"
},
{
"spname": "商品7",
"spprice": "价格7"
},
{
"spname": "商品8",
"spprice": "价格8"
}
]
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.myapplication">

<uses-permission android:name="android.permission.INTERNET" />

android:maxSdkVersion="18" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
<uses-feature
android:name="android.hardware.camera"
android:required="false" />

<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=".Main4Activity" android:theme="@style/MyDialogStyleBottom"/>
<activity android:name=".Main3Activity" />
<activity android:name=".Main2Activity" />
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>

</manifest>
Original file line number Diff line number Diff line change
@@ -0,0 +1,199 @@
package com.example.myapplication;

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.TextView;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;

public class Main2Activity extends AppCompatActivity {

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main2);
Button button=(Button)findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
TextView spsl=findViewById(R.id.spsl);
TextView sl=findViewById(R.id.sl);
TextView spjg=findViewById(R.id.spjg);
TextView jg=findViewById(R.id.jg);
TextView spdd=findViewById(R.id.spdd);
TextView ddh=findViewById(R.id.ddh);
String text=spsl.getText().toString()+sl.getText().toString()+"\n"+spjg.getText().toString()+jg.getText().toString()+"\n"+spdd.getText().toString()+ddh.getText().toString();
// Log.i("1",spsl.getText().toString());
//save(text);
saveTextIntoExternalStorage(text);

}
});
}
private void save(String text){
// 获取内部存储目录
File dir = this.getFilesDir();
//File dir = getCacheDir();
File file = new File(dir,"test.txt");
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("test.txt", 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());
String line;
BufferedReader bReader = new BufferedReader(reader);
while ((line =bReader.readLine())!=null){
Log.i("读取","从文件读取的内容: " + line);
}
bReader.close();
reader.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

// 显示结果
showResult(file.getAbsolutePath());

// 删除文件
file.delete();
deleteFile("test.txt");
}

// 将文字保存到外部存储
private void saveTextIntoExternalStorage(String text) {
if (!isExternalStorageWritable()) {
Log.e("外部存储", "外部存储不可写!");
return;
}

File dir = getPublicExtStorageDir("DIRECTORY", Environment.DIRECTORY_DOWNLOADS);
File file = new File(dir, "test01.txt");

try {
FileOutputStream fos = new FileOutputStream(file);
fos.write(text.getBytes());
fos.close();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}

FileReader reader = null;
try {
reader = new FileReader(file.getAbsoluteFile());
String line;
BufferedReader bReader = new BufferedReader(reader);
while ((line =bReader.readLine())!=null){
Log.i("读取","从文件读取的内容: " + line);
}
bReader.close();
reader.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("无法创建", "目录无法创建!");
}

long freeSpace = dir.getFreeSpace();
Log.i("剩余空间", "剩余空间大小: " + (freeSpace / 1024 / 1024) + "MB");
long totalSpace = dir.getTotalSpace();
Log.i("总空间", "总空间大小: " + (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("无法创建", "目录无法创建!");
}
return file;
}


private void showResult(String result) {
((TextView) findViewById(R.id.text_path)) //
.setText(result.toCharArray(), 0, result.length());
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,123 @@
package com.example.myapplication;

import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.net.Uri;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;

import org.json.JSONObject;

import java.io.BufferedReader;
import java.io.File;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;

public class Main3Activity extends AppCompatActivity {
String mCurrentPhotoPath;

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main3);
getJson();
ImageButton imageButton=(ImageButton)findViewById(R.id.imageButton);
imageButton.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//dispatchTakePictureIntent();

Intent intent=new Intent(Main3Activity.this,Main4Activity.class);
startActivityForResult(intent,123);

}
});
}
public void getJson(){
Thread thread=new Thread(new Runnable() {
@Override
public void run() {
try {
String url_s="https://raw.githubusercontent.com/517865058/android-labs-2019/master/students/soft1714080902301/json/test.json";

URL url=new URL(url_s);
HttpURLConnection conn = (HttpURLConnection)url.openConnection();
conn.setConnectTimeout(5000);//设置超时
conn.setUseCaches(false);//数据不多不用缓存了
conn.connect();
InputStream inputStream=conn.getInputStream();
InputStreamReader inputStreamReader=new InputStreamReader(inputStream);
BufferedReader bufferedReader=new BufferedReader(inputStreamReader);

if (conn.getResponseCode()==200){
String inputLine;
StringBuffer resultData=new StringBuffer();
while ((inputLine=bufferedReader.readLine())!=null){
resultData.append(inputLine);
}
String text=resultData.toString();
Log.v("out-----",text);
JSONObject jsonObject=new JSONObject(text);
TextView textView=(TextView)findViewById(R.id.name);
TextView textView1=(TextView)findViewById(R.id.username);
textView.setText(jsonObject.getString("name"));
textView1.setText(jsonObject.getString("username"));
//JSONArray jsonArray=jsonObject.getJSONArray("");

}
}catch (Exception e){
e.printStackTrace();
}

}
});
thread.start();
}
@Override
protected void onActivityResult(int requestCode,int resultCode,Intent data){
if(resultCode==RESULT_OK){
String result = data.getExtras().getString("data");
mCurrentPhotoPath=result;
Log.e("data",mCurrentPhotoPath);
if(mCurrentPhotoPath!=null){
setPic();
//Bitmap bitmap = BitmapFactory.decodeFile(mCurrentPhotoPath);
//ImageButton imageButton=findViewById(R.id.imageButton);
//imageButton.setImageBitmap(bitmap);
}}


}
private void setPic() {
ImageButton imageButton=findViewById(R.id.imageButton);
int targetW = imageButton.getWidth();
int targetH = imageButton.getHeight();
BitmapFactory.Options bmOptions = new BitmapFactory.Options();
bmOptions.inJustDecodeBounds = true;
BitmapFactory.decodeFile(mCurrentPhotoPath, bmOptions);
int photoW = bmOptions.outWidth;
int photoH = bmOptions.outHeight;
// Determine how much to scale down the image
int scaleFactor = Math.min(photoW/targetW, photoH/targetH);
// Decode the image file into a Bitmap sized to fill the View
bmOptions.inJustDecodeBounds = false;
bmOptions.inSampleSize = scaleFactor;
File file=new File(mCurrentPhotoPath);
Uri uri=Uri.fromFile(file);
Bitmap bitmap = BitmapFactory.decodeFile(uri.getPath(), bmOptions);
imageButton.setImageBitmap(bitmap);
}



}



Loading

0 comments on commit 2a4b708

Please sign in to comment.