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: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.text1, pdf_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
//.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
{
else
{
searchFolderRecursive1(file);
}
}
}
}
}
}
Don't forget to add below permissions into Androidmanifest.xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE" />
Happy coding...
10 comments
Write commentsthis post is really helpful..at last i found your code working after searching 2 days in google.thanx a lot
Replywelcome
ReplyThank you...To the point
Replygreat ...awesome,,,,after 3 days searching it is best example ....
ReplyThank you KALIDOSS RAJENDRAN
Replyi must tell you that i was desprate for searching this piece of code thank you buddy you are awsome!!!!
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.
Replyalso 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
My phone could not display any pdf file?
ReplyEven I have the same problem
ReplyGive permission to youe app from setting
Replygive permission to your app through setting
ReplyEmoticonEmoticon