How do I Parse uri with de.hdodenhof.circleimageview.CircleImageView?
My code
protected void onActivityResult(int requestCode, int resultCode, Intent data){
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
final Uri uri = data.getData();
StorageReference path = mStoragereference.child("Photos").child(uri.getLastPathSegment());
path.putFile(uri).addOnSuccessListener(new OnSuccessListener < UploadTask.TaskSnapshot > () {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
FirebaseUser user = FirebaseAuth.getInstance().getCurrentUser();
UserProfileChangeRequest profileUpdate = new UserProfileChangeRequest.Builder()
.setPhotoUri(Uri.parse(userPhoto))
.build();
if (userPhoto == null) {
Toast.makeText(EditInfo.this, "Error updating image",
Toast.LENGTH_SHORT).show();
}
Thanks for your response!
Since CircleImageView is a subclass of ImageView it inherits most of its properties/methods from ImageView.
Unfortunately it seems that ImageView doesn't expose its source URI, so you'll have to keep the URI that you pass into the CircleImageView around.
Related
I am following an android tutorial and I have been stuck on the deprecated method. I am unable to get the download url to enable me show the image in an ImageView
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICTURE_RESULT && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
StorageReference ref = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
ref.putFile(imageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
String url = taskSnapshot.getDownloadUrl().toString();
String pictureName = taskSnapshot.getStorage().getPath();
deal.setImageUrl(url);
How can I modify the line "String url = taskSnapshot.getDownloadUrl().toString();" so that I can be able to get the download link?
getDownloadUrl() is deprecated so you need to use Task and check isComplete() like below to see if it is completed
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICTURE_RESULT && resultCode == RESULT_OK) {
Uri imageUri = data.getData();
StorageReference ref = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
ref.putFile(imageUri).addOnSuccessListener(this, new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Task<Uri> uri = taskSnapshot.getStorage().getDownloadUrl();
while(!uri.isComplete());
Uri url = uri.getResult();
Log.i(TAG, url.toString());
deal.setImageUrl(url.toString());
Try this
taskSnapshot.getMetadata().getReference().getDownloadUrl().toString()
Try this
StorageReference imageRef = FirebaseUtil.mStorageRef.child(imageUri.getLastPathSegment());
UploadTask uploadTask = imageRef.putFile(imageUri);
uploadTask.continueWithTask(task -> {
if (!task.isSuccessful()) {
if (task.getException() != null)
throw task.getException();
}
return imageRef.getDownloadUrl();
}).addOnCompleteListener(task -> {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult()
} else {
if (task.getException() != null)
task.getException().printStackTrace();
}
});
Can you please help me, I am trying to upload an image in FirebaseStorage.
But it fails.
My Button click Method
public void uploadImage(View view){
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent,GALLERY_INTENT);
}
And here is onActivityResult()
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
progressDialog.setMessage("Uploading");
progressDialog.show();
if(requestCode ==GALLERY_INTENT){
final Uri uri = data.getData();
StorageReference filepath = storageReference.child("Photos").child(uri.getLastPathSegment());
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(),"Uploaded Successfully",Toast.LENGTH_LONG).show();
// profileImage.setImageURI(uri);
progressDialog.dismiss();
}
});
}
}
What's wrong with this code?
Try this
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
Log.e("resultCode: ", resultCode + " RequestCode: " + requestCode);
if (resultCode == getActivity().RESULT_OK) {
sendImageToFireBase(data.getData());
}
}
private void saveImageToFireBase(Uri pathUri) {
StorageReference storageReference = FirebaseStorage.getInstance().getReference().child("Photos");
StorageReference photoRef = storageReference.child(pathUri.getLastPathSegment());
// Upload file to Firebase Storage
photoRef.putFile(pathUri).addOnSuccessListener(getActivity(), new OnSuccessListener<UploadTask.TaskSnapshot>() {
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// When the image has successfully uploaded, we get its download URL
Uri downloadUrl = taskSnapshot.getDownloadUrl();
// Set the download URL to the message box, so that the user can send it to the database
}
});
}
This is the my code ( check this file full code on GitHub) for Uploading an image to Firebase Storage.
// ImagePickerButton shows an image picker to upload a image for a message
mPhotoPickerButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent i = new Intent();
i.setType("image/*");
i.setAction(Intent.ACTION_GET_CONTENT);
i.putExtra(Intent.EXTRA_LOCAL_ONLY, true);
startActivityForResult(Intent.createChooser(i, "Complete action using"), RC_PHOTO_PICKER);
}
});
This is the OnActivityResult Method:
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
Uri selectedImageUri = data.getData();
//get the reference to stored file at database
StorageReference selectedImageFilePath = mStorageReference.child(selectedImageUri.getLastPathSegment());
//upload file to firebase
selectedImageFilePath.putFile(selectedImageUri).addOnSuccessListener(MainActivity.this, new OnSuccessListener < UploadTask.TaskSnapshot > () {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
#SuppressWarnings("VisibleForTests")
String downloadUrl = taskSnapshot.getDownloadUrl().toString();
Message message = new Message(null, userName, downloadUrl);
messagesDatabaseReference.push().setValue(message);
}
});
}
}
}
Picking an Image from the Gallery works fine but I'm not able to upload the selected Image on Firebase Storage. Correct my code If I'm wrong at any site. However, I manually entered these lines in my manifest file
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
I'm currently running the app in my own mobile device connected via USB( Moto G4 - Nougat)
data.getData() could return an URI that does not point to a File.
You should use
InputStream is = getContentResolver().openInputStream(data.getData())
and than use
selectedImageFilePath.putStream(is)
Also you could add an onFailureListener to the upload task and debug what is going wrong.
In addition, if I recall correctly, I do not think you need the storage permissions when you directly read the data in onActivityResult.
Try this for button click :
//Select Image functionality
btnselectImage.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
Intent intent = new Intent(Intent.ACTION_PICK);
intent.setType("image/*");
startActivityForResult(intent, 2);
}
});
For saving image to fire base :
//Storing and getting image from firebase
#Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if(requestCode == GALLERY_INTENT && resultCode == RESULT_OK){
final Uri uri = data.getData();
StorageReference filepath = mStorageRef.child("Photos").child(name);
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
//Getting url where image is stored
saveImage(downloadUri);
Picasso.with(getActivity()).load(downloadUri).fit().centerCrop().into(profileImage);
}
});
}
This works fine for me, Hope this will help.
Instead of else if statement, I modified it by writing a seperate if statement.
if (requestCode == RC_SIGN_IN) {
if (resultCode == RESULT_OK) {
}else if (resultCode == RESULT_CANCELED) {
}
// This was the change I made
if (requestCode == RC_PHOTO_PICKER && resultCode == RESULT_OK) {
}
}
If ( //sign in)
{
If (//result ok)
{}
else if (//result cancel)
{}
}
else if (//rc photot picker && result ok)
{ //your storage code here.....}
tats works perfectly.....check out the braces carefully....
When I try to upload to Firebase my app is crashing with the following error.
I have an 'upload' button that is being used to invoke the camera. Upon clicking it, the image is not uploaded to Firebase.
My code:
private Button mUploadBtn;
private ImageView mImageView;
private static final int CAMERA_REQUEST_CODE = 1;
private StorageReference mStorage;
Uri photoURI;
private ProgressDialog mProgressDialog;
private static final int GALLERY_INTENT = 2;
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mStorage = FirebaseStorage.getInstance().getReference();
mUploadBtn = (Button) findViewById(R.id.upload);
mImageView = (ImageView) findViewById(R.id.imageView);
mProgressDialog = new ProgressDialog(this);
mUploadBtn.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
Intent intent = new Intent(android.provider.MediaStore.ACTION_IMAGE_CAPTURE);
startActivityForResult(intent, CAMERA_REQUEST_CODE);
}
});
}
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK) {
mProgressDialog.setMessage("Uplaoding");
mProgressDialog.show();
Uri uri = data.getData();
StorageReference filepath = mStorage.child("Photos").child("file");
filepath.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
mProgressDialog.dismiss();
Toast.makeText(MainActivity.this,"Done",Toast.LENGTH_LONG).show();
}
});
}
}
It looks like you're not working with the camera intent correctly. The code you show is expecting that the intent returned by the camera app contains a Uri to the captured image via its getData() method. That's not the way it works.
I recommend you follow this tutorial instead to work with the camera.
override this method
and do the following steps
#Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == CAMERA_REQUEST_CODE && resultCode == RESULT_OK)
{
Uri uri = data.getData();
StorageReference image_path = storageReference.child("Camera Photos").child(uri.getLastPathSegment());
image_path.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(MainActivity.this, "Image Uploaded", Toast.LENGTH_SHORT).show();
}
});
}
}
I want to dynamically create image views inside the onActivityResult method.
If i define my ImageView like this: imageView = (ImageView) findViewById(R.id.image_view); works perfectly with this code:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO && resultCode == RESULT_OK) {
Uri uri = data.getData();
StorageReference photoStorageReference = storageReference.child(uri.getLastPathSegment());
photoStorageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
Picasso.with(StorageActivity.this).load(downloadUri).fit().centerCrop().into(imageView);
}
});
}
}
But if i create the image views inside the onActivityResult method like this:
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_PHOTO && resultCode == RESULT_OK) {
Uri uri = data.getData();
StorageReference photoStorageReference = storageReference.child(uri.getLastPathSegment());
photoStorageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Uri downloadUri = taskSnapshot.getDownloadUrl();
ImageView imageView = new ImageView(getApplicationContext());
linearLayout.addView(imageView);
Picasso.with(StorageActivity.this).load(downloadUri).fit().centerCrop().into(imageView);
}
});
}
}
The image views are not displayed. I tried to create programmatically those image views in the onCreate method, but same problem. Nothing is displayed. If i create in stead of ImageView, buttons, the buttons are correctly displayed. What is wrong with my code?
Thanks in advance!
You forgot to specify the LayoutParameters for the newly added view. Like below
imageView.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT));