Как загрузить текстовый файл из Firebase Storage и манипулировать данными в нем? - PullRequest
0 голосов
/ 29 марта 2019

Я пытаюсь загрузить текстовый файл из Firebase Storage и отобразить его значения пользователю в соответствии с предпочтениями. Мне просто нужен фрагмент кода. Я пытался, но не получилось.

Я получаю ошибку:

W/System.err: java.io.FileNotFoundException:
 /data/user/0/com.ritesh.excelfiletest/files/text.txt

Код:

private void download() {
        StorageReference sr = FirebaseStorage.getInstance().getReference().child("text.txt");
        Log.d("keys tasksnapshot anf", sr.toString());


        try {
            File localFile = File.createTempFile("text", "txt");

            sr.getFile(localFile)
                    .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
                            Log.d("keys: tasksnapshot", taskSnapshot.toString());
                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

        openFile();
}

private void openFile() {
    FileInputStream fis = null;
    try {
        fis = openFileInput("text.txt");
        InputStreamReader isr = new InputStreamReader(fis);
        BufferedReader bufferedReader = new BufferedReader(isr);
        StringBuilder sb = new StringBuilder();
        String line;
        while ((line = bufferedReader.readLine()) != null) {
            sb.append(line);
        }


        TextView textView = findViewById(R.id.tv);
        textView.setText(sb.toString());

    } catch (FileNotFoundException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }

}

1 Ответ

0 голосов
/ 29 марта 2019

Я не нашел решения, как хотел, но немного изменил код. Вот оно:

Буду также признателен, если кто-нибудь скажет мне, почему «Файл» выше не работает.

private void download(String s) {
        StorageReference sr = FirebaseStorage.getInstance().getReference().child("text.txt");


        try {
            File localFile = File.createTempFile("text", "txt");

            Uri.Builder uri = new Uri.Builder();
            uri.appendEncodedPath(getFilesDir()+"/text.txt");
            Uri u = uri.build();

            Log.d("keysss byte transf", u.toString());


            sr.getFile(u)
                    .addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
                        @Override
                        public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {

                        }
                    });
        } catch (Exception e) {
            e.printStackTrace();
        }

        openFile();
} 
...