I would like to load an image, from Firebase Storage, to the action bar. I tried using Glide, the same way I would, if I was loading the reference into an ImageView. This wouldn’t work, I did some digging and found a reference to Glide.with(this).load(source).into(new Task){ but I wasn’t sure how to exicute this, or if it would allow me to set the image as an action bar icon/logo. Any advice would be appreciated. I am new to android. I learned java, coding desktop applications with JavaFX. The transition has taken some adjusting, and this has truly stumped me. I felt like their was probably a simple solution, but I am such a novice, it is escaping me. I have hacked at this for a few days, with no luck. I’ll take advice in any form. Thank you.
Immediately after posting this I tried something new, and it worked. Here is the code I used. I downloaded it to a file, and then created a Drawable, from the file.
StorageReference image = storageReference.child(firebaseAuth.getUid()).child("Images").child("Profile_Pic");
try {
final File localFile = File.createTempFile("images", "jpg");
image.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
String pathName = localFile.getPath();
Drawable d = Drawable.createFromPath(pathName);
actionBar.setLogo(d);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(HowItWorksActivity.this, "OOPS", Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
Toast.makeText(HowItWorksActivity.this, "Realy off", Toast.LENGTH_LONG).show();
}StorageReference image = storageReference.child(firebaseAuth.getUid()).child("Images").child("Profile_Pic");
try {
final File localFile = File.createTempFile("images", "jpg");
image.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
String pathName = localFile.getPath();
Drawable d = Drawable.createFromPath(pathName);
actionBar.setLogo(d);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(HowItWorksActivity.this, "OOPS", Toast.LENGTH_LONG).show();
}
});
} catch (IOException e) {
Toast.makeText(HowItWorksActivity.this, "Realy off", Toast.LENGTH_LONG).show();
}
Related
okay so the method I have tried is .getfile() for downloading the image. it triggers onsuccess listener as well but I can't locate the file in my internal storage, not sure if I can access it
#Override
public void onDownloadClick(int position) {
Toast.makeText(this, "will try to dwonload XD" , Toast.LENGTH_LONG).show();
Upload selectedItem= mUploads.get(position);
final String selectedkey= selectedItem.getKey();
StorageReference imgRef=mStorage.getReferenceFromUrl(selectedItem.getImageUrl());
try{
File localFIle=File.createTempFile("images", "jpg");
imgRef.getFile(localFIle).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(ImagesActivity.this, "downloaded" , Toast.LENGTH_LONG).show();
Log.d("file ", "filedownloaded");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Toast.makeText(ImagesActivity.this, "not able to download" , Toast.LENGTH_LONG).show();
}
});
}
catch (Exception e)
{
}
}
It looks like you're creating a File object called localFIle. If you want to know what path that it, just log its string value. If that's not the location you want, try something else. I suggest reading the documentation on locating files on Android devices.
I am pretty new with this, and I could use some help.
I am having difficulties downloading files from firebase storage into my android device storage. Problem is that files are downloaded into cache memory of app, and I would like to use them when I'm offline. Other thing is that I am not using SD card, so I would like my files downloaded into some of user accessible folder, if possible into download folder in internal memory.
Here is my code:
StorageReference ref = storage.getReferenceFromUrl("gs://diplomskirad-948a7.appspot.com/Dennis Lloyd - NEVERMIND.mp3");
File localFile=null;
try {
localFile = File.createTempFile("audio", ".mp3");
} catch (IOException e) {
e.printStackTrace();
}
ref.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.d("Success", "File downloaded successfully");
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("Error", "Error downloading file");
}
});
I guess should somehow change my "onSuccess" method, but I didn't have any success until now.
Try this:-
StorageReference ref = storage.getReferenceFromUrl("gs://diplomskirad-948a7.appspot.com/Dennis Lloyd - NEVERMIND.mp3");
File path= new File(Environment.getExternalStorageDirectory(), "file_name");
if(!path.exists()) {
path.mkdirs();
}
final File localFile = new File(path,"audio.mp3");
ref.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.d("Success", "File downloaded successfully " + localFile.toString());
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Log.e("Error", "Error downloading file");
}
});
people of StackOverflow.
Recently I finish a course of Android Studio andd in classes we begin to work with Firebase. I managed that my application reproduce audios in mp3 from the database, but now I would like to share it in another application, for it, I have done this:
this.stRef = FirebaseStorage.getInstance().getReference().child("audios/audioname.mp3");
File localFile = null;
String downloadedaudio="";
try {
localFile = File.createTempFile("audioname", ".mp3");
downloadedaudio = localFile.getPath();
} catch (IOException e) {
e.printStackTrace();
}
stRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getContext(),"Downloaded",Toast.LENGTH_LONG).show();
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(getContext(),"Failed to Download"+exception,Toast.LENGTH_LONG).show();
}
});
The app download a temp file, i check it in Android Studio file explorer, but then i does not share anything:
Uri uri = Uri.parse(downloadedaudio);
Intent share = new Intent(Intent.ACTION_SEND);
share.setType("audio/mp3");
share.putExtra(Intent.EXTRA_STREAM,uri);
c.startActivity(Intent.createChooser(share, "Compartir sonido en: "));
}
Any idea of what i am doing wrong?
Thank you.
I have stored the url of the pdf file stored in firebase storage, in database inside the node Pdf. I am trying to download the file from firebase storage with the help of the url stored inside the database.
public class DownloadFile extends AppCompatActivity{
DatabaseReference databaseReference= FirebaseDatabase.getInstance().getReference();
String root_child="my book";
#Override
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
databaseReference.child("Pdf").addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(DataSnapshot dataSnapshot) {
if(dataSnapshot.hasChild(root_child))
{
String url=dataSnapshot.child(root_child).getValue().toString();
StorageReference island=storageRef.child("books").child(root_child).child(url);
island.getFile(file).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
pdfView.fromFile(file).load();
}
});
}
}
The problem is nothing really shows up inside the pdf view. When I debug the app I found that StorageReference island=storageRef.child("books").child(root_child).child(url); is not creating the right reference. In short the file is not downloading. Since the file name varies according to what users upload, it is not possible for me to specify the file name hence I used 'child(url)' so that it can search the file using the url but I am not sure if that is the right way to search a file.Any help is appreciated.
As you have discovered while debugging, the file is not downloading because the reference is not correct.
How are you getting info from the user about which file to download ?
I believe you must be showing him the list of files which he/she has uploaded. So in that list you can store the info about the file_name. Now when the user clicks on the list-item, you can have the correct reference since you know the file_name from the clicked list-item.
In the same matter i give you a different approach that i use when i was getting that same error.
actually i save file in Firebase Storage with the name of post_id to which the file is related and then give shot to the following code
give a look
private void downloadFile(final String postKey) {
File cFile = new File(getContext().getCacheDir(), "app Directory");
final File cLocalFile = new File(cFile, postKey);
if (!rootPath.exists()) {
rootPath.mkdirs();
}
if (!cFile.exists()) {
cFile.mkdirs();
}
final FileDownloadTask downloadingTask = mStorage.child(postKey).getFile(cLocalFile);
downloadingTaskProgress(downloadingTask, cLocalFile);
}
private void downloadingTaskProgress(FileDownloadTask downloadingTask, final File cLocalFile) {
downloadingTask.addOnProgressListener(new OnProgressListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onProgress(final FileDownloadTask.TaskSnapshot taskSnapshot) {
double progressSize = (100.0 * taskSnapshot.getBytesTransferred())
/ taskSnapshot.getTotalByteCount();
}
}).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Toast.makeText(getActivity(), "File download complete: " + model.fileName, Toast.LENGTH_SHORT).show();
try {
writeToStorage(cLocalFile);
} catch (IOException e) {
e.printStackTrace();
}
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception exception) {
Toast.makeText(getContext(), "local file not created: " +
exception.toString(), Toast.LENGTH_LONG).show();
}
});
}
private void writeToStorage(File src) throws IOException {
FileChannel inChannel = new FileInputStream(src).getChannel();
FileChannel outChannel = new FileOutputStream(localFile).getChannel();
try {
inChannel.transferTo(0, inChannel.size(), outChannel);
} finally {
if (inChannel != null)
inChannel.close();
if (outChannel != null)
outChannel.close();
}
}
give some modification as per your need and this approach could work as it is in mine....
I got the solution of the problem myself.
I used StorageReference island=storage.getReferenceFromUrl(url); instead of StorageReference island=storageRef.child("books").child(root_child).child(url); . This solved my problem.
I wants to download all content of a Folder in FirebaseStorage. I can download a only file (image) like this:
FirebaseStorage storage = FirebaseStorage.getInstance();
try {
final File localFile = File.createTempFile("ImagensExercicios","bmp");
storage.getReference().child("ImagensExercicios").child("abdominal_1.bmp").getFile(localFile).
addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
#Override
public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
Log.i(TAG,"onSuccess()");
Bitmap bitmap;
bitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
imageView.setImageBitmap(bitmap);
}
}).addOnFailureListener(new OnFailureListener() {
#Override
public void onFailure(#NonNull Exception e) {
Log.i(TAG,"onFailure() " + e.getMessage());
}
});
} catch (Exception e) {
e.printStackTrace();
}
This works, but I wants download all images of the folder. Exists a way to do this?
In the firebase folder rename the items as
abdominal_1.bmp
abdominal_2.bmp
abdominal_3.bmp
And use above try catch inside a forloop.