package com.swy.signdemo;
import android.Manifest;
import android.app.AlertDialog;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.provider.Settings;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Toast;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import com.swy.signdemo.databinding.ActivityMainBinding;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
public class MainActivity extends AppCompatActivity {
private ActivityMainBinding binding;
private AlertDialog dialog;
private static final int REQUEST_EXTERNAL_STORAGE = 1;
private static String[] PERMISSIONS_STORAGE = {
Manifest.permission.READ_EXTERNAL_STORAGE,
Manifest.permission.WRITE_EXTERNAL_STORAGE
};
private boolean havePermission = false;
private PickColorWindow pickColorWindow;
private ColorData currentColor = new ColorData("#000000");
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
binding = ActivityMainBinding.inflate(getLayoutInflater());
Window window = getWindow();
window.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS);
window.setStatusBarColor(Color.TRANSPARENT); // 设置状态栏颜色为透明
window.getDecorView().setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN);
setContentView(binding.getRoot());
binding.pickColor.setOnClickListener(v -> {
showColorPickerDialog();
});
binding.clearButton.setOnClickListener(v -> {
binding.signatureView.clear();
});
binding.saveButton.setOnClickListener(v -> {
if (havePermission) {
saveBitmap(binding.signatureView.getSignatureBitmap());
} else {
checkPermission();
}
});
}
private void saveBitmap(Bitmap bitmap) {
// 获取外部存储目录
String folderName = Environment.DIRECTORY_PICTURES;
File file = new File(Environment.getExternalStoragePublicDirectory(folderName), "signature.png");
try {
if (file.exists()) {
file.delete();
}
// 创建目录(如果不存在)
file.getParentFile().mkdirs();
// 尝试创建文件
if (file.createNewFile()) {
OutputStream os = new FileOutputStream(file);
bitmap.compress(Bitmap.CompressFormat.PNG, 100, os); // 保存为PNG格式
os.close();
// 发送广播通知相册刷新
sendBroadcast(new Intent(Intent.ACTION_MEDIA_SCANNER_SCAN_FILE, Uri.fromFile(file)));
Toast.makeText(MainActivity.this, "保存成功", Toast.LENGTH_SHORT).show();
}
} catch (IOException e) {
e.printStackTrace();
}
}
private void showColorPickerDialog() {
if (pickColorWindow != null) {
pickColorWindow.dismiss();
pickColorWindow = null;
}
pickColorWindow = new PickColorWindow(this, currentColor, (ColorData color) -> {
currentColor = color;
binding.signatureView.setColor(Color.parseColor(currentColor.getColorValue()));
binding.viewColor.setBackgroundColor(Color.parseColor(currentColor.getColorValue()));
});
pickColorWindow.showAsDropDown(binding.getRoot());
}
private void checkPermission() {
//检查权限(NEED_PERMISSION)是否被授权 PackageManager.PERMISSION_GRANTED表示同意授权
if (Build.VERSION.SDK_INT >= 30) {
if (!Environment.isExternalStorageManager()) {
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
dialog = new AlertDialog.Builder(this)
.setTitle("提示")//设置标题
.setMessage("请开启文件访问权限,否则无法正常使用本应用!")
.setNegativeButton("取消", (dialog, i) -> dialog.dismiss())
.setPositiveButton("确定", (dialog, which) -> {
dialog.dismiss();
Intent intent = new Intent(Settings.ACTION_MANAGE_ALL_FILES_ACCESS_PERMISSION);
startActivity(intent);
}).create();
dialog.show();
} else {
havePermission = true;
saveBitmap(binding.signatureView.getSignatureBitmap());
Log.i("swyLog", "Android 11以上,当前已有权限");
}
} else {
if (Build.VERSION.SDK_INT > Build.VERSION_CODES.M) {
if (ActivityCompat.checkSelfPermission(this, Manifest.permission.WRITE_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
//申请权限
if (dialog != null) {
dialog.dismiss();
dialog = null;
}
dialog = new AlertDialog.Builder(this)
.setTitle("提示")//设置标题
.setMessage("请开启文件访问权限,否则无法正常使用本应用!")
.setPositiveButton("确定", (dialog, which) -> {
dialog.dismiss();
ActivityCompat.requestPermissions(MainActivity.this, PERMISSIONS_STORAGE, REQUEST_EXTERNAL_STORAGE);
}).create();
dialog.show();
} else {
havePermission = true;
saveBitmap(binding.signatureView.getSignatureBitmap());
Log.i("swyLog", "Android 6.0以上,11以下,当前已有权限");
}
} else {
havePermission = true;
saveBitmap(binding.signatureView.getSignatureBitmap());
Log.i("swyLog", "Android 6.0以下,已获取权限");
}
}
}
@Override
public void onRequestPermissionsResult(int requestCode, String permissions[], int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
switch (requestCode) {
case REQUEST_EXTERNAL_STORAGE: {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
havePermission = true;
saveBitmap(binding.signatureView.getSignatureBitmap());
Toast.makeText(this, "授权成功!", Toast.LENGTH_SHORT).show();
} else {
havePermission = false;
Toast.makeText(this, "授权被拒绝!", Toast.LENGTH_SHORT).show();
}
return;
}
}
}
}