2

I have a requirement to display pdf file from SD card. This pdf file is created after a network operation. I'm getting ActivityNotFoundException when trying to open pdf through activity.startActivity(intent); . Below is code folder_list_details.xml

  <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout
xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent">
 <ListView 
    android:layout_height="wrap_content"
    android:layout_width="fill_parent" android:id="@+id/listV_main"/>
<ImageView 
    android:id="@+id/photo"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:paddingRight="15dp"
    android:paddingTop="10dp" />
        <TextView android:id="@+id/name"
    android:textSize="14sp" 
    android:textStyle="bold" 
    android:textColor="#000000" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content"
    android:layout_toRightOf="@id/photo"/>

      </RelativeLayout>

FolderActivity.java

    public class FolderListActivity extends ListActivity{

public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    Log.i("FolderListActivity", "onCreate" );
    FolderDisplay folderObj = new FolderDisplay(FolderListActivity.this);
    folderObj.execute((Void) null);
     }
    } 

FolderDisplay.java :

     @Override
       protected void onPostExecute(Void v) {
    try{
                //activity is an  instance of FolderListActivity
        final ListView lv1 =activity.getListView(); 
        lv1.setOnItemClickListener(new OnItemClickListener() {
            @Override
      public void onItemClick(AdapterView<?> arg0, View v, int position,long id) {                           
            File file =Utility.getInstance().getFile();
                Intent intent = new Intent(Intent.ACTION_VIEW);
                 intent.setDataAndType(Uri.fromFile(file), "application/pdf");
                 intent.setFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
                  activity.startActivity(intent);
            }  
        });
    }catch(Exception e){
        }
 }

Mainfest.xml

                  <?xml version="1.0" encoding="utf-8"?>
             <manifest xmlns:android="http://schemas.android.com/apk/res/android"
           package="com.example.testlogin"
           android:versionCode="1"
             android:versionName="1.0" >

      <uses-sdk
       android:minSdkVersion="11"
     android:targetSdkVersion="17" />
       <uses-permission android:name="android.permission.INTERNET"/>
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE">
       </uses-  permission>

      <application
    android:allowBackup="true"
    android:icon="@drawable/ic_launcher"
    android:label="@string/app_name"
    android:theme="@style/AppTheme" 
    android:debuggable="true"
    >
    <activity
        android:name="com.example.testlogin.LoginActivity"
        android:label="@string/app_name"
        android:windowSoftInputMode="adjustResize|stateVisible" >
        <intent-filter>
            <action android:name="android.intent.action.MAIN" />

            <category android:name="android.intent.category.LAUNCHER" />
        </intent-filter>
      </activity>
        <activity
        android:name="com.example.testlogin.RepoListActivity"
        android:label="@string/title_activity_repo_list" >
       </activity>
       <activity
        android:name="com.example.testlogin.FolderListActivity"
        android:label="@string/title_activity_folder_list" >
      </activity>
     </application>

     </manifest>

Error I'm getting

android.content.ActivityNotFoundException: No Activity found to handle Intent { act=android.intent.action.VIEW dat=file:///mnt/sdcard/Alfdata/testdata5.pdf typ=application/pdf flg=0x4000000 }
onkar
  • 4,427
  • 10
  • 52
  • 89
Sam
  • 244
  • 2
  • 5
  • 20

3 Answers3

2

That is because your device or emulator does not have an application capable of viewing a local PDF file.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491
  • I'm getting the same error while opening text file as well. Any idea? – Sam Jan 31 '13 at 01:39
  • @Sam: That is because your device or emulator does not have an application capable of viewing a text file. – CommonsWare Jan 31 '13 at 01:43
  • thanks for quick response! Can you please let me know what I need to do to resolve this issue? – Sam Jan 31 '13 at 03:42
  • 1
    @Sam: Install apps on your device that can view PDFs and/or text files. – CommonsWare Jan 31 '13 at 03:49
  • Any suggestion what apps I need to install? – Sam Jan 31 '13 at 03:53
  • 2
    @Sam: You are welcome to browse [the Play Store](http://play.google.com) to find PDF viewers, text file viewers, etc. for your device. Most that advertise that they can view those types of files will support `ACTION_VIEW`. – CommonsWare Jan 31 '13 at 03:55
  • I'll try and let you know, Thanks for your suggestion – Sam Jan 31 '13 at 04:09
  • Now, I'm able to view text file after changing code to - String extension = android.webkit.MimeTypeMap.getFileExtensionFromUrl(Uri.fromFile(file).toString()); String mimetype = android.webkit.MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); intent.setDataAndType(Uri.fromFile(file), mimetype); However, still I'm not able to open pdf and JPEG file – Sam Jan 31 '13 at 05:31
  • @CommonsWare am facing the same error when am trying to open udp video. With http its working fine. And am opening my video on real device only instead am facing error.Any idea? – AndroidOptimist Oct 17 '13 at 09:23
0

I faced the same problem apparently , Android does not have a stock PDF Viewer but as an alternative if you have the pdf up on the cloud then u can open it in a webView which opens up a google docs link pointing to your pdf url .

Look at this post here

Community
  • 1
  • 1
Chris
  • 798
  • 1
  • 9
  • 15
0

Whenever you start an intent, you would have to have the native app installed on the emulator to handle that intent. Ex. If you invoke an intent with Maps as the action, you would have to use the Google API's based emulator. By default, android emulator does not have a PDF reader. You could test this on a device with a PDF reader and it should work fine.

Pls mark the answer as correct if you think your issue has been resolved.

lokoko
  • 5,785
  • 5
  • 35
  • 68