Cannot retrieve images from firebase storage
So Far i have succeeded uploading images to firebase but while retrieving them to recycler view i'm facing problems i.e images are not being retrieved at all.
Take a look at my source code:
This is Recycler View Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ImageViewHolder> {
Context mContext;
private List<Upload> mUploads;
public MyAdapter(Context context, List<Upload> uploads) {
this.mContext = context;
mUploads = uploads;
}
@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.layout_images, parent, false);
return new ImageViewHolder(v);
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
}
@Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
imageView = itemView.findViewById(R.id.image_view_upload);
}
}
This is my main class to retrieve images:
public class Viewimages extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewimages);
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new MyAdapter(Viewimages.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Viewimages.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
In this image the image name is successfully fetched but inside the cardview images are not being populated
This the picture of database where url is stored
|
show 4 more comments
So Far i have succeeded uploading images to firebase but while retrieving them to recycler view i'm facing problems i.e images are not being retrieved at all.
Take a look at my source code:
This is Recycler View Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ImageViewHolder> {
Context mContext;
private List<Upload> mUploads;
public MyAdapter(Context context, List<Upload> uploads) {
this.mContext = context;
mUploads = uploads;
}
@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.layout_images, parent, false);
return new ImageViewHolder(v);
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
}
@Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
imageView = itemView.findViewById(R.id.image_view_upload);
}
}
This is my main class to retrieve images:
public class Viewimages extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewimages);
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new MyAdapter(Viewimages.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Viewimages.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
In this image the image name is successfully fetched but inside the cardview images are not being populated
This the picture of database where url is stored
you have stored download_url to yourFirebaseDatabase?
– Ali Ahmed
Nov 22 '18 at 11:53
Show your Database. which data you're trying to retrieve.
– Ali Ahmed
Nov 22 '18 at 11:54
Yes download url is stored to firbase database
– ruchit patel
Nov 22 '18 at 11:58
You should check if the imageUrl's are correct, try opening them in a browser.
– Berke Atac
Nov 22 '18 at 11:59
@ruchitpatel Upload Image of your Database where you've stored download_urls
– Ali Ahmed
Nov 22 '18 at 12:00
|
show 4 more comments
So Far i have succeeded uploading images to firebase but while retrieving them to recycler view i'm facing problems i.e images are not being retrieved at all.
Take a look at my source code:
This is Recycler View Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ImageViewHolder> {
Context mContext;
private List<Upload> mUploads;
public MyAdapter(Context context, List<Upload> uploads) {
this.mContext = context;
mUploads = uploads;
}
@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.layout_images, parent, false);
return new ImageViewHolder(v);
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
}
@Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
imageView = itemView.findViewById(R.id.image_view_upload);
}
}
This is my main class to retrieve images:
public class Viewimages extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewimages);
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new MyAdapter(Viewimages.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Viewimages.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
In this image the image name is successfully fetched but inside the cardview images are not being populated
This the picture of database where url is stored
So Far i have succeeded uploading images to firebase but while retrieving them to recycler view i'm facing problems i.e images are not being retrieved at all.
Take a look at my source code:
This is Recycler View Adapter:
public class MyAdapter extends RecyclerView.Adapter<MyAdapter.ImageViewHolder> {
Context mContext;
private List<Upload> mUploads;
public MyAdapter(Context context, List<Upload> uploads) {
this.mContext = context;
mUploads = uploads;
}
@Override
public ImageViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
View v = LayoutInflater.from(mContext).inflate(R.layout.layout_images, parent, false);
return new ImageViewHolder(v);
}
@Override
public void onBindViewHolder(ImageViewHolder holder, int position) {
Upload uploadCurrent = mUploads.get(position);
holder.textViewName.setText(uploadCurrent.getName());
Picasso.get()
.load(uploadCurrent.getImageUrl())
.fit()
.centerCrop()
.into(holder.imageView);
}
@Override
public int getItemCount() {
return mUploads.size();
}
public class ImageViewHolder extends RecyclerView.ViewHolder {
public TextView textViewName;
public ImageView imageView;
public ImageViewHolder(View itemView) {
super(itemView);
textViewName = itemView.findViewById(R.id.text_view_name);
imageView = itemView.findViewById(R.id.image_view_upload);
}
}
This is my main class to retrieve images:
public class Viewimages extends AppCompatActivity {
private RecyclerView mRecyclerView;
private MyAdapter mAdapter;
private DatabaseReference mDatabaseRef;
private List<Upload> mUploads;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_viewimages);
mRecyclerView = findViewById(R.id.recyclerView);
mRecyclerView.setHasFixedSize(true);
mRecyclerView.setLayoutManager(new LinearLayoutManager(this));
mUploads = new ArrayList<>();
mDatabaseRef = FirebaseDatabase.getInstance().getReference("uploads");
mDatabaseRef.addValueEventListener(new ValueEventListener() {
@Override
public void onDataChange(DataSnapshot dataSnapshot) {
for (DataSnapshot postSnapshot : dataSnapshot.getChildren()) {
Upload upload = postSnapshot.getValue(Upload.class);
mUploads.add(upload);
}
mAdapter = new MyAdapter(Viewimages.this, mUploads);
mRecyclerView.setAdapter(mAdapter);
}
@Override
public void onCancelled(DatabaseError databaseError) {
Toast.makeText(Viewimages.this, databaseError.getMessage(), Toast.LENGTH_SHORT).show();
}
});
}
In this image the image name is successfully fetched but inside the cardview images are not being populated
This the picture of database where url is stored
edited Nov 22 '18 at 12:02
ruchit patel
asked Nov 22 '18 at 11:46
ruchit patelruchit patel
104
104
you have stored download_url to yourFirebaseDatabase?
– Ali Ahmed
Nov 22 '18 at 11:53
Show your Database. which data you're trying to retrieve.
– Ali Ahmed
Nov 22 '18 at 11:54
Yes download url is stored to firbase database
– ruchit patel
Nov 22 '18 at 11:58
You should check if the imageUrl's are correct, try opening them in a browser.
– Berke Atac
Nov 22 '18 at 11:59
@ruchitpatel Upload Image of your Database where you've stored download_urls
– Ali Ahmed
Nov 22 '18 at 12:00
|
show 4 more comments
you have stored download_url to yourFirebaseDatabase?
– Ali Ahmed
Nov 22 '18 at 11:53
Show your Database. which data you're trying to retrieve.
– Ali Ahmed
Nov 22 '18 at 11:54
Yes download url is stored to firbase database
– ruchit patel
Nov 22 '18 at 11:58
You should check if the imageUrl's are correct, try opening them in a browser.
– Berke Atac
Nov 22 '18 at 11:59
@ruchitpatel Upload Image of your Database where you've stored download_urls
– Ali Ahmed
Nov 22 '18 at 12:00
you have stored download_url to your
FirebaseDatabase ?– Ali Ahmed
Nov 22 '18 at 11:53
you have stored download_url to your
FirebaseDatabase ?– Ali Ahmed
Nov 22 '18 at 11:53
Show your Database. which data you're trying to retrieve.
– Ali Ahmed
Nov 22 '18 at 11:54
Show your Database. which data you're trying to retrieve.
– Ali Ahmed
Nov 22 '18 at 11:54
Yes download url is stored to firbase database
– ruchit patel
Nov 22 '18 at 11:58
Yes download url is stored to firbase database
– ruchit patel
Nov 22 '18 at 11:58
You should check if the imageUrl's are correct, try opening them in a browser.
– Berke Atac
Nov 22 '18 at 11:59
You should check if the imageUrl's are correct, try opening them in a browser.
– Berke Atac
Nov 22 '18 at 11:59
@ruchitpatel Upload Image of your Database where you've stored download_urls
– Ali Ahmed
Nov 22 '18 at 12:00
@ruchitpatel Upload Image of your Database where you've stored download_urls
– Ali Ahmed
Nov 22 '18 at 12:00
|
show 4 more comments
2 Answers
2
active
oldest
votes
Exactly as @Alex said, you're not storing the valid url, that's why you're not able to retrieve those images from the database.
For storing the url perfectly from your Firebase storage to your Firebase Database, you can use a code like this:
This code also contains the part where you can upload the image to your firebase storage, so I think this would make you relate to your code and may help you, even more.
private void uploadFile(Bitmap bitmap) {
FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReference();
final StorageReference ImagesRef = storageRef.child("images/"+mAu.getCurrentUser().getUid()+".jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte data = baos.toByteArray();
final UploadTask uploadTask = ImagesRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.i("whatTheFuck:",exception.toString());
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (!task.isSuccessful()) {
Log.i("problem", task.getException().toString());
}
return ImagesRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getCurrentUser().getUid());
Log.i("seeThisUri", downloadUri.toString());// This is the one you should store
ref.child("imageURL").setValue(downloadUri.toString());
} else {
Log.i("wentWrong","downloadUri failure");
}
}
});
}
});
}
The url that you can see in the code from downloadUri.toString(), this is the one you should be storing in your database.
Thanks your answer helped a lot i did some changes and got the correct download url
– ruchit patel
Nov 22 '18 at 16:00
@ruchitpatel no worries, glad to help :)
– PradyumanDixit
Nov 22 '18 at 16:49
add a comment |
You aren't getting anything because in your actual database you aren't storing proper image urls but some addresses of some object from the memory. So basically the problem is when you are uploading the images and saving the urls to your Firebase database. So you should convert those uri objects to String, so it can be stored correctly.
com.google.android.gms.tasks.zzu@13dd
Is not a valid url address.
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430319%2fcannot-retrieve-images-from-firebase-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
2 Answers
2
active
oldest
votes
2 Answers
2
active
oldest
votes
active
oldest
votes
active
oldest
votes
Exactly as @Alex said, you're not storing the valid url, that's why you're not able to retrieve those images from the database.
For storing the url perfectly from your Firebase storage to your Firebase Database, you can use a code like this:
This code also contains the part where you can upload the image to your firebase storage, so I think this would make you relate to your code and may help you, even more.
private void uploadFile(Bitmap bitmap) {
FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReference();
final StorageReference ImagesRef = storageRef.child("images/"+mAu.getCurrentUser().getUid()+".jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte data = baos.toByteArray();
final UploadTask uploadTask = ImagesRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.i("whatTheFuck:",exception.toString());
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (!task.isSuccessful()) {
Log.i("problem", task.getException().toString());
}
return ImagesRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getCurrentUser().getUid());
Log.i("seeThisUri", downloadUri.toString());// This is the one you should store
ref.child("imageURL").setValue(downloadUri.toString());
} else {
Log.i("wentWrong","downloadUri failure");
}
}
});
}
});
}
The url that you can see in the code from downloadUri.toString(), this is the one you should be storing in your database.
Thanks your answer helped a lot i did some changes and got the correct download url
– ruchit patel
Nov 22 '18 at 16:00
@ruchitpatel no worries, glad to help :)
– PradyumanDixit
Nov 22 '18 at 16:49
add a comment |
Exactly as @Alex said, you're not storing the valid url, that's why you're not able to retrieve those images from the database.
For storing the url perfectly from your Firebase storage to your Firebase Database, you can use a code like this:
This code also contains the part where you can upload the image to your firebase storage, so I think this would make you relate to your code and may help you, even more.
private void uploadFile(Bitmap bitmap) {
FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReference();
final StorageReference ImagesRef = storageRef.child("images/"+mAu.getCurrentUser().getUid()+".jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte data = baos.toByteArray();
final UploadTask uploadTask = ImagesRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.i("whatTheFuck:",exception.toString());
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (!task.isSuccessful()) {
Log.i("problem", task.getException().toString());
}
return ImagesRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getCurrentUser().getUid());
Log.i("seeThisUri", downloadUri.toString());// This is the one you should store
ref.child("imageURL").setValue(downloadUri.toString());
} else {
Log.i("wentWrong","downloadUri failure");
}
}
});
}
});
}
The url that you can see in the code from downloadUri.toString(), this is the one you should be storing in your database.
Thanks your answer helped a lot i did some changes and got the correct download url
– ruchit patel
Nov 22 '18 at 16:00
@ruchitpatel no worries, glad to help :)
– PradyumanDixit
Nov 22 '18 at 16:49
add a comment |
Exactly as @Alex said, you're not storing the valid url, that's why you're not able to retrieve those images from the database.
For storing the url perfectly from your Firebase storage to your Firebase Database, you can use a code like this:
This code also contains the part where you can upload the image to your firebase storage, so I think this would make you relate to your code and may help you, even more.
private void uploadFile(Bitmap bitmap) {
FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReference();
final StorageReference ImagesRef = storageRef.child("images/"+mAu.getCurrentUser().getUid()+".jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte data = baos.toByteArray();
final UploadTask uploadTask = ImagesRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.i("whatTheFuck:",exception.toString());
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (!task.isSuccessful()) {
Log.i("problem", task.getException().toString());
}
return ImagesRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getCurrentUser().getUid());
Log.i("seeThisUri", downloadUri.toString());// This is the one you should store
ref.child("imageURL").setValue(downloadUri.toString());
} else {
Log.i("wentWrong","downloadUri failure");
}
}
});
}
});
}
The url that you can see in the code from downloadUri.toString(), this is the one you should be storing in your database.
Exactly as @Alex said, you're not storing the valid url, that's why you're not able to retrieve those images from the database.
For storing the url perfectly from your Firebase storage to your Firebase Database, you can use a code like this:
This code also contains the part where you can upload the image to your firebase storage, so I think this would make you relate to your code and may help you, even more.
private void uploadFile(Bitmap bitmap) {
FirebaseStorage storage = FirebaseStorage.getInstance();
final StorageReference storageRef = storage.getReference();
final StorageReference ImagesRef = storageRef.child("images/"+mAu.getCurrentUser().getUid()+".jpg");
ByteArrayOutputStream baos = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.JPEG, 20, baos);
byte data = baos.toByteArray();
final UploadTask uploadTask = ImagesRef.putBytes(data);
uploadTask.addOnFailureListener(new OnFailureListener() {
@Override
public void onFailure(@NonNull Exception exception) {
Log.i("whatTheFuck:",exception.toString());
}
}).addOnSuccessListener(new OnSuccessListener<UploadTask.TaskSnapshot>() {
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
@Override
public void onSuccess(final UploadTask.TaskSnapshot taskSnapshot) {
// taskSnapshot.getMetadata() contains file metadata such as size, content-type, and download URL.
Task<Uri> urlTask = uploadTask.continueWithTask(new Continuation<UploadTask.TaskSnapshot, Task<Uri>>() {
@Override
public Task<Uri> then(@NonNull Task<UploadTask.TaskSnapshot> task) {
if (!task.isSuccessful()) {
Log.i("problem", task.getException().toString());
}
return ImagesRef.getDownloadUrl();
}
}).addOnCompleteListener(new OnCompleteListener<Uri>() {
@Override
public void onComplete(@NonNull Task<Uri> task) {
if (task.isSuccessful()) {
Uri downloadUri = task.getResult();
DatabaseReference ref = FirebaseDatabase.getInstance().getReference().child("users").child(mAu.getCurrentUser().getUid());
Log.i("seeThisUri", downloadUri.toString());// This is the one you should store
ref.child("imageURL").setValue(downloadUri.toString());
} else {
Log.i("wentWrong","downloadUri failure");
}
}
});
}
});
}
The url that you can see in the code from downloadUri.toString(), this is the one you should be storing in your database.
answered Nov 22 '18 at 12:20
PradyumanDixitPradyumanDixit
2,1672818
2,1672818
Thanks your answer helped a lot i did some changes and got the correct download url
– ruchit patel
Nov 22 '18 at 16:00
@ruchitpatel no worries, glad to help :)
– PradyumanDixit
Nov 22 '18 at 16:49
add a comment |
Thanks your answer helped a lot i did some changes and got the correct download url
– ruchit patel
Nov 22 '18 at 16:00
@ruchitpatel no worries, glad to help :)
– PradyumanDixit
Nov 22 '18 at 16:49
Thanks your answer helped a lot i did some changes and got the correct download url
– ruchit patel
Nov 22 '18 at 16:00
Thanks your answer helped a lot i did some changes and got the correct download url
– ruchit patel
Nov 22 '18 at 16:00
@ruchitpatel no worries, glad to help :)
– PradyumanDixit
Nov 22 '18 at 16:49
@ruchitpatel no worries, glad to help :)
– PradyumanDixit
Nov 22 '18 at 16:49
add a comment |
You aren't getting anything because in your actual database you aren't storing proper image urls but some addresses of some object from the memory. So basically the problem is when you are uploading the images and saving the urls to your Firebase database. So you should convert those uri objects to String, so it can be stored correctly.
com.google.android.gms.tasks.zzu@13dd
Is not a valid url address.
add a comment |
You aren't getting anything because in your actual database you aren't storing proper image urls but some addresses of some object from the memory. So basically the problem is when you are uploading the images and saving the urls to your Firebase database. So you should convert those uri objects to String, so it can be stored correctly.
com.google.android.gms.tasks.zzu@13dd
Is not a valid url address.
add a comment |
You aren't getting anything because in your actual database you aren't storing proper image urls but some addresses of some object from the memory. So basically the problem is when you are uploading the images and saving the urls to your Firebase database. So you should convert those uri objects to String, so it can be stored correctly.
com.google.android.gms.tasks.zzu@13dd
Is not a valid url address.
You aren't getting anything because in your actual database you aren't storing proper image urls but some addresses of some object from the memory. So basically the problem is when you are uploading the images and saving the urls to your Firebase database. So you should convert those uri objects to String, so it can be stored correctly.
com.google.android.gms.tasks.zzu@13dd
Is not a valid url address.
answered Nov 22 '18 at 12:07
Alex MamoAlex Mamo
41.5k72859
41.5k72859
add a comment |
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53430319%2fcannot-retrieve-images-from-firebase-storage%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
you have stored download_url to your
FirebaseDatabase?– Ali Ahmed
Nov 22 '18 at 11:53
Show your Database. which data you're trying to retrieve.
– Ali Ahmed
Nov 22 '18 at 11:54
Yes download url is stored to firbase database
– ruchit patel
Nov 22 '18 at 11:58
You should check if the imageUrl's are correct, try opening them in a browser.
– Berke Atac
Nov 22 '18 at 11:59
@ruchitpatel Upload Image of your Database where you've stored download_urls
– Ali Ahmed
Nov 22 '18 at 12:00