im not getting image uri .....the image stored in firebase - android

storageRef.child("users/me/profile.png").getBytes(Long.MAX_VALUE).addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] bytes) {
Uri image=uri;
Toast.makeText(viewpgdetails.this, "image uri="+image , Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(viewpgdetails.this, "failed to get image", Toast.LENGTH_SHORT).show();
// Handle any errors
}
});
// [END download_full_example]
}

It is unclear which type of URI you want, so I'll include both versions.
Google Storage URI
This is not very useful outside of a Google APIs.
This will be in the format: gs://PROJECT_ID.appspot.com/path/to/file.png
To get this value, you would use the following code:
String storageUri = storage.getReference('path/to/file.png').toString();
In the question's code, you would use:
StorageReference fileRef = storageRef.child("users/me/profile.png")
fileRef.getBytes(Long.MAX_VALUE)
.addOnSuccessListener(new OnSuccessListener<byte[]>() {
#Override
public void onSuccess(byte[] bytes) {
Toast.makeText(viewpgdetails.this, "image uri: " + fileRef.toString(), Toast.LENGTH_SHORT).show();
// Use the bytes to display the image
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(viewpgdetails.this, "failed to get image", Toast.LENGTH_SHORT).show();
// Handle any errors
}
});
HTTPS Resource URL
This URL would be used if you wanted to include the image in a web page or link to it in an email.
This will be in the format: https://firebasestorage.googleapis.com/v0/b/PROJECT_ID.appspot.com/o/path%2Fto%2Ffile.png?alt=media&token=ACCESS_TOKEN
Because this URL uses an access token issued by the server, it must be requested and returned to the client asynchronously. This is achieved with the following code:
Task<Uri> getDownloadUrlTask = storage.getReference('path/to/file.png').getDownloadUrl();
In the question's code, you would use:
StorageReference fileRef = storageRef.child("users/me/profile.png")
fileRef.getDownloadUrl()
.addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri downloadUrl) {
Toast.makeText(viewpgdetails.this, "image url: " + downloadUrl, Toast.LENGTH_SHORT).show();
// Use URL for internal web page, etc.
}
})
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(viewpgdetails.this, "failed to get image url", Toast.LENGTH_SHORT).show();
// Handle any errors
}
});
Note: If you want both the HTTPS URL and the file as a byte array, they would have to be requested as seperate Tasks using both getBytes() and getDownloadUrl().
See also
Cloud Storage for Android Guide
StorageReference API Reference

Related

Can't upload profile picture from Facebook since the profile picture is in the form of a download link

I tried to upload facebook profile picture to Firebase storage, using the following code.
public void uploadFbPfp(Uri uri) throws IOException {
Toast.makeText(Create_Profile.this, "test0", Toast.LENGTH_SHORT).show();
InputStream iStream = getContentResolver().openInputStream(uri);
byte[] inputData = getBytes(iStream);
Toast.makeText(Create_Profile.this, "test1", Toast.LENGTH_SHORT).show();
storageReference = FirebaseStorage.getInstance().getReference("User/"+mAuth.getCurrentUser().getUid());
Toast.makeText(Create_Profile.this, "test2", Toast.LENGTH_LONG).show();
storageReference.putBytes(inputData).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Create_Profile.this,"Profile successfully updated", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(Create_Profile.this, "Failed", Toast.LENGTH_LONG).show();
}
});
}
Uri was passed as Uri.parse(downloadLink), download link is in the form of a string
I only got the toast message for test0
Warning from logcat:
java.io.FileNotFoundException: No content provider: https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=&height=200&width=200&ext=1677946916&hash=AeQk8fUGBl7VJk7jOe
I removed the id for confidentiality hence the link will not be working
Initially I tried putFile to storageReference, however got the warning that said
could not retrieve file size for upload https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=&height=200&width=200&ext=1677946916&hash=AeQk8fUGBl7VJk7jOe
and
java.io.FileNotFoundException: No content provider: https://platform-lookaside.fbsbx.com/platform/profilepic/?asid=&height=200&width=200&ext=1677946916&hash=AeQk8fUGBl7VJk7jOe
This was the code i used
private void uploadProfilePic(Uri imageUri){
Uri uri = imageUri;
storageReference = FirebaseStorage.getInstance().getReference("User/"+mAuth.getCurrentUser().getUid());
storageReference.putFile(uri).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(Create_Profile.this, "Profile successfully updated", Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(Create_Profile.this, "Failed to upload the image", Toast.LENGTH_LONG).show();
}
});
}

How to get https:// url from firebase storage

How can I get the url of this link?
What you point at there is known as the download URL of the file. To get that URL, you'll want to follow the instructions on accessing a file through a URL:
StorageReference photoRef = storageRef.child("path/to/photo2.jpg");
photoRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
System.out.println(uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
throw exception
}
});

How to get URL from Firebase Storage and send it to database? [duplicate]

