how to display doc, docx, pdf, xls, txt from storage in listview in Android

how to open pdf file in android programmatically
how to open pdf file from internal storage in android programmatically
display pdf file inside my android application
android pdf viewer from url
how to open local pdf file in webview android programmatically
how to upload pdf file in android programmatically
how to load pdf url in webview android
how to open pdf file from url in android programmatically

hi guys I'm trying to display all doc , docx, pdf, xls, txt in a listview from my phone. so for starters, I tried this to implement on my app but somehow im having an error on this line ContentResolver cr = context.getContentResolver();

edit: thanks for answering CommonsWare. now im having a problem with this line newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));

and this is my code

 Uri uri = MediaStore.Files.getContentUri("external");

    String[] projection ={ MediaStore.Files.FileColumns._ID,
            MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE} ;

    String selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + "=?";
    String mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension("pdf");
    String[] selectionArgsPdf = new String[]{ mimeType };
    String sortOrder = null;

    Cursor allPdfFiles = getContentResolver().query(uri, projection, selectionMimeType, selectionArgsPdf, sortOrder);


    if (allPdfFiles.moveToFirst()) {
        do {

            ImageViewInfo newVVI = new ImageViewInfo();
            int id = allPdfFiles.getInt(allPdfFiles.getColumnIndex(MediaStore.Files.FileColumns._ID));


            newVVI.filePath = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
            newVVI.title = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE));
            newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));
            ImageRows.add(newVVI);
        } while (cursor.moveToNext());


        allPdfFiles.close(); }

Am I doing it right? please help me.

and how can I query multiple specified files like docs,docx,xls,txt? so that i can list it together with pdf.

thank you in Advance!

This syntax incorrect as you are trying to get the returned column as listed in your projection. Make sure that your projection lists the columns you want. Think of it as a spreadsheet where each column represents another attribute of the data. Your do while loop then effectively works down each row, whilst you ask for each element of that row.

incorrect:

  allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(Medi‌​aStore.Files.FileCol‌​umns.MIME_TYPE + "=?"));

correct:

       String[] projection ={ MediaStore.Files.FileColumns._ID,
        MediaStore.Files.FileColumns.DATA, MediaStore.Files.FileColumns.TITLE, MediaStore.Files.FileColumns.MIME_TYPE} ;

and

  newVVI.filePath = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.DATA));
        newVVI.title = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.TITLE));
        newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE));

How to open a PDF file in Android programmatically?, This syntax incorrect as you are trying to get the returned column as listed in your projection. Make sure that your projection lists the columns you want. Think of it  Word Office - Docs Reader, Document, XLSX, PPTX is an effective and professional word processor in Office Suite Office Word – Word Viewer and PDF, XLS, PPT let you easily view, reader, and create Word, Excel, and Powerpoint documents, and perform advanced Docs Sheet and Slide operations.

Context context = null;

Your first line sets context to null.

ContentResolver cr = context.getContentResolver();

Your second line results in a NullPointerException, as you are trying to call getContentResolver() on a null Context.

You need an actual Context, such as your Activity or Service.

Upload PDF File to Server in Android with PHP and MySQL, hi guys I'm trying to display all doc , docx, pdf, xls, txt in a listview from my phone. so for starters, I tried this to implement on my app but somehow im having an  Word Office - Docs Reader, Document, XLSX, PPTX is an effective and professional word processor in Office Suite Office Word – Word Viewer and PDF, XLS, PPT let you easily view, reader, and create Word, Excel, and Powerpoint documents, and perform advanced Docs Sheet and Slide operations. With Word Office, whether you are sitting in the office, on the go or wherever you are, you always have a

Thank you for your answer! I got a working solution by your code and have solved problem!

It works:

private static final String orderBy = MediaStore.Files.FileColumns.DATE_ADDED + " DESC";

ContentResolver contentResolver = getContentResolver();
Uri uri = MediaStore.Files.getContentUri("external");

final String[] columns = {
                    MediaStore.Images.Media.DATA, MediaStore.Images.Media._ID,
                    MediaStore.Images.Media.DATE_ADDED, MediaStore.Images.Media.TITLE,
                    MediaStore.Images.Media.SIZE, MediaStore.Images.Media.DISPLAY_NAME,
                    MediaStore.Images.Media.MIME_TYPE};

        List<String> extensions = new ArrayList<>();
        extensions.add("pdf");
        extensions.add("csv");
        extensions.add("doc");
        extensions.add("docx");
        extensions.add("xls");
        extensions.add("xlsx");

List<String> mimes = new ArrayList<>();
        for (String ext : extensions) {
            mimes.add(MimeTypeMap.getSingleton().getMimeTypeFromExtension(ext));
        }

Cursor cursor;
        cursor = contentResolver.query(uri, columns, null, null, orderBy);
        if (cursor != null) {
            int mimeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE);
            int pathColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);

            while (cursor.moveToNext()) {
                String mimeType = cursor.getString(mimeColumnIndex);
                String filePath = cursor.getString(pathColumnIndex);

                if (mimeType != null && mimes.contains(mimeType)) {
                    // handle cursor
                    makeFile(cursor);
                } else {
                    // need to check extension, because the Mime Type is null
                    String extension = getExtensionByPath(filePath);
                    if (extensions.contains(extension)) {
                        // handle cursor
                        makeFile(cursor);
                    }
                }
            }
            cursor.close();
        }

