private void openCamera(){
File outputImage = new File(getExternalCacheDir(),getnormalmac(dev_mac)+".jpg");
try {
if(outputImage.exists()){
outputImage.delete();
}
outputImage.createNewFile();
} catch (IOException e) {
e.printStackTrace();
}
if (Build.VERSION.SDK_INT>=24) {
imageUri = FileProvider.getUriForFile(this,
"com.example.newuieasyc.fileprovider",
outputImage);
}else{
imageUri =Uri.fromFile(outputImage);
}
Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE);
takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, imageUri);
mCameraForResult.launch(takePictureIntent);
}
private void openGallery(){
Intent mediaScanIntent = new Intent(Intent.ACTION_OPEN_document);
mediaScanIntent.addCategory(Intent.CATEGORY_OPENABLE);
mediaScanIntent.setType("image/*");
mGalleryForResult.launch(mediaScanIntent);
}
public String saveMyBitmap(Bitmap mBitmap, String bitName) {
File f = new File(Environment.getExternalStorageDirectory().getPath()+"/Android/data/com.example.newuieasyc/" + bitName + ".jpg");
try {
FileOutputStream fOut = new FileOutputStream(f);
mBitmap.compress(Bitmap.CompressFormat.JPEG, 10, fOut);
try {
fOut.flush();
fOut.close();
} catch (IOException e) {
e.printStackTrace();
}
} catch (FileNotFoundException e) {
e.printStackTrace();
}
return f.toString();
}
private final ActivityResultLauncher mCameraForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
binding.icon.setImageBitmap(bitmap);
imagePath=saveMyBitmap(bitmap,dev_mac);
saveString(dev_mac+"pic", imagePath);
} else {
}
});
private final ActivityResultLauncher mGalleryForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(),
result -> {
if (result.getResultCode() == Activity.RESULT_OK) {
imageUri=result.getData().getData();
if(imageUri!=null) {
Bitmap bit = null;
try {
bit = BitmapFactory.decodeStream(getContentResolver().openInputStream(imageUri));
} catch (FileNotFoundException e) {
e.printStackTrace();
}
binding.icon.setImageBitmap(bit);
imagePath=saveMyBitmap(bit,dev_mac);
saveString(dev_mac+"pic", imagePath);
}
}
});