This question already has an answer here:
getDownloadURL isn't inputting the link i need
(1 answer)
Closed 2 years ago.
I'm trying to upload an image to Firebase Storage and after that send the URL of this image to Firebase Database. The Uri is correct, but when I try to set it on my object, the method singleDetection.setImage(imagePath) is setting nothing. Here is my code:
Bitmap image = detectedFaceItems.get(0).getImage();
StorageReference storageRef = storage.getReference();
StorageReference imagesRef = storageRef.child("2.jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
image.compress(Bitmap.CompressFormat.JPEG, 100, baos);
byte[] data = baos.toByteArray();
StorageTask<UploadTask.TaskSnapshot> uploadTask = imagesRef.putBytes(data)
.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Failed to sent a file", Toast.LENGTH_LONG).show();
}
})
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(), "Successfully sent a file", Toast.LENGTH_LONG).show();
storageRef.child("2.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//Toast.makeText(getApplicationContext(), uri.toString(), Toast.LENGTH_LONG).show();
//Doesn't work corretly
String imagePath = uri.toString();
singleDetection.setImage(imagePath);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Failed to get an Uri", Toast.LENGTH_LONG).show();
}
});
}
});
You are uploading the image right, but your call to Firebase after successfully uploading the image using storageRef.child("2.jpg").getDownloadUrl(). not that right as you already don't know the generated Uri which you can get from the snapshot that is returned back with the taskSnapshot.
So replace the below part of code:
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getApplicationContext(), "Successfully sent a file", Toast.LENGTH_LONG).show();
storageRef.child("2.jpg").getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
//Toast.makeText(getApplicationContext(), uri.toString(), Toast.LENGTH_LONG).show();
//Doesn't work corretly
String imagePath = uri.toString();
singleDetection.setImage(imagePath);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(getApplicationContext(), "Failed to get an Uri", Toast.LENGTH_LONG).show();
}
});
}
});
With:
.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
// get upload url
taskSnapshot.getStorage().getDownloadUrl().addOnCompleteListener(new OnCompleteListener<Uri>() {
#Override
public void onComplete(#NonNull Task<Uri> task) {
String imagePath = String.valueOf(task.getResult());
singleDetection.setImage(imagePath);
}
});
}
});
Note: here you've uploaded the image itself, but don't upload its Uri, so you can to decide to upload it to firebase as you'd like.

Firebase Storage image upload successful through Android App but how to get url?

I have used this library in my Android app
implementation 'com.google.firebase:firebase-core:17.0.0'
implementation 'com.google.firebase:firebase-firestore:20.0.0'
implementation 'com.google.firebase:firebase-storage:18.0.0'
And this method use to upload my image on Firebase Storage:
StorageReference storageRef = mStorage.getReference();
finalStorageReference mountainsRef = storageRef.child("myImgName");
Uri file = Uri.fromFile(new File(myImgName));
UploadTask uploadTask = mountainsRef.putFile(file);
uploadTask.addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle unsuccessful uploads
progressDialog.dismiss();
Log.e(TAG, "img Error :" + exception.getMessage());
//Toast.makeText(MainActivity.this, "Failed "+e.getMessage(), Toast.LENGTH_SHORT).show();
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
progressDialog.dismiss();
//Log.e(TAG, "Task :" + taskSnapshot.getTask());
//Log.e(TAG, "Class Store:" + taskSnapshot.getStorage().getDownloadUrl());
Log.e(TAG,"metaData :"+taskSnapshot.getMetadata().getPath());
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, etc.
// ...
}
}).addOnProgressListener(new OnProgressListener<UploadTask.TaskSnapshot>() {
#Override
public void onProgress(UploadTask.TaskSnapshot taskSnapshot) {
double progress = (100.0 * taskSnapshot.getBytesTransferred() / taskSnapshot
.getTotalByteCount());
progressDialog.setMessage("Uploaded " + (int) progress + "%");
}
});
this code perfectly working for me to upload image. but how to know the image location url ?
To get the download URL for a file in Cloud Storage, you call getDownloadUrl() on the StorageReference to that file. getDownloadUrl() returns a task, so you'll need to add a success listener to get the result.
mountainsRef.getStorage().getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
// Got the download URL for 'users/me/profile.png' in uri
System.out.println(uri.toString());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
For more on this, see:
the sample in the documentation on uploading a file
the documentation on downloading a file
How to use getdownloadurl in recent versions?
You can find your project's URL at the top of the Files section of Storage in the Firebase Console
FirebaseStorage storage = FirebaseStorage.getInstance();
StorageReference storageRef = storage.getReferenceFromUrl("gs://example-firebase.appspot.com").child("android.jpg");
You can create a File object and attempt to load the file you want by calling getFile on your StorageReference with the new File object passed as a parameter. Since this operation happens asynchronously, you can add an OnSuccessListener and OnFailureListener
try {
final File localFile = File.createTempFile("images", "jpg");
storageRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Bitmap bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
mImageView.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
}
});
} catch (IOException e ) {}
You can get the file's Url by using the getDownloadUrl() method on your StorageReference, which will give you a Uri pointing to the file's location.
storageRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Log.e("Image +", "uri: " + uri.toString());
//Handle whatever you're going to do with the URL here
}
});

android: I want to retrieve an image from firebase and show it in a new activity

This is how I upload the image to Firebase storage.
Every time the app opens, I want to fetch image from storage and display in new activity.
StorageReference childRef=mStorage.child(newsnow).child("image3");
UploadTask uploadTask=childRef.putFile(uri3);
uploadTask.addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
#Override
public void onSuccess(UploadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(PostNews.this, "uploaded", Toast.LENGTH_SHORT).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(PostNews.this, "no da", Toast.LENGTH_SHORT).show();
}
});
For retrieving an image from firebase you have to store image URL in your Firebase database. After retrieving an image URL you can use libraries like Picasso, glide and so many others for getting an image from URL. You can prefer this link for more description.
Something like this should to the trick:
ImageView image = (ImageView) findViewById(R.id.image_placeholder);
childRef.getDownloadUrl().addOnSuccessListener(new OnSuccessListener<Uri>() {
#Override
public void onSuccess(Uri uri) {
Picasso.with(MainActivity.this).load(uri).fit().centerCrop().into(image);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
// Handle any errors
}
});
Where image with R.id.image_placeholder is the place where you would like the image to be displayed.
Note that childRef is initialized in the same way that is now in your posted code.

Categories

Resources