应用程序列出来自SD卡的活动中的Pdf

时间:2015-09-26 20:34:35

标签: java android pdf

我正在开发一个Android应用程序,它需要一个列表活动来显示来自sdcard..i的所有pdf,发现下面的代码并使用它,但它不起作用我的应用程序很遗憾关闭..谁能告诉我什么是错误。

MainActivity.java

package com.example.vinita.listallpdfs;

import android.app.ListActivity;
import android.os.Bundle;
import android.os.Environment;
import android.view.Menu;
import android.view.MenuItem;
import android.widget.ArrayAdapter;
import java.io.File;
import java.io.FilenameFilter;
import java.util.ArrayList;
import java.util.Arrays;

import static com.example.vinita.listallpdfs.R.layout.activity_main;


public class MainActivity extends ListActivity {
    ArrayAdapter adapter;
    int clickCounter=0;
    ArrayList listItems=new ArrayList();
    private File[] imagelist;
    String[] pdflist;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(activity_main);
        File images = Environment.getExternalStorageDirectory();
        //ListView list=(ListView)findViewById(R.id.list);
        imagelist = images.listFiles(new FilenameFilter() {
            @Override
            public boolean accept(File dir, String name) {
                return ((name.endsWith(".pdf")));
            }
        });
        pdflist = new String[imagelist.length];
        for (int i = 0; i < imagelist.length; i++) {
            pdflist[i] = imagelist[i].getName();
        }
        Arrays.sort(imagelist);
        Arrays.sort(pdflist);
        this.setListAdapter(new ArrayAdapter(this, android.R.layout.simple_list_item_1, pdflist));
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.menu_main, menu);
        return true;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        // Handle action bar item clicks here. The action bar will
        // automatically handle clicks on the Home/Up button, so long
        // as you specify a parent activity in AndroidManifest.xml.
        int id = item.getItemId();

        //noinspection SimplifiableIfStatement
        if (id == R.id.action_settings) {
            return true;
        }

        return super.onOptionsItemSelected(item);
    }
}

activity_main.xml中

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:paddingLeft="@dimen/activity_horizontal_margin"
    android:paddingRight="@dimen/activity_horizontal_margin"
    android:paddingTop="@dimen/activity_vertical_margin"
    android:paddingBottom="@dimen/activity_vertical_margin" tools:context=".MainActivity"
    android:background="#ff3db0ff"
    android:orientation="vertical">


    <ListView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:id="@android:id/list"
        android:layout_centerVertical="true"
        android:layout_centerHorizontal="true"
        android:scrollbars="vertical"/>
</RelativeLayout>

1 个答案:

答案 0 :(得分:0)

将此权限添加到清单文件

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

希望它有所帮助:D