correçao de erros no ecra da conta

main
230421 2025-12-06 20:50:17 +00:00
parent 66d019d9b6
commit 9875c82e65
1 changed files with 95 additions and 50 deletions

View File

@ -21,7 +21,6 @@ import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.Insets;
import androidx.core.view.ViewCompat;
@ -42,8 +41,6 @@ import java.io.InputStream;
public class ContaActivity extends AppCompatActivity {
private static final int PERMISSION_REQUEST_CODE = 100;
private ImageView imageProfile;
private TextInputEditText editName;
private TextView textEmail;
@ -57,6 +54,7 @@ public class ContaActivity extends AppCompatActivity {
private ProgressDialog progressDialog;
private ActivityResultLauncher<Intent> imagePickerLauncher;
private ActivityResultLauncher<String> permissionLauncher;
private Uri selectedImageUri;
@Override
@ -75,6 +73,7 @@ public class ContaActivity extends AppCompatActivity {
initViews();
initFirebase();
setupImagePicker();
setupPermissionLauncher();
loadUserData();
setupListeners();
}
@ -82,7 +81,9 @@ public class ContaActivity extends AppCompatActivity {
private void initViews() {
MaterialToolbar toolbar = findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
toolbar.setNavigationOnClickListener(v -> finish());
if (toolbar != null) {
toolbar.setNavigationOnClickListener(v -> finish());
}
imageProfile = findViewById(R.id.imageProfile);
editName = findViewById(R.id.editName);
@ -111,9 +112,16 @@ public class ContaActivity extends AppCompatActivity {
if (selectedImageUri != null) {
try {
InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
imageProfile.setImageBitmap(bitmap);
uploadProfileImage();
if (inputStream != null) {
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
inputStream.close();
if (bitmap != null) {
imageProfile.setImageBitmap(bitmap);
uploadProfileImage();
} else {
Toast.makeText(this, "Erro ao carregar imagem", Toast.LENGTH_SHORT).show();
}
}
} catch (Exception e) {
Toast.makeText(this, "Erro ao carregar imagem: " + e.getMessage(), Toast.LENGTH_SHORT).show();
}
@ -123,14 +131,29 @@ public class ContaActivity extends AppCompatActivity {
);
}
private void setupPermissionLauncher() {
permissionLauncher = registerForActivityResult(
new ActivityResultContracts.RequestPermission(),
isGranted -> {
if (isGranted) {
openImagePicker();
} else {
Toast.makeText(this, "Permissão necessária para selecionar imagem", Toast.LENGTH_SHORT).show();
}
}
);
}
private void loadUserData() {
FirebaseUser user = mAuth.getCurrentUser();
if (user != null) {
// Carregar email
textEmail.setText(user.getEmail());
if (textEmail != null) {
textEmail.setText(user.getEmail() != null ? user.getEmail() : "");
}
// Carregar nome
if (user.getDisplayName() != null && !user.getDisplayName().isEmpty()) {
if (editName != null && user.getDisplayName() != null && !user.getDisplayName().isEmpty()) {
editName.setText(user.getDisplayName());
}
@ -151,21 +174,30 @@ public class ContaActivity extends AppCompatActivity {
// Usar uma biblioteca de imagens seria ideal aqui (Glide/Picasso)
// Por agora, vamos usar uma abordagem simples com Thread
new Thread(() -> {
InputStream input = null;
try {
java.net.URL url = new java.net.URL(imageUrl);
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
connection.setDoInput(true);
connection.connect();
InputStream input = connection.getInputStream();
input = connection.getInputStream();
Bitmap bitmap = BitmapFactory.decodeStream(input);
runOnUiThread(() -> {
if (bitmap != null) {
if (bitmap != null && imageProfile != null) {
imageProfile.setImageBitmap(bitmap);
}
});
} catch (Exception e) {
// Se falhar, tentar carregar do Storage
runOnUiThread(() -> loadProfileImageFromStorage());
} finally {
if (input != null) {
try {
input.close();
} catch (Exception e) {
// Ignorar erro ao fechar
}
}
}
}).start();
}
@ -183,51 +215,32 @@ public class ContaActivity extends AppCompatActivity {
}
private void setupListeners() {
btnChangePhoto.setOnClickListener(v -> openImagePicker());
cardImageContainer.setOnClickListener(v -> openImagePicker());
btnSaveName.setOnClickListener(v -> saveUserName());
if (btnChangePhoto != null) {
btnChangePhoto.setOnClickListener(v -> openImagePicker());
}
if (cardImageContainer != null) {
cardImageContainer.setOnClickListener(v -> openImagePicker());
}
if (btnSaveName != null) {
btnSaveName.setOnClickListener(v -> saveUserName());
}
}
private void openImagePicker() {
if (checkPermissions()) {
// No Android 13+ (API 33+), ACTION_PICK não requer permissão
// Mas vamos verificar e pedir permissão se necessário para versões anteriores
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
// Android 13+ - não precisa de permissão para ACTION_PICK
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imagePickerLauncher.launch(intent);
} else {
requestPermissions();
}
}
private boolean checkPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
return ContextCompat.checkSelfPermission(this, Manifest.permission.READ_MEDIA_IMAGES)
== PackageManager.PERMISSION_GRANTED;
} else {
return ContextCompat.checkSelfPermission(this, Manifest.permission.READ_EXTERNAL_STORAGE)
== PackageManager.PERMISSION_GRANTED;
}
}
private void requestPermissions() {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.TIRAMISU) {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_MEDIA_IMAGES},
PERMISSION_REQUEST_CODE);
} else {
ActivityCompat.requestPermissions(this,
new String[]{Manifest.permission.READ_EXTERNAL_STORAGE},
PERMISSION_REQUEST_CODE);
}
}
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
if (requestCode == PERMISSION_REQUEST_CODE) {
if (grantResults.length > 0 && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
openImagePicker();
// Android 12 e anteriores - precisa de permissão
String permission = Manifest.permission.READ_EXTERNAL_STORAGE;
if (ContextCompat.checkSelfPermission(this, permission) == PackageManager.PERMISSION_GRANTED) {
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
imagePickerLauncher.launch(intent);
} else {
Toast.makeText(this, "Permissão necessária para selecionar imagem", Toast.LENGTH_SHORT).show();
permissionLauncher.launch(permission);
}
}
}
@ -240,10 +253,32 @@ public class ContaActivity extends AppCompatActivity {
progressDialog.show();
InputStream inputStream = null;
try {
// Comprimir imagem
InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
inputStream = getContentResolver().openInputStream(selectedImageUri);
if (inputStream == null) {
progressDialog.dismiss();
Toast.makeText(this, "Erro ao abrir imagem", Toast.LENGTH_SHORT).show();
return;
}
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
if (bitmap == null) {
progressDialog.dismiss();
Toast.makeText(this, "Erro ao processar imagem", Toast.LENGTH_SHORT).show();
return;
}
// Redimensionar se muito grande (max 1024x1024)
int maxSize = 1024;
if (bitmap.getWidth() > maxSize || bitmap.getHeight() > maxSize) {
float scale = Math.min((float) maxSize / bitmap.getWidth(), (float) maxSize / bitmap.getHeight());
int newWidth = Math.round(bitmap.getWidth() * scale);
int newHeight = Math.round(bitmap.getHeight() * scale);
bitmap = Bitmap.createScaledBitmap(bitmap, newWidth, newHeight, true);
}
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
byte[] imageData = baos.toByteArray();
@ -283,10 +318,20 @@ public class ContaActivity extends AppCompatActivity {
} catch (Exception e) {
progressDialog.dismiss();
Toast.makeText(this, "Erro ao processar imagem: " + e.getMessage(), Toast.LENGTH_SHORT).show();
} finally {
if (inputStream != null) {
try {
inputStream.close();
} catch (Exception e) {
// Ignorar erro ao fechar
}
}
}
}
private void saveUserName() {
if (editName == null) return;
String newName = editName.getText().toString().trim();
if (newName.isEmpty()) {