java - How to open a PDF file from in-app browser? -


i have basic file browser inside app , want able open pdf files, reason, though can tap on pdf files , open second, go away , phone gives toast message error, , toast haven't programmed anywhere in code. weird because same pdf file opens other activities without issues. (see openpdf() method works pdfmanager class) researched lot how fix couldn't find clear answer. helping!

the toast error: cannot display pdf (filename.pdf cannot opened)

onlistitemclick function androidfilebrowser:      @override     protected void onlistitemclick(listview l, view v, int position, long id) {         string filename = (string) getlistadapter().getitem(position);         if (path.endswith(file.separator)) {             filename = path + filename;         } else {             filename = path + file.separator + filename;         }         pdf = new file(environment.getexternalstoragepublicdirectory(environment.directory_downloads).getpath() + "/path/" + filename);         uri path = uri.fromfile(pdf);         intent intent = new intent();         intent.setaction(intent.action_view);         intent.setdataandtype(path, "application/pdf");         intent.setflags(intent.flag_activity_clear_top);         try {             startactivity(intent);         } catch (activitynotfoundexception e) {         e.printstacktrace();         }     } 

androidmanifest.xml:

<?xml version="1.0" encoding="utf-8"?> <manifest package="com.faraz.pdfreader"           xmlns:android="http://schemas.android.com/apk/res/android">      <uses-permission android:name="android.permission.camera"/>     <uses-permission android:name="android.permission.write_external_storage"/>     <uses-permission android:name="android.permission.internet"/>     <uses-permission android:name="android.permission.access_network_state"/>     <uses-permission android:name="android.permission.read_phone_state"/>      <application          android:allowbackup="true"         android:icon="@mipmap/ic_launcher"         android:label="@string/app_name"         android:theme="@style/apptheme"         >         <activity             android:name=".mainactivity"             android:label="@string/app_name"             android:screenorientation="portrait">             <intent-filter>                 <action android:name="android.intent.action.main"/>                  <category android:name="android.intent.category.launcher"/>             </intent-filter>         </activity>         <activity             android:name=".simplescanneractivity"             android:label="@string/simple_scanner_activity"             android:theme="@style/appoverlaytheme">         </activity>         <activity android:name=".pdfmanager"/>         <activity android:name=".androidfilebrowser"/>                   <!--android:screenorientation="portrait"-->     </application> </manifest> 

openpdf() method pdfmanager class

    public void openpdf() {         file pdf = new file(environment.getexternalstoragepublicdirectory(environment.directory_downloads).getpath() + "/path/" + pdfname + ".pdf");         if (pdf.exists()) {             uri path = uri.fromfile(pdf);             intent intent = new intent();             intent.setaction(intent.action_view);             intent.setdataandtype(path, "application/pdf");             intent.setflags(intent.flag_activity_clear_top);             try {                 startactivity(intent);             } catch (activitynotfoundexception e) {                 toast.maketext(pdfmanager.this,                         "no application available view pdf!",                         toast.length_short).show();      /*                intent intent = new intent(intent.action_view)                         .setdata(uri.parse("http://docs.google.com/gview?embedded=true&url=" + url));                 startactivity(intent);             }      */             }         }     }  } 

in onitemclick():

if (path.endswith(file.separator))  {     filename = path + filename; }  else  {     filename = path + file.separator + filename; }  pdf = new file(environment.getexternalstoragepublicdirectory(environment.directory_downloads).getpath() + "/path/" + filename); uri path = uri.fromfile(pdf); 

you use "path" before initialize it, depending on intentions either change name of 1 or other, or declare before if-statement not after.


Comments

Popular posts from this blog

javascript - Using jquery append to add option values into a select element not working -

Android soft keyboard reverts to default keyboard on orientation change -

Rendering JButton to get the JCheckBox behavior in a JTable by using images does not update my table -