From 7adf91ec386634540ccea1141e342d4837b60022 Mon Sep 17 00:00:00 2001 From: kunknu <11155047@qq.com> Date: Sun, 12 May 2019 21:21:48 +0800 Subject: [PATCH] =?UTF-8?q?#7=E6=8F=90=E4=BA=A4=E5=AE=9E=E9=AA=8C7?= =?UTF-8?q?=E4=BB=A3=E7=A0=81?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .../shiyan7/AndroidManifest.xml | 34 ++++++ .../shiyan7/MainActivity.java | 101 ++++++++++++++++++ .../shiyan7/activity_main.xml | 21 ++++ 3 files changed, 156 insertions(+) create mode 100644 students/soft1714080902324/shiyan7/AndroidManifest.xml create mode 100644 students/soft1714080902324/shiyan7/MainActivity.java create mode 100644 students/soft1714080902324/shiyan7/activity_main.xml diff --git a/students/soft1714080902324/shiyan7/AndroidManifest.xml b/students/soft1714080902324/shiyan7/AndroidManifest.xml new file mode 100644 index 000000000..27081d326 --- /dev/null +++ b/students/soft1714080902324/shiyan7/AndroidManifest.xml @@ -0,0 +1,34 @@ + + + + + + + + + + + + + + + + + + + + \ No newline at end of file diff --git a/students/soft1714080902324/shiyan7/MainActivity.java b/students/soft1714080902324/shiyan7/MainActivity.java new file mode 100644 index 000000000..dd587e7bf --- /dev/null +++ b/students/soft1714080902324/shiyan7/MainActivity.java @@ -0,0 +1,101 @@ +package edu.hzu.myapplication; + +import android.Manifest; +import android.content.Intent; +import android.content.pm.PackageManager; +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.net.Uri; +import android.os.Build; +import android.provider.MediaStore; +import android.support.annotation.NonNull; +import android.support.v4.app.ActivityCompat; +import android.support.v4.content.FileProvider; +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.ImageView; + +import java.io.File; +import java.io.FileNotFoundException; +import java.io.FileOutputStream; +import java.io.IOException; + +public class MainActivity extends AppCompatActivity { + + public static final int T_P=1; + private ImageView picture; + private Uri imageUri; + private static String[] PERMISSIONS_STORAGE = {Manifest.permission.WRITE_EXTERNAL_STORAGE}; + + @Override + protected void onCreate(Bundle savedInstanceState) { + permission(); + super.onCreate(savedInstanceState); + setContentView(R.layout.activity_main); + + Button takephoto=(Button) findViewById(R.id.take_p); + picture=(ImageView)findViewById(R.id.picture); + + takephoto.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + File outputImage=new File(getExternalCacheDir(),"output_image.jpg"); + try{ + if(outputImage.exists()){ + outputImage.delete(); + } + outputImage.createNewFile(); + }catch (IOException e){ + e.printStackTrace(); + } + if(Build.VERSION.SDK_INT>=24){ + imageUri= FileProvider.getUriForFile(MainActivity.this,"com.example.cameraalbumtest.fileprovider",outputImage); + } + else{ + imageUri=Uri.fromFile(outputImage); + } + //启动照相机 + Intent intent=new Intent("android.media.action.IMAGE_CAPTURE"); + intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); + startActivityForResult(intent,T_P); + } + + }); + } + private void permission(){ + if (Build.VERSION.SDK_INT > Build.VERSION_CODES.LOLLIPOP) { + if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) { + ActivityCompat.requestPermissions(this, PERMISSIONS_STORAGE, 1); + } + } + } + @Override + public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) { + super.onRequestPermissionsResult(requestCode, permissions, grantResults); + if (requestCode == 1) { + for (int i = 0; i < permissions.length; i++) { + Log.d("权限动态申请", "申请的权限为:" + permissions[i] + ",申请结果:" + grantResults[i]); + } + } + } + @Override + protected void onActivityResult(int requestCode,int resultCode,Intent data){ + switch (requestCode){ + case T_P: + if(resultCode==RESULT_OK){ + try{ + Bitmap bitmap= BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); + picture.setImageBitmap(bitmap); + }catch (FileNotFoundException e){ + e.printStackTrace(); + } + } + break; + default:break; + } + } + +} diff --git a/students/soft1714080902324/shiyan7/activity_main.xml b/students/soft1714080902324/shiyan7/activity_main.xml new file mode 100644 index 000000000..15eced538 --- /dev/null +++ b/students/soft1714080902324/shiyan7/activity_main.xml @@ -0,0 +1,21 @@ + + + +