首页 > 解决方案 > 使用改造 2 显示为空的 PDF 文件

问题描述

我正在开发一个应用程序,我正在下载 PDF 文件,将其保存到内部存储,然后使用 FileProvider 在其他应用程序中打开该文件。

注意:这可能是一个重复的问题,我已经完成了 StackOverflow 上的大部分问题,但仍然没有找到解决方案。

文件下载得很好,但是当我打开它时,它是空的。下载的文件是 30 kb,它有 5 页,但都是空的。最初,我认为它是空的,因为其他应用程序没有打开文件的权限,但我做了另一件事来检查它是否是权限问题。我已将文件保存到外部存储中,但它仍然是空的。因此,这意味着这不是权限问题。

请注意: 除了 pdf 文件,还有一些 .xls 文件,当我在 excel android 应用程序中打开这些文件时,它说无法打开文件。这表明在写入字节流时存在一些问题。

改造接口.java

@GET(ApiConstants.END_POINT_DOWNLOAD_DOCUMENT)
@Streaming
Call<ResponseBody> downloadDocument(@Query("bucket") String bucket, @Query("filename") String fileName);

下载文件的代码:这里我正在检查文件是否已经存在,然后返回文件,否则下载文件。

public LiveData<Resource<File>> openOrDownloadFile(String bucket, String fileName) {
        MutableLiveData<Resource<File>> documentLiveData = new MutableLiveData<>();
        documentLiveData.postValue(Resource.loading(null));
        Context context = MyApp.getInstance();
        final File file = new File(context.getFilesDir(), fileName);
        if (file.exists()) {
            documentLiveData.postValue(Resource.success(file));
        } else {
            Call<ResponseBody> call = apiService.downloadDocument(bucket, fileName);
            call.enqueue(new Callback<ResponseBody>() {
                @Override
                public void onResponse(Call<ResponseBody> call, Response<ResponseBody> response) {
                    if (response.isSuccessful()) {
                        appExecutors.diskIO().execute(new Runnable() {
                            @Override
                            public void run() {
                                try {
                                    InputStream inputStream = null;
                                    OutputStream outputStream = null;
                                    try {
                                        byte[] fileReader = new byte[4096];
                                        inputStream = response.body().byteStream();
                                        outputStream = new FileOutputStream(file);
                                        while (true) {
                                            int read = inputStream.read(fileReader);
                                            if (read == -1) {
                                                break;
                                            }
                                            outputStream.write(fileReader, 0, read);
                                        }
                                        documentLiveData.postValue(Resource.success(file));
                                        outputStream.flush();
                                    } catch (IOException e) {
                                        documentLiveData.postValue(Resource.error("Error: Unable to save file/n"+e.getLocalizedMessage(), null));
                                    } finally {
                                        if (inputStream != null) {
                                            inputStream.close();
                                        }

                                        if (outputStream != null) {
                                            outputStream.close();
                                        }
                                    }
                                } catch (IOException e) {
                                    Log.e(AppConstants.TAG, e.getMessage(), e);
                                    documentLiveData.postValue(Resource.error("Error: Unable to save file/n"+e.getLocalizedMessage(), null));
                                }
                            }
                        });
                    } else {
                        documentLiveData.postValue(Resource.error("Unable to download file", null));
                    }
                }

                @Override
                public void onFailure(Call<ResponseBody> call, Throwable t) {
                    documentLiveData.postValue(Resource.error(t.getLocalizedMessage(), null));
                }
            });
        }

        return documentLiveData;
    }

片段代码

private void onItemClickListener(Document document) {
        mDocumentsViewModel.openORDownloadFile(document.getType(), document.getName()).observe(this, new Observer<Resource<File>>() {
            @Override
            public void onChanged(@Nullable Resource<File> fileResource) {
                binding.setResource(fileResource);
                if (fileResource.status == Status.SUCCESS) {
                    openFile(fileResource.data);
                }
            }
        });


    }

    void openFile(File file) {
        Intent intent = new Intent(Intent.ACTION_VIEW);
        Uri uri = FileProvider.getUriForFile(getContext(), BuildConfig.APPLICATION_ID, file);
        intent.setDataAndType(uri, mDocumentsViewModel.getMimeType(file.getAbsolutePath()));
        intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
        PackageManager pm = getActivity().getPackageManager();
        if (intent.resolveActivity(pm) != null) {
            startActivity(intent);
        } else {
            Toast.makeText(getActivity(), "This file cannot be opened on this device. Please download some compatible app from play store", Toast.LENGTH_SHORT).show();
        }
    }

以下是版本: ext.retrofit_version = "2.4.0" ext.okhttp_version = "3.8.0"

我正在努力解决这个问题,如果你能指出这个问题,这将是一个很大的帮助。谢谢你。

更新:问题出在后端 API 上。我的代码是正确的。一旦他们在那里解决了问题,它就开始在我身边工作而没有任何改变。

标签: androidretrofit2

解决方案


推荐阅读