Walid Sassi
Walid Sassi

Reputation: 75

open pdf file from asset

good morning i used a code to open pdf file that exist in asset folder but the app crash every time with this message unable to start activity Pdfreader ActivityNoFoundExeption NoActivity found to handle intent this the code

package com.example.albir;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.AssetManager;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;

public class Pdfreader extends Activity {
     @Override
     protected void onCreate(Bundle savedInstanceState)
     {
         super.onCreate(savedInstanceState);
         setContentView(R.layout.pdf);
         CopyReadAssets();

     }

     @SuppressLint("WorldReadableFiles")
    @SuppressWarnings("deprecation")
    private void CopyReadAssets()
     {
         AssetManager assetManager = getAssets();

         InputStream in = null;
         OutputStream out = null;
         File file = new File(getFilesDir(), "beralahsa001.pdf");
         try
         {
             in = assetManager.open("beralahsa001.pdf");
             out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);

             copyFile(in, out);
             in.close();
             in = null;
             out.flush();
             out.close();
             out = null;
         } catch (Exception e)
         {
             Log.e("tag", e.getMessage());
         }

         Intent intent = new Intent(Intent.ACTION_VIEW);
         intent.setDataAndType(
                 Uri.parse("file://" + getFilesDir() + "/beralahsa001.pdf"),
                 "application/pdf");

         startActivity(intent);
     }

     private void copyFile(InputStream in, OutputStream out) throws IOException
     {
         byte[] buffer = new byte[1024];
         int read;
         while ((read = in.read(buffer)) != -1)
         {
             out.write(buffer, 0, read);
         }
     }

}

this is the manifest

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

    <uses-sdk
        android:minSdkVersion="8"
        android:targetSdkVersion="18" />
     <uses-permission android:name="android.permission.INTERNET"/>  
     <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
    <application
        android:allowBackup="true"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/AppTheme" >
        <activity
            android:name="com.example.albir.Menu"
            android:label="@string/app_name" >
            <intent-filter>
                <action android:name="android.intent.action.MAIN" />

                <category android:name="android.intent.category.LAUNCHER" />
            </intent-filter>
        </activity>
        <activity android:name=".ShowDetails"></activity>
        <activity android:name=".MainActivity"></activity>
        <activity android:name=".Pdfreader"></activity>

    </application>

</manifest>

can you please help me

Upvotes: 0

Views: 1469

Answers (2)

bagher
bagher

Reputation: 7

Try it..

AssetManager assetManager = getAssets();


InputStream in = null;
OutputStream out = null;
File file = new File(getFilesDir(), "pdfname.pdf");
try
{
in = assetManager.open("pdfname.pdf");
out = openFileOutput(file.getName(), Context.MODE_WORLD_READABLE);


copyFile(in, out);
in.close();
in = null;
out.flush();
out.close();
out = null;
} catch (Exception e)
{
Toast.makeText(getApplicationContext(), "no pdf viewer", 2000).show();
finish();
}
try{
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(
Uri.parse("file://" + getFilesDir() + "/pdfname.pdf"),
"application/pdf");


startActivity(intent);
finish();
}
catch (Exception e)
{
Toast.makeText(getApplicationContext(), "no pdf viewer", 2000).show();
finish();
}
}


private void copyFile(InputStream in, OutputStream out) throws IOException
{
byte[] buffer = new byte[1024];
int read;
while ((read = in.read(buffer)) != -1)
{
out.write(buffer, 0, read);
}
}

Upvotes: 1

OrhanC1
OrhanC1

Reputation: 1410

It seems as the intent resolver can't find anything that can handle PDFs. Try installing a PDF viewer on your device.

Upvotes: 1

Related Questions