Java – Firebase Storage Android : How can I change the filename of a downloaded file?

Firebase Storage Android : How can I change the filename of a downloaded file?… here is a solution to the problem.

Firebase Storage Android : How can I change the filename of a downloaded file?

Hello, I am trying to download a file from Firebase storage to the cache. That works great. My problem is that I can’t change the whole filename. I can only change the prefix and suffix. Here is the filename I got.

System.out: myImage -1452943744jpg

How do I remove a number and change the file name to myImage?

Here is my code :

try {

localFile = File.createTempFile( "myImage " ,"jpg");
        System.out.println(localFile.getName());
    } catch (IOException e) {
        e.printStackTrace();
    }

liftImage.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
        @Override
        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
             Local temp file has been created
            myBitmap = BitmapFactory.decodeFile(localFile.getAbsolutePath());
            myImageView.setImageBitmap(myBitmap);

}

Solution

When you request that the system create a temporary file, it generates a unique file name using the prefix and suffix that you provide and a string that makes the name unique. In this case, the string is a numeric field that you observe, possibly from the current system time. If you want full control over the file name, you can’t use createTempFile().

Instead, decide where the file is located and create it like this:

localFile = new File(XXXX, "myImage.jpg");

where XXXX is one of the Context methods that return the directory:

This guide describes some considerations for using caches, internal and external file storage.

Related Problems and Solutions