package com.example.camrea;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.content.FileProvider;
import android.Manifest;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.MediaStore;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.ImageView;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.text.DateFormat;
import java.text.DateFormatSymbols;
import java.text.SimpleDateFormat;
import java.util.Date;
public class MainActivity extends AppCompatActivity {
private static final String TAG = "MainActivity";
private Button mInternet;
private Button mCamera;
private Button mPhone;
private Button mVedio;
private Button mJietu;
private ImageView mImage;
private Uri uriForFile;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
supportRequestWindowFeature(Window.FEATURE_NO_TITLE);
setContentView(R.layout.activity_main);
if(Build.VERSION.SDK_INT>= Build.VERSION_CODES.M){
requestPermissions(new String[]{
Manifest.permission.CALL_PHONE,
Manifest.permission.CAMERA,
Manifest.permission.INTERNET,
Manifest.permission.WRITE_EXTERNAL_STORAGE,
Manifest.permission.READ_EXTERNAL_STORAGE
},110);
}
initView();
}
private void initView() {
mInternet = findViewById(R.id.internet);
mCamera = findViewById(R.id.camera);
mPhone = findViewById(R.id.phone);
mVedio = findViewById(R.id.vedio);
mJietu = findViewById(R.id.jietu);
mImage = findViewById(R.id.image);
}
public void click(View view) {
switch (view.getId()){
case R.id.camera:
camera();
break;
case R.id.internet:
internet();
break;
case R.id.jietu:
jietu();
break;
case R.id.vedio:
vedio();
break;
case R.id.phone:
phone();
break;
}
}
private void jietu() {
View decorView = getWindow().getDecorView();
decorView.setDrawingCacheEnabled(true);
decorView.buildDrawingCache();
Bitmap drawingCache = decorView.getDrawingCache();
mImage.setImageBitmap(drawingCache);
DateFormat df = new SimpleDateFormat("yyHHmmss");
String format = df.format(new Date());
String filePath= "/mnt/sdcard/DCIM/Camera/"+format+".jpg";
try {
drawingCache.compress(Bitmap.CompressFormat.JPEG,100,new FileOutputStream(filePath));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
}
private void vedio() {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_VIDEO_CAPTURE);
startActivity(intent);
}
private void camera() {
Intent intent = new Intent();
intent.setAction(MediaStore.ACTION_IMAGE_CAPTURE);
DateFormat df = new SimpleDateFormat("yyHHmmss");
String format = df.format(new Date());
String filePath= "/mnt/sdcard/DCIM/Camera/"+format+".jpg";
File file = new File(filePath);
uriForFile = FileProvider.getUriForFile(this, "com.example.camrea.fileProvider", file);
intent.putExtra(MediaStore.EXTRA_OUTPUT,uriForFile);
startActivityForResult(intent,110);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode==110){
mImage.setImageURI(uriForFile);
}
}
private void internet() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_VIEW);
intent.setData(Uri.parse("http://www.baidu.com/"));
startActivity(intent);
}
private void phone() {
Intent intent = new Intent();
intent.setAction(Intent.ACTION_CALL);
intent.setData(Uri.parse("tel:10086"));
startActivity(intent);
}
}