如何在Android中使用翻新从rest api读取Pdf文件

问题描述 投票:0回答:3

这是我的json数据来自后端。如何使用Retofit库读取此Pdf文件。

预先感谢

{
        "data": [
            {
                "Invoice": "Bhavdip-html-to-pdf (1).pdf"
            }
        ]
    }
android retrofit2
3个回答
0
投票

您必须首先使用以下命令下载pdf文件

下载管理器

之后,您可以使用该库进行读取。

Library for read pdf in java

注意:

您必须在json中输入PDF的网址


0
投票

请参阅https://www.codexpedia.com/android/android-download-large-file-using-retrofit-streaming/

对于大文件,这不是一个好方案。如果文件较小,则可以使用翻新版进行下载,但是如果文件较大,则应使用下载管理器。

以上链接可帮助您下载经过改造的文件。


0
投票
URL url = new URL( f_url[0] );
                URLConnection conection = url.openConnection();
                conection.connect();
                // getting file length
                int lenghtOfFile = conection.getContentLength();

                // input stream to read file - with 8k buffer
                InputStream input = new BufferedInputStream( url.openStream(), 1024 );

                // Output stream to write file
                String extStorageDirectory = Environment.getExternalStorageDirectory().toString();
                File folder = new File( extStorageDirectory );

                String timeStamp = new SimpleDateFormat( "yyyyMMdd_HHmmss", Locale.getDefault() ).format( new Date() );
                String fileName = "SMART_" + timeStamp + "_" + Brochure.substring( Brochure.lastIndexOf( '/' ) + 1 );

                File file = new File( folder, fileName );
                try {
                    file.createNewFile();
                } catch (IOException e1) {
                    e1.printStackTrace();
                }
                OutputStream output = new FileOutputStream( file);

                byte[] data = new byte[1024];

                long total = 0;

                while ((read( data )) != -1) {
                   // writing data to file
                    output.write( data, 0, count );
                }

                // flushing output
                output.flush();

                // closing streams
                output.close();
                input.close();

            } catch (Exception e) {
                Log.e( "Error: ", e.getMessage() );
            }

这是从服务器下载pdf文件的方式

© www.soinside.com 2019 - 2024. All rights reserved.