在Android Studio中构建应用程序时,我有2条消息。 这是用于添加个人资料图片的片段个人资料。
2条消息:
使用或覆盖已弃用的API。 使用
-Xlint:deprecation
重新编译以获取详细信息。
AND
使用未经检查或不安全的操作。 使用
-Xlint:unchecked
重新编译以获取详细信息。
我可以打开应用程序,但不知道是否需要更改详细信息
public class FragmentProfile extends Fragment {
CircleImageView image_profile;
TextView username;
DatabaseReference reference;
FirebaseUser fuser;
StorageReference storageReference;
private static final int IMAGE_REQUEST = 1;
private Uri imageUri;
private StorageTask uploadTask;
@Nullable
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState){
View view = inflater.inflate(R.layout.profile_fragment, container, false);
image_profile = view.findViewById(R.id.profile_image);
username = view.findViewById(R.id.username);
storageReference = FirebaseStorage.getInstance().getReference("uploads");
fuser = FirebaseAuth.getInstance().getCurrentUser();
reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());
reference.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(@NonNull DataSnapshot dataSnapshot) {
User user = dataSnapshot.getValue(User.class);
username.setText(user.getUsername());
if (user.getImageURL().equals("default")){
image_profile.setImageResource(R.drawable.default_user);
} else {
Glide.with(getContext()).load(user.getImageURL()).into(image_profile);
}
}
@Override
public void onCancelled(@NonNull DatabaseError databaseError) {
}
});
image_profile.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
openImage();
}
});
return view;
}
private void openImage() {
Intent intent = new Intent();
intent.setType("image/*");
intent.setAction(Intent.ACTION_GET_CONTENT);
startActivityForResult(intent , IMAGE_REQUEST);
}
private String getFileExtension(Uri uri){
ContentResolver contentResolver = getContext().getContentResolver();
MimeTypeMap mimeTypeMap = MimeTypeMap.getSingleton();
return mimeTypeMap.getExtensionFromMimeType(contentResolver.getType(uri));
}
private void uploadImage(){
final ProgressDialog pd = new ProgressDialog(getContext());
pd.setMessage("Envoie");
pd.show();
if (imageUri != null){
final StorageReference fileReference = storageReference.child(System.currentTimeMillis()
+"."+getFileExtension(imageUri));
uploadTask = fileReference.putFile(imageUri);
uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) throws Exception {
if (!task.isSuccessful()){
throw task.getException();
}
return fileReference.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()){
Uri downloadUri = task.getResult();
String mUri = downloadUri.toString();
reference = FirebaseDatabase.getInstance().getReference("Users").child(fuser.getUid());
HashMap<String, Object> map = new HashMap<>();
map.put("imageURL", mUri);
reference.updateChildren(map);
pd.dismiss();
} else {
Toast.makeText(getContext(), "Echec", Toast.LENGTH_SHORT).show();
pd.dismiss();
}
}
}).addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception e) {
Toast.makeText(getContext(), e.getMessage(),Toast.LENGTH_SHORT).show();
pd.dismiss();
}
});
} else {
Toast.makeText(getContext(), "Aucune image sélectionnée", Toast.LENGTH_SHORT).show();
}
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == IMAGE_REQUEST && resultCode == RESULT_OK
&& data != null && data.getData() != null){
imageUri = data.getData();
if(uploadTask != null && uploadTask.isInProgress()){
Toast.makeText(getContext(), "Téléchargement en cours", Toast.LENGTH_SHORT).show();
} else {
uploadImage();
}
}
}
}