这是我的json数据来自后端。如何使用Retofit库读取此Pdf文件。
预先感谢
{
"data": [
{
"Invoice": "Bhavdip-html-to-pdf (1).pdf"
}
]
}
请参阅https://www.codexpedia.com/android/android-download-large-file-using-retrofit-streaming/
对于大文件,这不是一个好方案。如果文件较小,则可以使用翻新版进行下载,但是如果文件较大,则应使用下载管理器。
以上链接可帮助您下载经过改造的文件。
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文件的方式