【问题标题】:how to retrieve pdf file firebase once?如何一次检索pdf文件firebase?
【发布时间】:2018-03-10 11:32:47
【问题描述】:

在我使用 Firebase 存储中的下载链接将其加载到我的 PDFView 中之后,我正在使用 Firebase 存储来存储我的 PDF 书字节[] 数组 它工作正常,但我只想在该用户离线浏览后第一次加载它请帮助我在哪里离线添加 Firebase 我只需要检索那本书一次,因此用户第一次只需要互联网
我还看到了文档放在哪里

FirebaseDatabase.getInstance().setPersistenceEnabled(true);

这是我的活动 .java,它运行良好,每次我都等着取回我的书,请帮助我

package com.xxxx.xxxxx;

import android.app.ProgressDialog;
import android.os.AsyncTask;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ProgressBar;


import com.github.barteksc.pdfviewer.PDFView;
import com.google.firebase.database.DataSnapshot;
import com.google.firebase.database.DatabaseError;
import com.google.firebase.database.DatabaseReference;
import com.google.firebase.database.FirebaseDatabase;
import com.google.firebase.database.ValueEventListener;

import org.apache.commons.io.IOUtils;

import java.io.BufferedInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.URL;

import javax.net.ssl.HttpsURLConnection;

public class ReadActivity extends AppCompatActivity {
 PDFView pdf;
 String url;
 DatabaseReference mData;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_read);
        pdf=findViewById(R.id.pdfView);
url="https://firebasestorage.googleapis.com/v0/b/programminglib-978cc.appspot.com/o/JavaScriptEnlightenment-arabic-17.01-itwadi.com.pdf?alt=media&token=aa160de7-8cfa-4ec1-813f-0d67e2d8f307";
        new RetrievePDFbyte()
                .execute(url);



    }

class RetrievePDFbyte extends AsyncTask<String,Void,byte[]>{
    ProgressDialog progressDialog;
    protected void onPreExecute()
    {
        progressDialog = new ProgressDialog(ReadActivity.this);
        progressDialog.setTitle("getting the book content...");
        progressDialog.setMessage("Please wait...");
        progressDialog.setCanceledOnTouchOutside(false);
        progressDialog.show();

    }

    @Override
    protected byte [] doInBackground(String... strings) {
        InputStream inputStream = null;
        try{
            URL url=new URL(strings[0]);
            HttpsURLConnection httpsURLConnection=(HttpsURLConnection)url.openConnection();
            if (httpsURLConnection.getResponseCode()==200){
                inputStream=new BufferedInputStream(httpsURLConnection.getInputStream());
            }
        }catch (IOException e){
            return null;
        }
        try {
            return IOUtils.toByteArray(inputStream);
        } catch (IOException e) {
            e.printStackTrace();
        }
        return  null;
    }

    @Override
    protected void onPostExecute(byte[] bytes) {
        pdf.fromBytes(bytes).load();
        progressDialog.dismiss();
    }

}
}

【问题讨论】:

  • 从 inputStream 创建文件并保存在本地,
  • 使用 FirebaseDatabase.getInstance().setPersistenceEnabled(true); 在 Firebase 实时数据库(用于同步 JSON 数据的数据库)中启用磁盘持久性。它与用于存储和访问文件的 Firebase Storage 无关。
  • 如果我将 url 存储在 firebase 数据库中然后加载 url ?有效吗?

标签: android firebase firebase-storage offlineapps


【解决方案1】:

为了实现这一点,您需要下载您的.pdf 文件并将其本地存储在用户设备上。这意味着即使没有互联网连接,用户也可以读取文件,因为用户将能够从其设备打开文件。根据official documentation,您可以使用以下代码实现这一点:

islandRef = storageRef.child("images/island.jpg");

File localFile = File.createTempFile("images", "jpg");

islandRef.getFile(localFile).addOnSuccessListener(new OnSuccessListener<FileDownloadTask.TaskSnapshot>() {
    @Override
    public void onSuccess(FileDownloadTask.TaskSnapshot taskSnapshot) {
        // Local temp file has been created
    }
}).addOnFailureListener(new OnFailureListener() {
    @Override
    public void onFailure(@NonNull Exception exception) {
        // Handle any errors
    }
});

但是请记住,每次打开应用程序时都会检查文件是否存在,只有在文件不存在时才下载。

【讨论】:

  • 仍然不明白如何实现请帮助
  • 你有什么不明白的?
  • 您只需粘贴 Firebase 文档中的代码,请解释一下将代码放在哪里以及我还需要什么?
  • 是的,我只是复制粘贴到这里。这是一个简单的例子。您需要将此代码添加到您实际需要它的 onCreate 方法中。不需要更多的东西。
  • 兄弟我很困惑,我使用了很多功能来下载文件,但所有的方法都是手动设置 url 链接
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2014-07-12
  • 2017-01-18
  • 1970-01-01
  • 2012-11-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多