1

I'm trying to open a PDF file out of my Android application using Adobe Reader. When my code gets excecuted, the Adobe Reader opens but throws the following error:'Error: The document path is not valid' The file blabla.pdf is in my Application rootfolder

String filename = "blabla.pdf";
File file = new File(filename);
Uri internal = Uri.fromFile(file);
Intent target = new Intent(Intent.ACTION_VIEW);
target.setDataAndType(internal, "application/pdf");
target.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);

intent = Intent.createChooser(target, "Open File");
try {
    startActivity(intent);
} catch (ActivityNotFoundException e) {
    Toast.makeText(this, "U hebt geen PDF viewer geïnstalleerd op dit toestel. " +
        "Ga naar de app store en download een PDF viewer om dit bestand te openen.", Toast.LENGTH_LONG).show();
}
startActivity(intent);

I want to use internal storage, not external storage. I've used this example: Link and didn't really understand these 2 examples: Link1 and Link2

Community
  • 1
  • 1
Jelle
  • 1,020
  • 2
  • 13
  • 22

3 Answers3

2

1)First copy the file to internal storage. -> Keep your pdf file in assets folder.Use follwing function to copy file to internal storage

private void copy( InputStream in, File dst ) throws IOException {
    FileOutputStream out = new FileOutputStream( dst );
    byte[] buf = new byte[1024];
    int len;

    while ( ( len = in.read( buf ) ) > 0 ) {
        out.write( buf, 0, len );
    }

    in.close( );
    out.close( );
}

function call for the same :

File f = new File( getContext( ).getFilesDir( ), "abc.pdf" );
AssetManager assets = getContext( ).getResources( ).getAssets( );
copy( assets.open( "abc.pdf" ), f );

2)Now you have your file in internal storage so use following code to get file object

File f = new File( getContext( ).getFilesDir( )+"/"+abc.pdf);
shwetap
  • 621
  • 1
  • 6
  • 14
0

Use below code to check whether the file path is correct or not

if(file.exists()){
  //file path is correct 
}else{
  //file path is not correct 
}
Santosh Kathait
  • 1,444
  • 1
  • 11
  • 22
0

You must set full path to file. Like in example you shown

File file = new File(Environment.getExternalStorageDirectory().getAbsolutePath() +"/"+ filename);
Erzer
  • 86
  • 3
  • This works, but I'd like to use the internal storage method with out having to acces external storage – Jelle May 22 '14 at 14:09
  • Application haven't access to inner data of other application. Read this [link](http://developer.android.com/intl/ru/reference/android/os/Environment.html#getExternalStorageDirectory()) about method. External storage is not really external. – Erzer May 23 '14 at 06:26