correçao de erros no ecra da conta
parent
66d019d9b6
commit
9875c82e65
|
|
@ -21,7 +21,6 @@ import androidx.activity.result.ActivityResultLauncher;
|
||||||
import androidx.activity.result.contract.ActivityResultContracts;
|
import androidx.activity.result.contract.ActivityResultContracts;
|
||||||
import androidx.annotation.NonNull;
|
import androidx.annotation.NonNull;
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
import androidx.appcompat.app.AppCompatActivity;
|
||||||
import androidx.core.app.ActivityCompat;
|
|
||||||
import androidx.core.content.ContextCompat;
|
import androidx.core.content.ContextCompat;
|
||||||
import androidx.core.graphics.Insets;
|
import androidx.core.graphics.Insets;
|
||||||
import androidx.core.view.ViewCompat;
|
import androidx.core.view.ViewCompat;
|
||||||
|
|
@ -42,8 +41,6 @@ import java.io.InputStream;
|
||||||
|
|
||||||
public class ContaActivity extends AppCompatActivity {
|
public class ContaActivity extends AppCompatActivity {
|
||||||
|
|
||||||
private static final int PERMISSION_REQUEST_CODE = 100;
|
|
||||||
|
|
||||||
private ImageView imageProfile;
|
private ImageView imageProfile;
|
||||||
private TextInputEditText editName;
|
private TextInputEditText editName;
|
||||||
private TextView textEmail;
|
private TextView textEmail;
|
||||||
|
|
@ -57,6 +54,7 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
private ProgressDialog progressDialog;
|
private ProgressDialog progressDialog;
|
||||||
|
|
||||||
private ActivityResultLauncher<Intent> imagePickerLauncher;
|
private ActivityResultLauncher<Intent> imagePickerLauncher;
|
||||||
|
private ActivityResultLauncher<String> permissionLauncher;
|
||||||
private Uri selectedImageUri;
|
private Uri selectedImageUri;
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
|
|
@ -75,6 +73,7 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
initViews();
|
initViews();
|
||||||
initFirebase();
|
initFirebase();
|
||||||
setupImagePicker();
|
setupImagePicker();
|
||||||
|
setupPermissionLauncher();
|
||||||
loadUserData();
|
loadUserData();
|
||||||
setupListeners();
|
setupListeners();
|
||||||
}
|
}
|
||||||
|
|
@ -82,7 +81,9 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
private void initViews() {
|
private void initViews() {
|
||||||
MaterialToolbar toolbar = findViewById(R.id.toolbar);
|
MaterialToolbar toolbar = findViewById(R.id.toolbar);
|
||||||
setSupportActionBar(toolbar);
|
setSupportActionBar(toolbar);
|
||||||
toolbar.setNavigationOnClickListener(v -> finish());
|
if (toolbar != null) {
|
||||||
|
toolbar.setNavigationOnClickListener(v -> finish());
|
||||||
|
}
|
||||||
|
|
||||||
imageProfile = findViewById(R.id.imageProfile);
|
imageProfile = findViewById(R.id.imageProfile);
|
||||||
editName = findViewById(R.id.editName);
|
editName = findViewById(R.id.editName);
|
||||||
|
|
@ -111,9 +112,16 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
if (selectedImageUri != null) {
|
if (selectedImageUri != null) {
|
||||||
try {
|
try {
|
||||||
InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
|
InputStream inputStream = getContentResolver().openInputStream(selectedImageUri);
|
||||||
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
if (inputStream != null) {
|
||||||
imageProfile.setImageBitmap(bitmap);
|
Bitmap bitmap = BitmapFactory.decodeStream(inputStream);
|
||||||
uploadProfileImage();
|
inputStream.close();
|
||||||
|
if (bitmap != null) {
|
||||||
|
imageProfile.setImageBitmap(bitmap);
|
||||||
|
uploadProfileImage();
|
||||||
|
} else {
|
||||||
|
Toast.makeText(this, "Erro ao carregar imagem", Toast.LENGTH_SHORT).show();
|
||||||
|
}
|
||||||
|
}
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
Toast.makeText(this, "Erro ao carregar imagem: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
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() {
|
private void loadUserData() {
|
||||||
FirebaseUser user = mAuth.getCurrentUser();
|
FirebaseUser user = mAuth.getCurrentUser();
|
||||||
if (user != null) {
|
if (user != null) {
|
||||||
// Carregar email
|
// Carregar email
|
||||||
textEmail.setText(user.getEmail());
|
if (textEmail != null) {
|
||||||
|
textEmail.setText(user.getEmail() != null ? user.getEmail() : "");
|
||||||
|
}
|
||||||
|
|
||||||
// Carregar nome
|
// Carregar nome
|
||||||
if (user.getDisplayName() != null && !user.getDisplayName().isEmpty()) {
|
if (editName != null && user.getDisplayName() != null && !user.getDisplayName().isEmpty()) {
|
||||||
editName.setText(user.getDisplayName());
|
editName.setText(user.getDisplayName());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -151,21 +174,30 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
// Usar uma biblioteca de imagens seria ideal aqui (Glide/Picasso)
|
// Usar uma biblioteca de imagens seria ideal aqui (Glide/Picasso)
|
||||||
// Por agora, vamos usar uma abordagem simples com Thread
|
// Por agora, vamos usar uma abordagem simples com Thread
|
||||||
new Thread(() -> {
|
new Thread(() -> {
|
||||||
|
InputStream input = null;
|
||||||
try {
|
try {
|
||||||
java.net.URL url = new java.net.URL(imageUrl);
|
java.net.URL url = new java.net.URL(imageUrl);
|
||||||
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
|
java.net.HttpURLConnection connection = (java.net.HttpURLConnection) url.openConnection();
|
||||||
connection.setDoInput(true);
|
connection.setDoInput(true);
|
||||||
connection.connect();
|
connection.connect();
|
||||||
InputStream input = connection.getInputStream();
|
input = connection.getInputStream();
|
||||||
Bitmap bitmap = BitmapFactory.decodeStream(input);
|
Bitmap bitmap = BitmapFactory.decodeStream(input);
|
||||||
runOnUiThread(() -> {
|
runOnUiThread(() -> {
|
||||||
if (bitmap != null) {
|
if (bitmap != null && imageProfile != null) {
|
||||||
imageProfile.setImageBitmap(bitmap);
|
imageProfile.setImageBitmap(bitmap);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// Se falhar, tentar carregar do Storage
|
// Se falhar, tentar carregar do Storage
|
||||||
runOnUiThread(() -> loadProfileImageFromStorage());
|
runOnUiThread(() -> loadProfileImageFromStorage());
|
||||||
|
} finally {
|
||||||
|
if (input != null) {
|
||||||
|
try {
|
||||||
|
input.close();
|
||||||
|
} catch (Exception e) {
|
||||||
|
// Ignorar erro ao fechar
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}).start();
|
}).start();
|
||||||
}
|
}
|
||||||
|
|
@ -183,51 +215,32 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setupListeners() {
|
private void setupListeners() {
|
||||||
btnChangePhoto.setOnClickListener(v -> openImagePicker());
|
if (btnChangePhoto != null) {
|
||||||
cardImageContainer.setOnClickListener(v -> openImagePicker());
|
btnChangePhoto.setOnClickListener(v -> openImagePicker());
|
||||||
|
}
|
||||||
btnSaveName.setOnClickListener(v -> saveUserName());
|
if (cardImageContainer != null) {
|
||||||
|
cardImageContainer.setOnClickListener(v -> openImagePicker());
|
||||||
|
}
|
||||||
|
if (btnSaveName != null) {
|
||||||
|
btnSaveName.setOnClickListener(v -> saveUserName());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void openImagePicker() {
|
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);
|
Intent intent = new Intent(Intent.ACTION_PICK, MediaStore.Images.Media.EXTERNAL_CONTENT_URI);
|
||||||
imagePickerLauncher.launch(intent);
|
imagePickerLauncher.launch(intent);
|
||||||
} else {
|
} else {
|
||||||
requestPermissions();
|
// 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);
|
||||||
private boolean checkPermissions() {
|
imagePickerLauncher.launch(intent);
|
||||||
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();
|
|
||||||
} else {
|
} 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();
|
progressDialog.show();
|
||||||
|
|
||||||
|
InputStream inputStream = null;
|
||||||
try {
|
try {
|
||||||
// Comprimir imagem
|
// 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);
|
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();
|
ByteArrayOutputStream baos = new ByteArrayOutputStream();
|
||||||
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
|
bitmap.compress(Bitmap.CompressFormat.JPEG, 80, baos);
|
||||||
byte[] imageData = baos.toByteArray();
|
byte[] imageData = baos.toByteArray();
|
||||||
|
|
@ -283,10 +318,20 @@ public class ContaActivity extends AppCompatActivity {
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
progressDialog.dismiss();
|
progressDialog.dismiss();
|
||||||
Toast.makeText(this, "Erro ao processar imagem: " + e.getMessage(), Toast.LENGTH_SHORT).show();
|
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() {
|
private void saveUserName() {
|
||||||
|
if (editName == null) return;
|
||||||
|
|
||||||
String newName = editName.getText().toString().trim();
|
String newName = editName.getText().toString().trim();
|
||||||
|
|
||||||
if (newName.isEmpty()) {
|
if (newName.isEmpty()) {
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue