diff --git a/students/soft1714080902417/app/src/main/AndroidManifest.xml b/students/soft1714080902417/app/src/main/AndroidManifest.xml index 69020b534..eeac861e8 100644 --- a/students/soft1714080902417/app/src/main/AndroidManifest.xml +++ b/students/soft1714080902417/app/src/main/AndroidManifest.xml @@ -2,16 +2,25 @@ + - + + + + + + + diff --git a/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/DowloadImage.java b/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/DowloadImage.java deleted file mode 100644 index f99e293d8..000000000 --- a/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/DowloadImage.java +++ /dev/null @@ -1,82 +0,0 @@ -package edu.hzuapps.androidlabs.soft1714080902417; - -import android.app.Activity; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.os.Handler; -import android.os.Message; -import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; -import android.text.TextUtils; -import android.view.View; -import android.widget.EditText; -import android.widget.ImageView; -import android.widget.Toast; - -import java.io.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; - - -public class DowloadImage extends Activity { - protected static final int CHANGE_UI=1; - protected static final int ERROR=2; - private EditText et_path; - private ImageView iv; - private Handler handler = new Handler(){ - public void handleMessage(android.os.Message msg){ - if(msg.what == CHANGE_UI){ - Bitmap bitmap = (Bitmap) msg.obj; - iv.setImageBitmap(bitmap); - }else if (msg.what == ERROR){ - Toast.makeText(DowloadImage.this,"显示图片错误",Toast.LENGTH_SHORT).show(); - } - }; - }; - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_dowload_image); - - et_path=(EditText)findViewById(R.id.et_path); - iv=(ImageView) findViewById(R.id.iv); - } - public void onClick(View view){ - final String path = et_path.getText().toString().trim(); - if(TextUtils.isEmpty(path)){ - Toast.makeText(this,"图片路径不能为空",Toast.LENGTH_SHORT).show(); - }else { - new Thread() { - private HttpURLConnection conn; - private Bitmap bitmap; - public void run() { - try { - URL url = new URL(path); - conn = (HttpURLConnection) url.openConnection(); - conn.setRequestMethod("GET"); - conn.setConnectTimeout(5000); - conn.setRequestProperty("User-Agent", "Mozilla/4.0(compatible;MSIE 6.0;Windows NT 5.1;" + "SV1;.NET4.0C;.NET4.0E;.NET CLR 2.0.50727;" + ".NET CLR 3.0.4506.2152;.NET CLR 3.5.30729;Shuame)"); - int code = conn.getResponseCode(); - if (code == 200) { - InputStream is = conn.getInputStream(); - bitmap = BitmapFactory.decodeStream(is); - Message msg = new Message(); - msg.what = CHANGE_UI; - msg.obj= bitmap; - handler.sendMessage(msg); - }else{ - Message msg = new Message(); - msg.what=ERROR; - handler.sendMessage(msg); - } - } catch (Exception e) { - e.printStackTrace(); - Message msg = new Message(); - msg.what = ERROR; - handler.sendMessage(msg); - } - }; - }.start(); - } - } -} \ No newline at end of file diff --git a/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/Register.java b/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/Register.java deleted file mode 100644 index 679544d57..000000000 --- a/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/Register.java +++ /dev/null @@ -1,109 +0,0 @@ -package edu.hzuapps.androidlabs.soft1714080902417; - -import android.support.v7.app.AppCompatActivity; -import android.os.Bundle; -import android.util.Log; -import android.view.View; -import android.widget.Button; -import android.widget.TextView; -import android.widget.Toast; - -import org.json.JSONArray; -import org.json.JSONObject; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileNotFoundException; -import java.io.FileOutputStream; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; -import java.net.HttpURLConnection; -import java.net.URL; - - -public class Register extends AppCompatActivity { - private TextView textView; - public static final String DIRECTORY = "demo"; - public static final String FILENAME = "file_demo.txt"; - public static final String TAG=Register.class.getSimpleName(); - - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity_register); - textView = (TextView) findViewById(R.id.textView6); - Button button = (Button) findViewById(R.id.button3); - button.setOnClickListener(new MyonclickListener()); - - - } - private class MyonclickListener implements View.OnClickListener { - - @Override - public void onClick(View v) { - String string = textView.getText().toString(); - saveTextIntoInternalStorage(string); - - } - } - 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(); - } - } - - Toast.makeText( Register.this,"数据提交成功!",0).show(); - - - } - - -} diff --git a/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/Soft1714080902417MainActivity.java b/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/Soft1714080902417MainActivity.java index b1444ba82..5558e2a90 100644 --- a/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/Soft1714080902417MainActivity.java +++ b/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/Soft1714080902417MainActivity.java @@ -14,6 +14,18 @@ public class Soft1714080902417MainActivity extends AppCompatActivity { protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); + Button button1=(Button) findViewById(R.id.button1); + button1.setOnClickListener(new View.OnClickListener() + { + @Override + public void onClick(View v) + { + Intent intent=new Intent(Soft1714080902417MainActivity.this,TakePhotos.class); + startActivity(intent); + } + }); + + Button button=(Button) findViewById(R.id.button); button.setOnClickListener(new View.OnClickListener() { @@ -25,6 +37,8 @@ public void onClick(View v) } }); + + } diff --git a/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/TakePhotos.java b/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/TakePhotos.java new file mode 100644 index 000000000..5b01295d8 --- /dev/null +++ b/students/soft1714080902417/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902417/TakePhotos.java @@ -0,0 +1,332 @@ +package edu.hzuapps.androidlabs.soft1714080902417; + +import android.annotation.TargetApi; + +import android.content.ContentUris; + +import android.content.Context; +import android.content.Intent; + +import android.database.Cursor; + +import android.graphics.Bitmap; + +import android.graphics.BitmapFactory; + +import android.net.Uri; + +import android.os.Build; + +import android.os.Environment; + +import android.provider.DocumentsContract; + +import android.provider.MediaStore; + +import android.support.v7.app.AppCompatActivity; + +import android.os.Bundle; + +import android.view.View; + +import android.widget.Button; + +import android.widget.ImageView; + +import android.widget.Toast; + + + +import java.io.File; + +import java.io.FileNotFoundException; + +import java.io.IOException; + + + +public class TakePhotos extends AppCompatActivity { + + public static final int TAKE_PHOTO = 1; + public static final int CROP_PHOTO = 2; + public static final int CHOOSE_PHOTO = 3; + private Button takephoto; + private Button choosephoto; + private ImageView picture; + private Uri imageUri; + + @Override + protected void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_take_photos); + takephoto = (Button) findViewById(R.id.take_photo); + picture = (ImageView) findViewById(R.id.picture); + choosephoto = (Button) findViewById(R.id.choose_photo); + + /* + * 实现监听器 + * */ + + takephoto.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + + /* + * 创建File对象,实现拍照图片的存储 + * */ + + File outputImage = new File(Environment.getExternalStorageDirectory(), + + "output_image.jpg"); + + /* + * 初始化图片 + * */ + try { + + if (outputImage.exists()) { + + outputImage.delete(); + + } + + outputImage.createNewFile(); + + } catch (IOException e) { + + e.printStackTrace(); + + } + + /* + * 将File对象转成Uri对象 + * */ + + imageUri = Uri.fromFile(outputImage); + + /* + * 构建隐式Intent,指定图片输出地址 + * */ + + Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); + + intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); + + /* + * 启动相机,并向下一个活动传递参数 + * */ + + startActivityForResult(intent, TAKE_PHOTO); + + } + + }); + choosephoto.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + Intent intent = new Intent("android.intent.action.GET_CONTENT"); + intent.setType("image/*"); + /* + * 打开相册 + * */ + + startActivityForResult(intent, CHOOSE_PHOTO); + + } + + }); + + } + + @Override + protected void onActivityResult(int requestCode, int resultCode, Intent data) { + + switch (requestCode) { + + /* + * 拍照成功返回结果,跳转到裁剪 + * */ + case TAKE_PHOTO: + + if (resultCode == RESULT_OK) { + + Intent intent = new Intent("com.android.camera.action.CROP"); + intent.setDataAndType(imageUri, "image/*"); + intent.putExtra("scale", true); + intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); + /* + * 启动剪裁程序 + * */ + startActivityForResult(intent, CROP_PHOTO); + } + + break; + + /* + * 裁剪结束,将.jpg解析成Bitmap显示出来 + * */ + case CROP_PHOTO: + if (resultCode == RESULT_OK) { + + try { + + Bitmap bitmap = BitmapFactory.decodeStream( + getContentResolver().openInputStream(imageUri)); + picture.setImageBitmap(bitmap); //显示剪裁后的图片 + } catch (FileNotFoundException e) { + e.printStackTrace(); + } + } + break; + + /* + * 判断版本,选择处理图片方式 + * */ + case CHOOSE_PHOTO: + if (resultCode == RESULT_OK) { + if (Build.VERSION.SDK_INT >= 19) { + + /* + * 4.4及以上版本 + * */ + + handleImageOnKitKat(data); + + } else { + + /* + * 4.4以下版本 + * */ + handleImageBeforeKitKat(data); + } + } + break; + default: + break; + } + } + + /* + * 4.4以上版本需要封装Uri + * */ + + @TargetApi(19) + private void handleImageOnKitKat(Intent data) { + String imagePath = null; + + /* + * 获取路径 + * */ + Uri uri = data.getData(); + + /* + *Document类型Uri对document id处理 + * */ + if (DocumentsContract.isDocumentUri(this, uri)) { + String docId = DocumentsContract.getDocumentId(uri); + + /* + * 与Uri中Authority部分比较,media格式需进一步解析 + * */ + if ("com.android.providers.media.documents".equals( + + uri.getAuthority())) { + + String id = docId.split(":")[1]; + + String selection = MediaStore.Images.Media._ID + "=" + id; + + /* + * 传入getImagePath获得真实路径 + * */ + imagePath = getImagePath( + MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); + } else if ("com.android.providers.downloads.documents".equals( + uri.getAuthority())) { + + /* + * downloads格式获得路径 + * */ + Uri contentUri = ContentUris.withAppendedId( + + Uri.parse("content://downloads/public_downloads"), + + Long.valueOf(docId)); + + imagePath = getImagePath(contentUri, null); + + } + } else if ("content".equalsIgnoreCase(uri.getScheme())) { + + /* + * 普通处理 + * */ + imagePath = getImagePath(uri, null); + } + + /* + * 图片显示 + * */ + displayImage(imagePath); + + } + + /* + * 4.4以下版本返回图片真实Uri,直接处理 + * */ + private void handleImageBeforeKitKat(Intent data) { + + Uri uri = data.getData(); + + String imagePath = getImagePath(uri, null); + + displayImage(imagePath); + + } + + /* + * 获取图片路径 + * */ + private String getImagePath(Uri uri, String selection) { + String path = null; + Cursor cursor = getContentResolver().query(uri, null, selection, null, null); + if (cursor != null) { + + /* + * 定位第一行返回指定列名称 + * */ + + if (cursor.moveToFirst()) { + + path = cursor.getString(cursor.getColumnIndex(MediaStore.Images.Media.DATA)); + } + + /* + * 释放资源 + * */ + cursor.close(); + + } + return path; + + } + + /* + * 打印图片 + * */ + private void displayImage(String imagePath) { + if (imagePath != null) { + + /* + * 将照片解析为Bitmap形式展现 + * */ + Bitmap bitmap = BitmapFactory.decodeFile(imagePath); + picture.setImageBitmap(bitmap); + } else { + Toast.makeText(this, "failed to get image", Toast.LENGTH_SHORT).show(); + + } + + } + +} \ No newline at end of file diff --git a/students/soft1714080902417/app/src/main/res/layout/activity_dowload_image.xml b/students/soft1714080902417/app/src/main/res/layout/activity_dowload_image.xml deleted file mode 100644 index 3278f50d9..000000000 --- a/students/soft1714080902417/app/src/main/res/layout/activity_dowload_image.xml +++ /dev/null @@ -1,27 +0,0 @@ - - - - - -