Load PDF files

Introduction:
   This example shows you how to show list of all pdf file saved on your sdcard and internal storage into listview and then open the selected pdf file to view via pdfviewer in android default pdf viewer.

demo:



Using this code

activity_main.xml

<RelativeLayout 
 xmlns:android="http://schemas.android.com/apk/res/android"
 xmlns:tools="http://schemas.android.com/tools"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:background="#9EBA89" >

<ListView
 android:id="@+id/listView1"
 android:layout_width="match_parent"
 android:layout_height="wrap_content" >
</ListView>

</RelativeLayout>



MainActivity.java

package com.example.readpdfnames;

import java.io.File;
import java.util.ArrayList;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ArrayAdapter;
import android.widget.ListView;

public class MainActivity extends Activity {

File path;
ListView list;
static ArrayList<String> pdf_paths=new ArrayList<String>();
static ArrayList<String> pdf_names=new ArrayList<String>();

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

list=(ListView)findViewById(R.id.listView1);
pdf_paths.clear();
pdf_names.clear();


//Access External storage 
path = new File(Environment.getExternalStorageDirectory() + "");
searchFolderRecursive1(path);

ArrayAdapter<String> adapter = new ArrayAdapter<String>(this,
android.R.layout.simple_list_item_1, android.R.id.text1pdf_names);
Log.e("aaaaaaaaaa"""+pdf_names);

list.setAdapter(adapter);
list.setOnItemClickListener(new OnItemClickListener() {

@Override
public void onItemClick(AdapterView<?> arg0, View arg1, int arg2,
long arg3) {
// TODO Auto-generated method stub
String path = pdf_paths.get(arg2);
File file = new File(path);
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.fromFile(file), "application/pdf");
intent.setFlags(Intent.FLAG_ACTIVITY_NO_HISTORY);
startActivity(intent);
}
});
}
private static void searchFolderRecursive1(File folder)
{
 if (folder != null)
 {
  if (folder.listFiles() != null)
  {
   for (File file : folder.listFiles())
   {
    if (file.isFile())
    {
     //.pdf files 
     if(file.getName().contains(".pdf"))
      {
       Log.e("ooooooooooooo""path__="+file.getName());
       file.getPath();
       pdf_names.add(file.getName());
       pdf_paths.add(file.getPath());
       Log.e("pdf_paths"""+pdf_names);
      }
     } 
     else 
     {
      searchFolderRecursive1(file);
     }
    }
   }
  }
 }
}

Don't forget to add below permissions into Androidmanifest.xml

<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />



 
Happy coding...

Previous
Next Post »

10 comments

Write comments
Unknown
AUTHOR
January 6, 2016 at 12:14 PM delete

this post is really helpful..at last i found your code working after searching 2 days in google.thanx a lot

Reply
avatar
Mukesh Vijay
AUTHOR
March 22, 2016 at 1:02 PM delete

Thank you...To the point

Reply
avatar
Rrahul Jangid
AUTHOR
July 14, 2016 at 2:29 PM delete

great ...awesome,,,,after 3 days searching it is best example ....

Reply
avatar
Unknown
AUTHOR
December 30, 2016 at 11:23 PM delete

Thank you KALIDOSS RAJENDRAN
i must tell you that i was desprate for searching this piece of code thank you buddy you are awsome!!!!

Reply
avatar
Unknown
AUTHOR
October 4, 2017 at 7:23 PM delete

Thanks a lot but unfortunately mine doesn't show any pdf files yet i got several pdf files in both the internal storage and sdcard.

also i don't understand this statement below the code that says "Don't forget to add below permissions into Androidmanifest.xml" which permissions should i add into the manifest.xml?

thanks. pls help

Reply
avatar
Unknown
AUTHOR
January 20, 2018 at 6:33 AM delete

My phone could not display any pdf file?

Reply
avatar
Unknown
AUTHOR
January 20, 2018 at 6:36 AM delete

Even I have the same problem

Reply
avatar
Unknown
AUTHOR
March 6, 2018 at 5:24 PM delete

Give permission to youe app from setting

Reply
avatar
Unknown
AUTHOR
March 6, 2018 at 5:25 PM delete

give permission to your app through setting

Reply
avatar