Trying to access sub folders in assets folder

自闭症网瘾萝莉.ら 提交于 2019-12-23 10:14:09

问题


my assets folder has a tree of sub folders.. i am trying to access a pdf in that directory .. the url is as follows

url = QP/28/28/mth.pdf

the complete directory is as follows

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

i have accessed the path using the following code

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

i am not getting any error msgs but the pdf is not opening.. when the sub folders are not used, and the pdf's are just present in the asset folder.. then the pdf opens correctly.. so what exactly is the problem???

and the log cat is not displaying any error..


回答1:


You won't need to pass the file path because assets are in build in the apk so you just use this function in this example i read the text file from the assets..

      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) {

                }

All the best




回答2:


SOLVED

it was really simple.. to access the sub folders in assets folder you would need the following..

1st access the file by its file name..

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

2nd while attempting to open the file use both file name and the url to the file

    in = assetManager.open(url+fname);

3rd in the intent use only the file name..(i was using the entire path)

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



回答3:


You have to use three ///. Try this:

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


来源:https://stackoverflow.com/questions/22652009/trying-to-access-sub-folders-in-assets-folder

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!