简体   繁体   中英

Get path from picture downloaded from Firebase?

I'm making an app where a user can take and upload a picture and another user can download and view it in a ImageView . To prevent that I take up all ram of the app I get the whole picture down on storage.

It has worked with the standard formula on here; https://firebase.google.com/docs/storage/android/download-files

Problem is, I have no idea how to get the path that the image is downloading to. I cant find it in AVD monitor, and I need the URL for each downloaded picture, since it will be custom. Im trying with something like this where it makes a new dir and puts it there, but it doesnt work, nor can I get a path.

public void getImageFromPath() throws IOException {
            StorageReference islandRef = storageRef.child("userimages/test@test.dk/test@test.dk");

            String timeStamp = new SimpleDateFormat("yyyyMMdd_HHmmss").format(new Date());

            String imageFileName = "JPEG_" + timeStamp + "_";

            File directory = new File(Environment.getDataDirectory()
                    + "/Test/");
            if (!directory.exists()) {
                directory.mkdir();
            }
            final File localFile = File.createTempFile(imageFileName, ".jpg", directory);
            try {
                islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                    @Override
                    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                        Toast.makeText(getApplicationContext(), "Downloaded", Toast.LENGTH_SHORT).show();
                        Log.d(TAG, "Downloaded til path");
                        String mCurrentPhotoPath = "file:" + localFile.getAbsolutePath();
                        System.out.println(mCurrentPhotoPath);

                    }
                }).addOnFailureListener(new OnFailureListener() {
                    @Override
                    public void onFailure(@NonNull Exception exception) {
                        Log.d(TAG, "Downloaded IKKE til path");
                    }
                });
                throw new IOException("Works not");
            }
            catch(IOException e)
            {
                System.out.println(e.getMessage());
            }

        }

You're creating a temporary local file. That's where you download the data to.

You can then get the absolute path of that file with:

File localFile = File.createTempFile(imageFileName, ".jpg", directory);
String path = localFile.getAbsolutePath();

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM