You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Android Studio中如何在应用内直接展示从Firebase获取的PDF而非下载

安卓Firebase PDF应用实现方案

1. 自定义PDF列表布局(实现图标+文本单行展示)

1.1 新建列表项布局

首先在res/layout目录下新建item_pdf.xml,用来定义单个PDF条目的样式:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="horizontal"
    android:padding="16dp"
    android:gravity="center_vertical">

    <!-- 左侧PDF图标 -->
    <ImageView
        android:id="@+id/iv_pdf_icon"
        android:layout_width="40dp"
        android:layout_height="40dp"
        android:src="@drawable/ic_pdf" <!-- 自行准备PDF图标放入drawable目录即可 -->
        android:layout_marginEnd="16dp"/>

    <!-- 中间文本区域,可放文件名、上传时间两个文本 -->
    <LinearLayout
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:layout_weight="1"
        android:orientation="vertical">

        <TextView
            android:id="@+id/tv_pdf_name"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="16sp"
            android:textColor="#000"
            android:maxLines="1"
            android:ellipsize="end"/>

        <TextView
            android:id="@+id/tv_pdf_time"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:textSize="12sp"
            android:textColor="#666"
            android:layout_marginTop="4dp"/>
    </LinearLayout>

    <!-- 右侧更多操作按钮 -->
    <ImageView
        android:id="@+id/iv_more"
        android:layout_width="24dp"
        android:layout_height="24dp"
        android:src="@drawable/ic_more"/>

</LinearLayout>

1.2 替换原列表适配器

原来使用的系统默认ArrayAdapter仅支持展示单文本,需要改成自定义适配器适配上面的布局,修改RetrievePDF里的适配器逻辑:

// 把原来的ArrayAdapter替换成这个自定义Adapter
public class PdfListAdapter extends ArrayAdapter<putPDF> {
    public PdfListAdapter(Context context, List<putPDF> pdfList) {
        super(context, 0, pdfList);
    }

    @NonNull
    @Override
    public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) {
        if (convertView == null) {
            convertView = LayoutInflater.from(getContext()).inflate(R.layout.item_pdf, parent, false);
        }
        putPDF currentPdf = getItem(position);
        // 绑定控件
        TextView tvName = convertView.findViewById(R.id.tv_pdf_name);
        TextView tvTime = convertView.findViewById(R.id.tv_pdf_time);
        ImageView ivIcon = convertView.findViewById(R.id.iv_pdf_icon);
        
        tvName.setText(currentPdf.getName());
        // 如果你putPDF类里存储了上传时间可以直接赋值,不需要的话可以删掉该文本控件
        // tvTime.setText(currentPdf.getUploadTime());
        return convertView;
    }
}

然后修改retrievePDFFiles方法里的适配器设置代码:

// 删掉原来的ArrayAdapter相关代码,替换为以下内容
PdfListAdapter adapter = new PdfListAdapter(RetrievePDF.this, uploadedPDF);
listView.setAdapter(adapter);

1.3 修正原有布局的滑动冲突

现有布局把ListView放在ScrollView里会导致滑动异常,直接删掉ScrollView层即可,修改activity_retrieve_pdf.xml里的中间内容部分:

<!-- 删掉原来的ScrollView和嵌套的LinearLayout,直接放置ListView -->
<ListView
    android:id="@+id/listView"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_above="@id/footer"
    android:layout_below="@id/header"
    android:padding="7dp" />

2. 应用内打开PDF实现

你需要新建一个单独的Activity承载PDFView,这样逻辑拆分清晰,后续维护也更方便,步骤如下:

2.1 导入PDFView依赖

先在模块级build.gradle(Module: app)的dependencies块里添加依赖,同步项目:

implementation 'com.github.barteksc:android-pdf-viewer:3.2.0-beta.1'

2.2 新建PDF查看页

新建空ActivityPdfViewActivity,对应布局activity_pdf_view.xml:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <com.github.barteksc.pdfviewer.PDFView
        android:id="@+id/pdfview"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>
</LinearLayout>

2.3 修改列表页点击跳转逻辑

修改RetrievePDF里的ListView点击事件:

listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
    @Override
    public void onItemClick(AdapterView<?> parent, View view, int i, long l) {
        putPDF putPDF = uploadedPDF.get(i);
        // 跳转到自定义的PDF查看页,传递PDF的文件名/路径
        Intent intent = new Intent(RetrievePDF.this, PdfViewActivity.class);
        intent.putExtra("PDF_NAME", putPDF.getName());
        startActivity(intent);
    }
});

2.4 实现PDF加载逻辑

在PdfViewActivity的onCreate方法里添加加载代码:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_pdf_view);
    PDFView pdfView = findViewById(R.id.pdfview);
    String pdfName = getIntent().getStringExtra("PDF_NAME");
    
    StorageReference storageRef = FirebaseStorage.getInstance().getReference().child("uploadPDF/" + pdfName);
    storageRef.getStream().addOnSuccessListener(taskSnapshot -> {
        pdfView.fromStream(taskSnapshot.getStream())
                .enableSwipe(true)
                .swipeHorizontal(false)
                .enableDoubletap(true)
                .load();
    }).addOnFailureListener(e -> {
        Toast.makeText(PdfViewActivity.this, "加载失败:" + e.getMessage(), Toast.LENGTH_SHORT).show();
    });
}

内容的提问来源于stack exchange,提问作者Jason Yeoh

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.29 16:54:00