diff --git a/students/com1714080901133/app/src/main/AndroidManifest.xml b/students/com1714080901133/app/src/main/AndroidManifest.xml deleted file mode 100644 index 26fb712bc..000000000 --- a/students/com1714080901133/app/src/main/AndroidManifest.xml +++ /dev/null @@ -1,24 +0,0 @@ - - - - - - - - - - - - - - - \ No newline at end of file diff --git a/students/com1714080901133/app/src/main/java/edu/hzuapps/androidlabs/com1714080901133/Com1714080901133Activity.java b/students/com1714080901133/app/src/main/java/edu/hzuapps/androidlabs/com1714080901133/Com1714080901133Activity.java index d41508749..b44baeb24 100644 --- a/students/com1714080901133/app/src/main/java/edu/hzuapps/androidlabs/com1714080901133/Com1714080901133Activity.java +++ b/students/com1714080901133/app/src/main/java/edu/hzuapps/androidlabs/com1714080901133/Com1714080901133Activity.java @@ -1,79 +1,192 @@ package edu.hzuapps.androidlabs.com1714080901133; - +import android.annotation.TargetApi; +import android.content.ContentUris; +import android.content.Intent; +import android.database.Cursor; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.os.Handler; -import android.os.Message; +import android.net.Uri; +import android.os.Build; +import android.os.Environment; +import android.os.StrictMode; +import android.provider.DocumentsContract; +import android.provider.MediaStore; +import android.support.annotation.RequiresApi; 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.InputStream; -import java.net.HttpURLConnection; -import java.net.URL; - -public class Com1714080901133Activity extends AppCompatActivity { - - private String imageUrl = "http://img.mp.itc.cn/upload/20170628/b0dabda375494e078340073efc4156c4_th.jpg"; - private ImageView img; - private Handler handle = new Handler() { - public void handleMessage(Message msg) { - switch (msg.what) { - case 0: - Bitmap bmp=(Bitmap)msg.obj; - img.setImageBitmap(bmp); - break; - } - }; - }; +import java.io.File; +import java.io.FileNotFoundException; +import java.io.IOException; + +public class Com1714080901133Activity 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; + @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR2) @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); + StrictMode.VmPolicy.Builder builder = new StrictMode.VmPolicy.Builder(); + StrictMode.setVmPolicy(builder.build()); + builder.detectFileUriExposure(); setContentView(R.layout.com_1714080901133_activity); + 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 outputImage = new File(Environment.getExternalStorageDirectory(), + "output_image.jpg"); - ImageView jmp = findViewById(R.id.hzu); - { + try { + if (outputImage.exists()) { + outputImage.delete(); + } + outputImage.createNewFile(); + } catch (IOException e) { + e.printStackTrace(); + } + + imageUri = Uri.fromFile(outputImage); + + Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); + intent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri); + + startActivityForResult(intent, TAKE_PHOTO); + } + }); - img = (ImageView) findViewById(R.id.img); - img.setOnClickListener(new View.OnClickListener() { + choosephoto.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { - new Thread(new Runnable() { - - @Override - public void run() { - Bitmap bmp = getURLimage(imageUrl); - Message msg = new Message(); - msg.what = 0; - msg.obj = bmp; - handle.sendMessage(msg); - } - }).start(); + Intent intent = new Intent("android.intent.action.GET_CONTENT"); + intent.setType("image/*"); + + startActivityForResult(intent, CHOOSE_PHOTO); } }); - }} - - private Bitmap getURLimage(String imageUrl) { - Bitmap bmp = null; - try { - URL myurl = new URL(imageUrl); - HttpURLConnection conn = (HttpURLConnection) myurl.openConnection(); - conn.setConnectTimeout(6000); - conn.setDoInput(true); - conn.setUseCaches(false); - conn.connect(); - InputStream is = conn.getInputStream(); - bmp = BitmapFactory.decodeStream(is); - is.close(); - } catch (Exception e) { - e.printStackTrace(); + } + + @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; + + 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) { + + handleImageOnKitKat(data); + } else { + + handleImageBeforeKitKat(data); + } + } + break; + default: + break; } - return bmp; + } + + + @TargetApi(19) + private void handleImageOnKitKat(Intent data) { + String imagePath = null; + + Uri uri = data.getData(); + + if (DocumentsContract.isDocumentUri(this, uri)) { + String docId = DocumentsContract.getDocumentId(uri); + + if ("com.android.providers.media.documents".equals( + uri.getAuthority())) { + String id = docId.split(":")[1]; + String selection = MediaStore.Images.Media._ID + "=" + id; + imagePath = getImagePath( + MediaStore.Images.Media.EXTERNAL_CONTENT_URI, selection); + } else if ("com.android.providers.downloads.documents".equals( + uri.getAuthority())) { + + 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); } -} + 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 = 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/com1714080901133/app/src/main/res/drawable/bilibili.jpg b/students/com1714080901133/app/src/main/res/drawable/bilibili.jpg deleted file mode 100644 index f2d3f7f19..000000000 Binary files a/students/com1714080901133/app/src/main/res/drawable/bilibili.jpg and /dev/null differ diff --git a/students/com1714080901133/app/src/main/res/drawable/hzxy.jpg b/students/com1714080901133/app/src/main/res/drawable/hzxy.jpg deleted file mode 100644 index 926b9970c..000000000 Binary files a/students/com1714080901133/app/src/main/res/drawable/hzxy.jpg and /dev/null differ diff --git a/students/com1714080901133/app/src/main/res/layout/com_1714080901133_activity.xml b/students/com1714080901133/app/src/main/res/layout/com_1714080901133_activity.xml index 13c22abfa..d7936c2c1 100644 --- a/students/com1714080901133/app/src/main/res/layout/com_1714080901133_activity.xml +++ b/students/com1714080901133/app/src/main/res/layout/com_1714080901133_activity.xml @@ -1,43 +1,35 @@ - - - - - - - - - - - - - + tools:context=".Com1714080901133Activity"> + + + +