如何在android studio中读取pdf文件
我是android开发的初学者,我想制作一款可以读取pdf文件的android应用程序。我拥有的pdf文件是大型竞赛(每个pdf文件大约60 mb),当我搜索时,我需要将文件存储在服务器类网站或其他地方(我不知道我到底需要什么),如果我将文件存储在服务器上,我如何让用户阅读pdf文件而不将其下载到手机中。如何在android studio中读取pdf文件,android,android-studio,Android,Android Studio,我是android开发的初学者,我想制作一款可以读取pdf文件的android应用程序。我拥有的pdf文件是大型竞赛(每个pdf文件大约60 mb),当我搜索时,我需要将文件存储在服务器类网站或其他地方(我不知道我到底需要什么),如果我将文件存储在服务器上,我如何让用户阅读pdf文件而不将其下载到手机中。 我发现我需要使用web视图,但我不确定这是否正确。如果我的想法是错误的,请告诉我什么是正确的方法。我希望有人能帮助我,因为我正在搜索这个,我没有找到任何具体的答案。谢谢你,你可以用不同的方式查
我发现我需要使用web视图,但我不确定这是否正确。如果我的想法是错误的,请告诉我什么是正确的方法。我希望有人能帮助我,因为我正在搜索这个,我没有找到任何具体的答案。谢谢你,你可以用不同的方式查看android项目中的pdf文件。我将在这里描述其中的一些- 1。使用库: 步骤如下:
安装 添加到build.gradle:
implementation'com.github.barteksc:android pdf viewer:2.8.2'
ProGuard 如果您正在使用ProGuard,请将以下规则添加到ProGuard配置文件:
-保持com.shockwave类。**
在布局中包含PDFView
进一步的链接是
2。将pdf url加载到webview:
3。手动将PDF文件显示到
Imageview
//使用butterknife视图绑定将手动PDF查看器创建到imageview的示例
公共类PdfRenderActivity扩展了AppCompative活动{
@BindView(R.id.pdf_image)ImageView imageViewPdf;
@BindView(R.id.button\u pre\u doc)浮动操作按钮prePageButton;
@BindView(R.id.button\u next\u doc)浮动ActionButton nextPageButton;
私有静态最终字符串FILENAME=“report.pdf”;
私有int页索引;
私人PdfRenderer PdfRenderer;
私人PDFRender.Page当前页面;
专用ParcelFileDescriptor ParcelFileDescriptor;
@凌驾
创建时受保护的void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
setContentView(R.layout.activity\uPDF\uRender);
把(这个)绑起来;
pageIndex=0;
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@凌驾
受保护的void onStart(){
super.onStart();
试一试{
openRenderer(getApplicationContext());
显示页面(页面索引);
}捕获(IOE异常){
e、 printStackTrace();
}
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@凌驾
公共void onStop(){
试一试{
关闭渲染器();
}捕获(IOE异常){
e、 printStackTrace();
}
super.onStop();
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@OnClick(R.id.button\u pre\u doc)
公共作废onPreviousDocClick(){
showPage(currentPage.getIndex()-1);
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
@OnClick(R.id.button\u next\u doc)
public void onNextDocClick(){
showPage(currentPage.getIndex()+1);
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
私有void openRenderer(上下文上下文)引发IOException{
//在本示例中,我们从assets目录中读取PDF。
File File=新文件(context.getCacheDir(),文件名);
如果(!file.exists()){
//由于PdfRenderer无法直接处理压缩的资产文件,因此我们将其复制到
//缓存目录。
InputStream asset=context.getAssets().open(文件名);
FileOutputStream输出=新的FileOutputStream(文件);
最终字节[]缓冲区=新字节[1024];
整数大小;
而((大小=资产读取(缓冲区))!=-1){
输出.写入(缓冲区,0,大小);
}
asset.close();
output.close();
}
parcelFileDescriptor=parcelFileDescriptor.open(文件,parcelFileDescriptor.MODE\u只读);
//这是我们用来渲染PDF的PdfRenderer。
if(parcelFileDescriptor!=null){
pdfRenderer=新的pdfRenderer(parcelFileDescriptor);
}
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
私有void closeRenderer()引发IOException{
如果(null!=currentPage){
currentPage.close();
}
pdfRenderer.close();
parcelFileDescriptor.close();
}
@RequiresApi(api=Build.VERSION\u code.LOLLIPOP)
专用void显示页(整数索引){
if(pdfrender.getPageCount()是的,如果你不想下载该文件,你需要使用web view。你还需要在服务器上设置一个URL来呈现和托管pdf。如果你不想处理这个问题,你可以使用google docs提供的一个,但我建议不要在产品代码中使用它。试试这个:hey@Saurabh谢谢你的回复。我没有任何问题服务器,但我看到,当我使用web view时,我需要使用google docs viewer,google docs viewer将允许用户下载pdf文件。是否有我可以使用的库或查看器,并且只允许用户阅读pdf文件?谢谢,我不知道有。在这种情况下,您必须自己渲染它。
webview = (WebView)findViewById(R.id.webview);
progressbar = (ProgressBar) findViewById(R.id.progressbar);
webview.getSettings().setJavaScriptEnabled(true);
String filename = file_url_with_name;
webview.loadUrl(file_url + filename);
webview.setWebViewClient(new WebViewClient() {
public void onPageFinished(WebView view, String url) {
// do your stuff here
progressbar.setVisibility(View.GONE);
}
});
// Example for creating manual PDF viewer into imageview used butterknife view binding<br/>
public class PdfRenderActivity extends AppCompatActivity {
@BindView(R.id.pdf_image) ImageView imageViewPdf;
@BindView(R.id.button_pre_doc) FloatingActionButton prePageButton;
@BindView(R.id.button_next_doc) FloatingActionButton nextPageButton;
private static final String FILENAME = "report.pdf";
private int pageIndex;
private PdfRenderer pdfRenderer;
private PdfRenderer.Page currentPage;
private ParcelFileDescriptor parcelFileDescriptor;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_pdf_render);
ButterKnife.bind(this);
pageIndex = 0;
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onStart() {
super.onStart();
try {
openRenderer(getApplicationContext());
showPage(pageIndex);
} catch (IOException e) {
e.printStackTrace();
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
public void onStop() {
try {
closeRenderer();
} catch (IOException e) {
e.printStackTrace();
}
super.onStop();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@OnClick(R.id.button_pre_doc)
public void onPreviousDocClick(){
showPage(currentPage.getIndex() - 1);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@OnClick(R.id.button_next_doc)
public void onNextDocClick(){
showPage(currentPage.getIndex() + 1);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void openRenderer(Context context) throws IOException {
// In this sample, we read a PDF from the assets directory.
File file = new File(context.getCacheDir(), FILENAME);
if (!file.exists()) {
// Since PdfRenderer cannot handle the compressed asset file directly, we copy it into
// the cache directory.
InputStream asset = context.getAssets().open(FILENAME);
FileOutputStream output = new FileOutputStream(file);
final byte[] buffer = new byte[1024];
int size;
while ((size = asset.read(buffer)) != -1) {
output.write(buffer, 0, size);
}
asset.close();
output.close();
}
parcelFileDescriptor = ParcelFileDescriptor.open(file, ParcelFileDescriptor.MODE_READ_ONLY);
// This is the PdfRenderer we use to render the PDF.
if (parcelFileDescriptor != null) {
pdfRenderer = new PdfRenderer(parcelFileDescriptor);
}
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void closeRenderer() throws IOException {
if (null != currentPage) {
currentPage.close();
}
pdfRenderer.close();
parcelFileDescriptor.close();
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void showPage(int index) {
if (pdfRenderer.getPageCount() <= index) {
return;
}
// Make sure to close the current page before opening another one.
if (null != currentPage) {
currentPage.close();
}
// Use `openPage` to open a specific page in PDF.
currentPage = pdfRenderer.openPage(index);
// Important: the destination bitmap must be ARGB (not RGB).
Bitmap bitmap = Bitmap.createBitmap(currentPage.getWidth(), currentPage.getHeight(),
Bitmap.Config.ARGB_8888);
// Here, we render the page onto the Bitmap.
// To render a portion of the page, use the second and third parameter. Pass nulls to get
// the default result.
// Pass either RENDER_MODE_FOR_DISPLAY or RENDER_MODE_FOR_PRINT for the last parameter.
currentPage.render(bitmap, null, null, PdfRenderer.Page.RENDER_MODE_FOR_DISPLAY);
// We are ready to show the Bitmap to user.
imageViewPdf.setImageBitmap(bitmap);
updateUi();
}
/**
* Updates the state of 2 control buttons in response to the current page index.
*/
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
private void updateUi() {
int index = currentPage.getIndex();
int pageCount = pdfRenderer.getPageCount();
prePageButton.setEnabled(0 != index);
nextPageButton.setEnabled(index + 1 < pageCount);
}
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
public int getPageCount() {
return pdfRenderer.getPageCount();
}
}