I am working on an android app that requires browsing a pdf from phone and then storing it's content into a String. I have the following code:
//src= location of the pdf file obtained from while browsing pdf from the phone (eg:"/sdcard/bluetooth/My_pdf.pdf")
public void convertPDFToText(String src) {
try {
//create pdf reader
PdfReader pr = new PdfReader(src);
//get the number of pages in the document
int pNum = pr.getNumberOfPages();
//extract text from each page and write it to the output text file
for (int page = 1; page <= pNum; page++) {
//text is the required String (initialized as "" )
text = text + PdfTextExtractor.getTextFromPage(pr, page);
}
} catch (Exception e) {
e.printStackTrace();
}
}
When I run this code separately in a java file, it runs perfectly fine (changing the src input according to the location of the pdf in the system), but when I run the same code in android studio, it gives me an exception saying:
java.io.IOException: /sdcard/bluetooth/My_pdf.pdf not found as file or resource.
at com.itextpdf.text.io.RandomAccessSourceFactory.createByReadingToMemory(RandomAccessSourceFactory.java:263)
at com.itextpdf.text.io.RandomAccessSourceFactory.createBestSource(RandomAccessSourceFactory.java:173)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:223)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:207)
at com.itextpdf.text.pdf.PdfReader.<init>(PdfReader.java:197)
at com.example.prachisingh.summer.DisplayActivity.convertPDFToText(DisplayActivity.java:112)
at com.example.prachisingh.summer.DisplayActivity.onCreate(DisplayActivity.java:70)
at android.app.Activity.performCreate(Activity.java:6259)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1130)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2379)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2490)
at android.app.ActivityThread.-wrap11(ActivityThread.java)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1354)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:148)
at android.app.ActivityThread.main(ActivityThread.java:5443)
at java.lang.reflect.Method.invoke(Native Method)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:728)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:618) resource.I need help figuring out the cause of this error.
Here is my AndroidMAnifest.xml file:
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.Computer.app_name">
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity
android:name=".MainActivity"
android:label="@string/app_name"
android:theme="@style/AppTheme.NoActionBar">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
<activity
android:name=".DisplayActivity" >
</activity>
<meta-data
android:name="com.google.android.gms.version"
android:value="@integer/google_play_services_version" />
</application>