package com.example.graduationdesign;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AlertDialog;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.content.FileProvider;
import android.Manifest;
import android.app.Activity;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Typeface;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.provider.DocumentsContract;
import android.provider.MediaStore;
import android.text.InputType;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;
import android.widget.Toast;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.nio.file.Files;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Set;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class imageData extends Activity {
//相机事件
private static final int REQUEST_IMAGE_CAPTURE = 1;
//相册事件
private static final int REQUEST_CODE_SELECT_IMAGE = 2;
//同时获取相机相册权限
private static final int MY_REQUEST_CODE = 3000;
//照片存储文件夹
private static String mDir = "";
//照片存储路径
private String mCurrentPhotoPath = "";
//照片名称
private String mCurrentPhotoName = "";
//植株id
private String ID = "";
private File documentsDirectory = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS);
private File imgDirectory = new File(documentsDirectory, "data_img");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_image_data);
//同时请求相机、读写、相册权限
if (ContextCompat.checkSelfPermission(this, Manifest.permission.CAMERA) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED
|| ContextCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions(this, new String[]{Manifest.permission.CAMERA, Manifest.permission.READ_EXTERNAL_STORAGE, Manifest.permission.WRITE_EXTERNAL_STORAGE}, MY_REQUEST_CODE);
}
Intent intent =getIntent();
//获取布局
LinearLayout imageLayout = (LinearLayout)findViewById(R.id.imageLayout);
//定义文件名称
String fname = "example_image.xlsx";
Button fBut = (Button)findViewById(R.id.img_file);
TextView fView = (TextView)findViewById(R.id.img_file_name);
fBut.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
AlertDialog.Builder builder = new AlertDialog.Builder(imageData.this);
builder.setTitle("请输入excel名称");
final EditText input = new EditText(imageData.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String text = input.getText().toString();
fView.setText(text+".xlsx");
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
//监听打开excel
Button excel_open = (Button) findViewById(R.id.img_excel);
excel_open.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
TextView fName = (TextView)findViewById(R.id.img_file_name);
String fileName = fName.getText().toString();
// 获取要打开的Excel文件的路径
File file = new File(Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOCUMENTS)+"/data_excel/" + fileName);
if (file.exists()) {
// 文件已存在,执行您的操作
Uri fileUri = FileProvider.getUriForFile(imageData.this, getPackageName() + ".fileprovider", file);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(fileUri, "application/vnd.ms-excel");
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
startActivity(intent);
} else {
// 文件不存在,弹出对话框
Toast.makeText(imageData.this, "文件不存在,无法查看!", Toast.LENGTH_SHORT).show();
}
}
});
//iconImg_icon的显示
Typeface font = Typeface.createFromAsset(getAssets(), "iconfont.ttf");
TextView iconImg_id = findViewById(R.id.iconImg_id);
iconImg_id.setTypeface(font);
iconImg_id.setText(getResources().getString(R.string.plants_id));
//监听照片存储按钮
Button picDir = (Button)findViewById(R.id.pic_dir);
Drawable icon_dir=getResources().getDrawable(R.drawable.icon_dir);
icon_dir.setBounds(0,0,50,50);//必须设置图片的大小否则没有作用
picDir.setCompoundDrawables(icon_dir,null ,null,null);//设置图片left这里如果是右边就放到第二个参数里面依次对应
picDir.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
AlertDialog.Builder builder = new AlertDialog.Builder(imageData.this);
builder.setTitle("请输入文件夹名称");
final EditText input = new EditText(imageData.this);
input.setInputType(InputType.TYPE_CLASS_TEXT);
builder.setView(input);
builder.setPositiveButton("确认", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
mDir = input.getText().toString();
TextView DirView = (TextView)findViewById(R.id.dir_name);
DirView.setText("/"+mDir);
}
});
builder.setNegativeButton("取消", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.cancel();
}
});
builder.show();
}
});
//监听跳转图像存储文件夹
Button imgDir_open = (Button) findViewById(R.id.img_dir);
Drawable icon_see=getResources().getDrawable(R.drawable.icon_see);
icon_see.setBounds(0,0,50,50);//必须设置图片的大小否则没有作用
imgDir_open.setCompoundDrawables(icon_see,null ,null,null);