android


Want to List PDF files from Download Folder. Getting Error


I want to list PDF files from Downloads folder, but I am getting error of null point while getting files. I am providing my code below. For displaying pdf file I am using com.github.barteksc:android-pdf-viewer:1.4.0' library.
package com.tutorials.hp.gridviewpdf;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.support.design.widget.FloatingActionButton;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.widget.Toolbar;
import android.view.View;
import android.widget.GridView;
import java.io.File;
import java.util.ArrayList;
public class MainActivity extends AppCompatActivity {
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
Toolbar toolbar = (Toolbar) findViewById(R.id.toolbar);
setSupportActionBar(toolbar);
final GridView gv= (GridView) findViewById(R.id.gv);
FloatingActionButton fab = (FloatingActionButton) findViewById(R.id.fab);
fab.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View view) {
gv.setAdapter(new CustomAdapter(MainActivity.this,getPDFs()));
}
});
}
private ArrayList<PDFDoc> getPDFs()
{
ArrayList<PDFDoc> pdfDocs=new ArrayList<>();
//TARGET FOLDER
File downloadsFolder= Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);
PDFDoc pdfDoc;
if(downloadsFolder.exists())
{
//GET ALL FILES IN DOWNLOAD FOLDER
File[] files=downloadsFolder.listFiles();
//LOOP THRU THOSE FILES GETTING NAME AND URI
for (int i=0;i<files.length;i++)
{
File file=files[i];
if(file.getPath().endsWith("pdf"))
{
pdfDoc=new PDFDoc();
pdfDoc.setName(file.getName());
pdfDoc.setPath(file.getAbsolutePath());
pdfDocs.add(pdfDoc);
}
}
}
return pdfDocs;
}
}
Here is my logcat

Related Links

Why cache Dao inside DatabaseHelper, if its cached in DaoManage
Android Kiosk Mode app. Block Notification bar and recent apps button
Populating Android Spinner with List of Objects
ListView not populating via Array Adapter
unable to startActivity master-detail from another activity
NumberPicker textColour
Change keyboard language in android programitically
Android apk taking gson.jar as a native platform
Android App for Tablet
Exception with Android WebView
Android: Binding dates to listview alongside other data
Updated collided sprite only
Get Public tweets Twitter API 1.1 [closed]
Space in SQLite query [closed]
Android: Clear Activity Stack [duplicate]
How to Set monogram to textview — android

Categories

HOME
url-redirection
spark-streaming
jax-rs
pyqt
swt
mstest
socket.io
bitbucket-api
javacc
tail
tvos
barcode
drop-down-menu
endeca
intercom
frequency-distribution
glyphicons
jersey-1.0
shortest-path
sar
rubymotion
short-url
comparator
draft-js-plugins
definitelytyped
jquery-multiselect
angular-translate
rpgle
jaws
sqlpackage
dst
complex-numbers
papaparse
reverse
mongoose-schema
hackintosh
movilizer
congestion-control
knime
xmldocument
phpdbg
pom.xml
truezip
er-diagram
swingworker
structuremap4
filenet
adafruit
neon
mta
shopping
pushbots
nio2
invoke-sqlcmd
directdraw
toran-proxy
decimalformat
pypiserver
page.js
diff3
structuremap3
mdichild
fpdi
cfcache
away3d
mongohq
beaker-testing
observium
jquery-1.9
sentestingkit
google-maps-mobile
promotion-code
appledoc
glui
interop-domino
asynchronous-wcf-call
java1.4
movieplayer
commercial-application

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App