diff --git a/students/soft1714080902129/app/src/main/AndroidManifest.xml b/students/soft1714080902129/app/src/main/AndroidManifest.xml index 2ca8addcc..abb6915a5 100644 --- a/students/soft1714080902129/app/src/main/AndroidManifest.xml +++ b/students/soft1714080902129/app/src/main/AndroidManifest.xml @@ -20,6 +20,15 @@ + + + @@ -27,4 +36,5 @@ + \ No newline at end of file diff --git a/students/soft1714080902129/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902129/electricitypaymentsystem/Soft1714080902129Activity.java b/students/soft1714080902129/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902129/electricitypaymentsystem/Soft1714080902129Activity.java index 6441d35d7..e0be3327f 100644 --- a/students/soft1714080902129/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902129/electricitypaymentsystem/Soft1714080902129Activity.java +++ b/students/soft1714080902129/app/src/main/java/edu/hzuapps/androidlabs/soft1714080902129/electricitypaymentsystem/Soft1714080902129Activity.java @@ -4,10 +4,14 @@ import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; +import android.net.Uri; import android.os.Build; import android.os.Handler; import android.os.Message; +import android.provider.MediaStore; +import android.support.annotation.Nullable; import android.support.annotation.RequiresApi; +import android.support.v4.content.FileProvider; import android.support.v7.app.AppCompatActivity; import android.os.Bundle; import android.view.View; @@ -15,13 +19,16 @@ import android.widget.CalendarView; import android.widget.LinearLayout; +import java.io.File; +import java.io.IOException; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; public class Soft1714080902129Activity extends AppCompatActivity { - + public static final int TAKE_PHOTO = 1; + private Uri imageUri; private LinearLayout bg; private Button button; private String url = "https://timgsa.baidu.com/timg?image&quality=80&size=b9999_10000&sec=1557248715893&di=e64b82e55e4f27b1712bab3afc0825be&imgtype=0&src=http%3A%2F%2Fwww.51pptmoban.com%2Fd%2Ffile%2F2012%2F11%2F01%2Fa1f289fb0f6233b02486d2fba8c6f1da.jpg"; @@ -69,7 +76,54 @@ public void run() { }).start(); } }); + Button takePhoto = findViewById(R.id.take_photo); + takePhoto.setOnClickListener(new View.OnClickListener() { + @Override + public void onClick(View v) { + //创建File对象,用户存储拍照后的图片 + File output = new File(getExternalCacheDir(),"output_image.jpg"); + try{ + if(output.exists()) { + output.delete(); + } + output.createNewFile(); + }catch(IOException e){ + e.printStackTrace(); + } + if(Build.VERSION.SDK_INT >= 24){ + imageUri = FileProvider.getUriForFile(Soft1714080902129Activity.this, + "edu.hzuapps.androidlabs.soft1714080902129.electricitypaymentsystem.fileProvider666",output); + } + else{ + imageUri = Uri.fromFile(output); + } + //启动相机程序 + Intent intent = new Intent("android.media.action.IMAGE_CAPTURE"); + intent.putExtra(MediaStore.EXTRA_OUTPUT,imageUri); + startActivityForResult(intent,TAKE_PHOTO); + } + }); } + + @Override + protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { + switch(requestCode){ + case TAKE_PHOTO: + if(resultCode == RESULT_OK){ + try{ + Bitmap bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri)); + bg.setBackground(new BitmapDrawable(bitmap)); + + }catch (IOException e){ + e.printStackTrace(); + } + } + break; + default: + break; + } + } + //从网络下载图片 private Bitmap getImage(String url_image) { Bitmap bmp = null; diff --git a/students/soft1714080902129/app/src/main/res/layout/activity_soft1714080902129.xml b/students/soft1714080902129/app/src/main/res/layout/activity_soft1714080902129.xml index ebca44bbe..20dc8c624 100644 --- a/students/soft1714080902129/app/src/main/res/layout/activity_soft1714080902129.xml +++ b/students/soft1714080902129/app/src/main/res/layout/activity_soft1714080902129.xml @@ -26,4 +26,9 @@ android:layout_width="wrap_content" android:layout_height="wrap_content" android:text="更换背景" /> +