public static String getExtensionByPath(@NonNull String path) {
        String result = "%20";
        int i = path.lastIndexOf('.');
        if (i > 0) {
            result = path.substring(i + 1);
        }
        return result;
    }

private MyCustomFile makeFile(Cursor cursor) {
        int mimeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.MIME_TYPE);
        int pathColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DATA);
        int sizeColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.SIZE);
        int titleColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.TITLE);
        int nameColumnIndex = cursor.getColumnIndex(MediaStore.Images.Media.DISPLAY_NAME);

        int fileId = cursor.getInt(pathColumnIndex);
        String fileSize = cursor.getString(sizeColumnIndex);
        String fileDisplayName = cursor.getString(nameColumnIndex);
        String fileTitle = cursor.getString(titleColumnIndex);
        String filePath = cursor.getString(pathColumnIndex);
        String mimeType = cursor.getString(mimeColumnIndex);
        String type = MimeTypeMap.getSingleton().getExtensionFromMimeType(mimeType);
        if (type == null) {
            type = FileUtils.getExtensionByPath(filePath);
            mimeType = MimeTypeMap.getSingleton().getMimeTypeFromExtension(type);
        }

        MyCustomFile result = new MyCustomFile();
        result.setFileId(fileId);
        result.setFilePath(filePath);
        result.setFileSize(fileSize);
        result.setFileTitle(fileTitle);
        result.setFileDisplayName(fileDisplayName);
        result.setMimeType(mimeType);
        result.setFileExtension(type);

        return result;
    }

Hope it help someone as me =)

Firebase Storage, If you want to display PDF in your Android application, there are various Opening a PDF file form storage using AndroidPdfViewer library; Opening a loadUrl("https://docs.google.com/gview?embedded=true&url=$url") } }. Simple way to convert doc/docx file into pdf file, using kingsoft office. How to create Data Entry Form in Excel - Ms Office? - Duration: 5:06. How to convert PDF to Word on Android | PDF

I have created a query like this and it's working like a charm

val mimeTypes = mutableListOf<String>()
val extenstions = mutableListOf("pdf","doc","odt")
extenstions.forEach { mimeType ->
     MimeTypeMap
         .getSingleton()
         .getMimeTypeFromExtension(mimeType)?.let {
             mimeTypes.add("'$it'")
         }
}

val selectionMimeType = MediaStore.Files.FileColumns.MIME_TYPE + " IN (${mimeTypes.joinToString()})"
context.contentResolver.query(
     MediaStore.Files.getContentUri("external"),
     projection,
     selectionMimeType,
     null,
     MediaStore.Video.Media.DATE_ADDED
)

How to open pdf file in android programmatically, In this tutorial we will learn how to upload pdf file to server in android. files of different extensions such as .pdf, .docx, .xls and so on and use them as is required for this project as it involves reading the android storage. we need a button for the same and also a listview to display the list of pdfs fetched. For further details, please check Convert word file to PDF Furthermore, it supports variety of document formats i.e. DOC, DOCX, ODT, RTF, EPUB etc. Please check Supported file formats for more information. "Aspose.Cells.zip" offers the feature to convert Excel worksheets to PDF. For further details, please visit Convert Excel files to PDf

Read text file from assets folder in Android Studio, list view display the files names in blurry way… why i'am facing this problem>??? Reply. Sonu says. Smart office reader - Office viewer, PDF reader free for Android is a free office document viewer for your android cell phone and tablet devices. Smart office reader - Office viewer, PDF reader includes parts: Word reader, Excel reader and PowerPoint reader and Pdf reader. With All document reader , you can read all file documents in .doc, .docx, .txt, .ppt, .xls and .xlsx files formats and

flutter_filereader, Programmatically convert MS Word DOC and DOCX files to PDF in ASP. to downloading a pdf file and viewing it using an Android APP. txt"); Or you can to generate XLS and XLSX Excel files programmatically from any ASP. doc, . We can use android device monitor to browse the android internal storage directory. A tiny Docx Viewer app is ready for you to read any docx file on the go. This docx viewer is a clever HD quality word reader which can easily view all images, tables, different font styles, bullets and more for any docx files in a responsive display for mobiles as well as tablets. Its not just a basic docx reader it can do more. ** Compatible with almost all Android OS versions ** Added

Offline Document Viewer :Pdf reader & Free Download, all you mobile or storage documents separately like pdf documents, word documents, This app is a document reader to read .doc, .txt, .ppt, .xls and .pdf files. office co workers .this app will read all kind of .doc and .docx files in your android phone of your mobile like pdf, ppt, xls, docs and txt files in a grid or list view. I don' have MS office, only notepad which saves my files as .txt files. Ineed to convert them to .doc or .docx . how can i do this without purching office? Thanks for any help available !! See Bruce Hagen's reply, and let me add the following: Notepad is not really a word processing program; it's nothing more

Comments
  • Do not edit your question to replace it with a different question. Use the "Ask Question" button to ask a different question.
  • sorry for that, well technically i just updated my post and there still some question that is not answered. but thanks bro I'll keep that in mind next time :)
  • Thanks! this works for me, thanks for correcting my syntax :)
  • do you know how can i query other file extensions like docx, pptx, xlsx?
  • try mime type "text/*"
  • thanks for answering! I've edited my post and now I'm having an error in this line newVVI.mimeType = allPdfFiles.getString(allPdfFiles.getColumnIndexOrThrow(MediaStore.Files.FileColumns.MIME_TYPE + "=?"));