【问题标题】:I am trying to read a pdf file from android mobile memory but its showing "load pdf error"我正在尝试从 android 移动内存中读取 pdf 文件,但它显示“加载 pdf 错误”
【发布时间】:2020-10-30 05:59:31
【问题描述】:
protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_pdf_viewer);
        pdfView=(PDFView)findViewById(R.id.pdfView);
        Bundle extras = getIntent().getExtras();
        url = extras.getString("url");
        Uri uri=Uri.parse(url);
 pdfView.fromUri(uri)
 .pages(0, 2, 1, 3, 3, 3) 
                        .enableSwipe(true)
                        .swipeHorizontal(true)
                        .enableDoubletap(true)
                        .defaultPage(0)
                        .enableAnnotationRendering(false)
                        .password(null)
                        .scrollHandle(null)
                        .enableAntialiasing(true)                        
                        .spacing(2)
                        .autoSpacing(true) 
                        .pageFitPolicy(FitPolicy.WIDTH)
                        .fitEachPage(true) 
                        .pageSnap(true) 
                        .pageFling(true)
                        .nightMode(false) 
                        .load();
            } 

该文件位于给定的文件路径上:/storage/emulated/0/Android/data/edu/files/101.pdf 我到处都能找到正确的路径但仍然出现以下错误:

2020-10-30 02:58:17.947 7146-7146/edu E/PDFView: 加载 pdf 错误 java.io.FileNotFoundException:没有内容提供者:/storage/emulated/0/Android/data/edu/files/101.pdf

我已经尝试到了极限,但找不到错误,请指导我哪里错了?

【问题讨论】:

  • url 是文件系统路径,而不是 Uri。不要使用Uri.parse()。使用Uri.fromFile(new File(url))
  • 一切正常。你真的救了我的命……非常感谢。

标签: android android-studio android-layout


【解决方案1】:

有用的Uri 有一个方案,例如contenthttpsfileUri.parse() 会解析一个裸文件系统路径,但它没有将file 作为方案放入,生成的Uri 通常是没用的。

因此,如果您有一个字符串形式的文件系统路径,并且您需要一个Uri,请使用Uri.fromFile(new File(...)),其中... 是该路径。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2022-09-28
    • 2011-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-04-06
    • 1970-01-01
    相关资源
    最近更新 更多