【问题标题】:Trying to access sub folders in assets folder尝试访问资产文件夹中的子文件夹
【发布时间】:2014-05-04 07:47:34
【问题描述】:

我的资产文件夹有一个子文件夹树..我正在尝试访问该目录中的 pdf .. url 如下

url = QP/28/28/mth.pdf

完整目录如下

ful directory = file:///data/data/com.example.testqstn/files/QP/28/28/mth.pdf

我已经使用以下代码访问了路径

intent.setDataAndType(Uri.parse("file://" + getApplicationContext().getFilesDir()
       + "/"+url), "application/pdf");
startActivity(intent);
     finish();

我没有收到任何错误消息,但 pdf 没有打开.. 当子文件夹没有使用,并且 pdf 只存在于资产文件夹中.. 然后 pdf 正确打开.. 那到底是什么问题???

并且日志猫没有显示任何错误..

【问题讨论】:

  • 另一个应用程序无法访问您的私人存储。所以你需要将pdf存储在sd卡上。
  • AFAIK,本机代码无法访问资产。

标签: android pdf android-intent


【解决方案1】:

已解决

真的很简单.. 要访问资产文件夹中的子文件夹,您需要以下内容..

第一次通过文件名访问文件..

    File file = new File(getFilesDir(), fname);

第二次尝试打开文件时同时使用文件名和文件的 url

    in = assetManager.open(url+fname);

意图中的第三个仅使用文件名..(我使用的是整个路径)

    intent.setDataAndType(
        Uri.parse("file://" + getFilesDir() + "/"+fname),
        "application/pdf");

【讨论】:

    【解决方案2】:

    你不需要传递文件路径,因为资产是在 apk 中构建的,所以你只需在这个例子中使用这个函数我从资产中读取文本文件..

          String[] files = assetManager.list("fonts/temp/temp1/HippaPrivarcyDocument");
          // Above line gives the list files in asset particular sub folder........
          InputStream input;
                    try {
                        input = getAssets().open("fonts/temp/temp1/HippaPrivarcyDocument");
                        int size = input.available();
                        byte[] buffer = new byte[size];
                        input.read(buffer);
                        input.close();
                        // byte buffer into a string
                        text = new String(buffer);
                    } catch (Exception e) {
    
                    }
    

    一切顺利

    【讨论】:

    • 如果我需要访问资产文件夹的子文件夹,我该怎么做..您只提到资产文件夹内部,但我想知道子文件夹文件。
    【解决方案3】:

    您必须使用三个///。试试这个:

    intent.setDataAndType(Uri.parse("file:///android_asset/" + "relative_path");
    

    【讨论】:

    • 你使用过android_asset文件夹吗?假设asset文件夹包含test1.txt(asset/test1.txt)..那么就写“file:///android_asset/”+“test1.txt”
    • 查看这个 GIT 示例,它展示了如何访问资产文件夹中的文件 https://github.com/commonsguy/cw-advandroid/tree/master/WebView/GeoWeb1
    猜你喜欢
    • 2015-03-13
    • 2012-05-13
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多