2

I am trying to create a project in which I need my application to read locally store pdf, doc, xls files.

I don't have any idea on how to do it. I want to show all that locally stored files in my webview. Is there any library to render this files or any other way to do it? If any one can guide me on it than it will be my pleasure.

Widor
  • 13,003
  • 7
  • 42
  • 64
Girija Patil
  • 430
  • 1
  • 5
  • 16
  • Please research on the topic first and state where exactly you are stuck.... – GAMA Jun 13 '12 at 07:07
  • I m able to read pdf,doc,xl using third party app(Kingsoft office)..but i want to read these file on webview.for this any third party jar will be required that suggest me. – Girija Patil Jun 13 '12 at 07:14
  • refer http://stackoverflow.com/questions/3831019/how-to-read-a-pdf-in-android – GAMA Jun 13 '12 at 07:16
  • also http://stackoverflow.com/questions/6489376/how-to-read-doc-file, http://stackoverflow.com/questions/9974468/how-to-read-doc-docx-xls-files-in-android , http://stackoverflow.com/questions/7790503/how-to-read-write-doc-and-excel-file-in-android – GAMA Jun 13 '12 at 07:18
  • Have a look at this http://stackoverflow.com/questions/7790503/how-to-read-write-doc-and-excel-file-in-android – Aamirkhan Jun 13 '12 at 07:20

1 Answers1

0
package com.cdn.file;

import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.ListAdapter;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

public class FileExplorerActivity extends Activity {
    /** Called when the activity is first created. */
    private Button buttonList, buttonBack;
    // Stores names of traversed directories
    ArrayList<String> str = new ArrayList<String>();
    // Check if the first level of the directory structure is the one showing

    private static final String TAG = "F_PATH";
    private List<Item> fileList;
    private Item item;
    private File path = new File(Environment.getExternalStorageDirectory() + "");
    private String chosenFile = "";

    ListView listViewDir;
    ListAdapter adapter;
    ListFile listFileAdaptor;
    int value = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
        listViewDir = (ListView) findViewById(R.id.listViewDir);

        buttonBack = (Button) findViewById(R.id.buttonBack);
        buttonBack.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {


            }
        });

        buttonList = (Button) findViewById(R.id.buttonList);
        buttonList.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View v) {

                if (isSdPresent()) {
                    Toast.makeText(FileExplorerActivity.this, "Un Mounted",
                            Toast.LENGTH_SHORT).show();
                    loadFileList();
                    listFileAdaptor = new ListFile(FileExplorerActivity.this,
                            fileList);
                    listViewDir.setAdapter(listFileAdaptor);


                }

                else {
                    Toast.makeText(FileExplorerActivity.this, "Mounted",
                            Toast.LENGTH_SHORT).show();
                }

            }

        });

        listViewDir.setOnItemClickListener(new OnItemClickListener() {

            @Override
            public void onItemClick(AdapterView<?> parent, View view,
                    int position, long id) {
            //  if (value == 0) {
                    chosenFile = fileList.get(position).getFile();
                /*} else {
                    chosenFile = backList.get(position).getFile();
                    value =0;
                }*/
                File sel = new File(path + "/" + chosenFile);

                Log.v("Prashant111 ==", sel.toString());
                Log.v("Prashant222 ==", "" + sel.toURI());

                if (sel.isDirectory()) {

                    // Adds chosen directory to list
                    str.add(chosenFile);

                    path = new File(sel + "");

                    loadFileList();
                    listFileAdaptor = new ListFile(FileExplorerActivity.this,
                            fileList);
                    listViewDir.setAdapter(listFileAdaptor);


                    // Log.d(TAG, path.getAbsolutePath());

                    Log.d("Prashant333 ==", "" + path.toURI());

                    Log.d("Prashant444 ==", "" + path.toString());

                }

                // Checks if 'up' was clicked

                // File picked
                else {
                    Intent intent = new Intent(Intent.ACTION_VIEW);
                    Uri uri = Uri.fromFile(new File(chosenFile));
                    intent.putExtra(Intent.ACTION_VIEW, uri);
                    Intent chooser = Intent.createChooser(intent, "Prashant");
                    startActivity(chooser);

                }

            }
        });
    }

    public static boolean isSdPresent() {
        return android.os.Environment.getExternalStorageState().equals(
                android.os.Environment.MEDIA_MOUNTED);

    }

    private class Item {
        public String file;
        public int icon;

        public String getFile() {
            return file;
        }

        public void setFile(String file) {
            this.file = file;
        }

        public int getIcon() {
            return icon;
        }

        public void setIcon(int icon) {
            this.icon = icon;
        }

    }

    private void loadFileList() {
        try {
            path.mkdirs();
        } catch (SecurityException e) {
            Log.e(TAG, "unable to write on the sd card ");
        }

        // Checks whether path exists
        if (path.exists()) {
            FilenameFilter filter = new FilenameFilter() {
                @Override
                public boolean accept(File dir, String filename) {
                    File sel = new File(dir, filename);
                    // Filters based on whether the file is hidden or not
                    return (sel.isFile() || sel.isDirectory())
                            && !sel.isHidden();

                }
            };

            String[] fList = path.list(filter);
            fileList = new ArrayList<FileExplorerActivity.Item>();

            for (int i = 0; i < fList.length; i++) {
                item = new Item();
                item.setIcon(R.drawable.file_icon);
                item.setFile(fList[i]);
                // fileList.add(item);
                // Convert into file path
                File sel = new File(path, fList[i]);

                Log.v("sel ====", sel.toString());

                String name = sel.getName();

                // Set drawables
                if (sel.isDirectory()) {
                    item.setFile(name);
                    item.setIcon(R.drawable.directory_icon);
                    fileList.add(item);

                } else {
                    if (name.endsWith(".mp4") || name.endsWith(".3gp")) {
                        Log.v("aaaaaaaaaaa", name);
                        item.setFile(name);
                        item.setIcon(R.drawable.icon);
                        fileList.add(item);
                    } else {

                        fileList.remove(item);
                    }
                }
            }

        } else {
            Log.e(TAG, "path does not exist");
        }

    }

    class ViewHolder {
        TextView textViewName;
        ImageView view;
    }

    class ListFile extends BaseAdapter {
        List<Item> fileList;
        private LayoutInflater inflator;
        Context context;

        ListFile(Context context, List<Item> fileList) {
            this.fileList = fileList;
            this.context = context;
            inflator = (LayoutInflater) context
                    .getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        }

        @Override
        public int getCount() {

            return fileList.size();
        }

        @Override
        public Object getItem(int position) {

            return fileList.get(position);
        }

        @Override
        public long getItemId(int position) {

            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView == null) {
                if (getItemViewType(position) == 0) {
                    convertView = inflator.inflate(R.layout.file_list, null);
                }

                holder = new ViewHolder();

                holder.textViewName = (TextView) convertView
                        .findViewById(R.id.textView1);
                holder.view = (ImageView) convertView
                        .findViewById(R.id.imageView1);

                convertView.setTag(holder);
                // Set the display text
            } else {
                holder = (ViewHolder) convertView.getTag();
            }
            holder.textViewName.setText(fileList.get(position).getFile());

            holder.view.setImageResource((fileList.get(position).getIcon()));

            return convertView;
        }
    }
}

Try using this code. I am taking video files but you can specify the type of files that you want.

Harry Joy
  • 58,650
  • 30
  • 162
  • 207
Prashant Mishra
  • 627
  • 5
  • 18