Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Trying to access sub folders in assets folder

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

like image 651
Alvin Avatar asked Mar 26 '14 05:03

Alvin


2 Answers

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

like image 80
Naveen Kumar Kuppan Avatar answered Oct 22 '22 05:10

Naveen Kumar Kuppan


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");
like image 29
Alvin Avatar answered Oct 22 '22 03:10

Alvin