Reputation: 5
I am very new to this, and I am trying to put the pieces together so I will get the result I want. I don't really understand what I am doing wrong. The problem is when I am trying to put a profile photo, by the time I am pressing the selected photo to show on image view, the app crashes and I got a message like "URI cannot be null".
public class Profile extends AppCompatActivity {
private ImageView ProfileImg;
private String Tag;
private Uri imagePath = null;
private Uri imageUriPath;
ProgressBar progressBar;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_profile);
progressBar = findViewById(R.id.progressBar);
Button btnUpload = findViewById(R.id.btnUploadPhoto);
Button btnLogOut = findViewById(R.id.btnLogOut);
ProfileImg = findViewById(R.id.profile_img);
/* START ACTIVITY FOR RESULT*/
btnLogOut.setOnClickListener(view -> {
FirebaseAuth.getInstance().signOut();
startActivity(new Intent(Profile.this,MainActivity.class).setFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK|Intent.FLAG_ACTIVITY_CLEAR_TOP));
finish();
});
ProfileImg.setOnClickListener(view -> {
Intent photoIntent = new Intent(Intent.ACTION_PICK);
photoIntent.setType("image/*");
NewStartActivityForResult.launch(photoIntent);
});
btnUpload.setOnClickListener(view -> {
uploadImage();
});
/*Select photo from device*/
}
private ActivityResultLauncher<Intent> NewStartActivityForResult = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {
if (result.getResultCode() == Activity.RESULT_OK){
Intent GotResults = result.getData();
if(GotResults == null)
{
Log.d(Tag,"InitializedActivityLaunchers: intent data is null");
return;
}
Uri imageUriPath = GotResults.getData();
GetImageInView();
}
});
private void uploadImage() {
FirebaseStorage.getInstance().getReference("images/" + UUID.randomUUID().toString()).putFile(imagePath).addOnCompleteListener(new OnCompleteListener<UploadTask.TaskSnapshot>() {
@Override
public void onComplete(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (task.isSuccessful())
{
Toast.makeText(Profile.this, "Image Uploaded!",Toast.LENGTH_SHORT).show();
}else {
Toast.makeText(Profile.this, "Something went Wrong!", Toast.LENGTH_SHORT).show();
}
}
});
}
private void GetImageInView(){
Bitmap bitmap = null;
try{
bitmap = MediaStore.Images.Media.getBitmap(getContentResolver(),imageUriPath);
}catch (IOException e){
e.printStackTrace();
}
}
I first had a problem with startOnActivityResult in which I had a line through the command saying that is deprecated and MediaStore.Images.Media.getBitmap in which I had the same "error", deprecated on getBitmap, and I tried to use the new methods watching tutorials and other forums, but I think I cannot connect the activityResultLauncher with Bitmap because the output of activityResultLauncher is Intent and Bitmap needs URI.
Upvotes: 0
Views: 137
Reputation: 1701
In NewStartActivityForResult
, just manage code as mentioned below
From
Uri imageUriPath = GotResults.getData();
To
imageUriPath = GotResults.getData();
You are declaring and initalising variable instead of only initalising
Upvotes: 0