6

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..

Alvin
  • 416
  • 1
  • 8
  • 18

3 Answers3

4

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

Naveen Kumar Kuppan
  • 1,424
  • 1
  • 10
  • 12
  • Incase i need to access sub folder of the assets folder how can i do.. you mentioned only for inside the asset folder but i want to know sub folder files. – Sethu Mar 26 '14 at 05:37
1

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");
Alvin
  • 416
  • 1
  • 8
  • 18
0

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

intent.setDataAndType(Uri.parse("file:///android_asset/" + "relative_path");
Zohra Khan
  • 5,182
  • 4
  • 25
  • 34
  • Have you used android_asset folder. Suppose the asset folder contains test1.txt (asset/test1.txt).. Then just write "file:///android_asset/" + "test1.txt" – Zohra Khan Mar 26 '14 at 15:04
  • Check this GIT example which shows how to access file in asset folderhttps://github.com/commonsguy/cw-advandroid/tree/master/WebView/GeoWeb1 – Zohra Khan Mar 26 '14 at 15:07