Android 如何读取大于1mb的文件

Android 如何读取大于1mb的文件,android,file,Android,File,我已经创建了一个应用程序,其中我正在从手机内存中读取文件。它可以读取任何文件,但当我正在读取.vcf文件时。但当它小于1 mb时,请给出正确的结果,如果它大于1 mb,则不会返回任何内容。我如何从文件中读取数据。请提出建议 文件\u浏览 public class File_Explorer extends Activity { // Stores names of traversed directories ArrayList<String> str = new A

我已经创建了一个应用程序,其中我正在从手机内存中读取文件。它可以读取任何文件,但当我正在读取.vcf文件时。但当它小于1 mb时,请给出正确的结果,如果它大于1 mb,则不会返回任何内容。我如何从文件中读取数据。请提出建议

文件\u浏览

public class File_Explorer extends Activity {

    // 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 Boolean firstLvl = true;
     String aDataRow = "";  
     static StringBuilder aBuffer = new StringBuilder();
     String aBuffer1="";
    private static final String TAG = "F_PATH";

    private Item[] fileList;
    private File path = new File(Environment.getExternalStorageDirectory().getAbsolutePath());
    private String chosenFile;
    private static final int DIALOG_LOAD_FILE = 0;
    static String fileExtension="";
    ListAdapter adapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);

        loadFileList();

        showDialog(DIALOG_LOAD_FILE);
        Log.d(TAG, path.getAbsolutePath());

    }

    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 Item[fList.length];
            for (int i = 0; i < fList.length; i++) {
                fileList[i] = new Item(fList[i], R.drawable.ic_launcher);

                // Convert into file path
                File sel = new File(path, fList[i]);

                // Set drawables
                if (sel.isDirectory()) {
                    fileList[i].icon = R.drawable.ic_launcher;
                    Log.d("DIRECTORY", fileList[i].file);
                } else {
                    Log.d("FILE", fileList[i].file);
                }
            }

            if (!firstLvl) {
                Item temp[] = new Item[fileList.length + 1];
                for (int i = 0; i < fileList.length; i++) {
                    temp[i + 1] = fileList[i];
                }
                temp[0] = new Item("Back", R.drawable.ic_launcher);
                fileList = temp;
            }
        } else {
            Log.e(TAG, "path does not exist");
        }

        adapter = new ArrayAdapter<Item>(this,
                android.R.layout.select_dialog_item, android.R.id.text1,
                fileList) {
            @Override
            public View getView(int position, View convertView, ViewGroup parent) {
                // creates view
                View view = super.getView(position, convertView, parent);
                TextView textView = (TextView) view
                        .findViewById(android.R.id.text1);

                // put the image on the text view
                textView.setCompoundDrawablePadding(
                        fileList[position].icon);



                return view;
            }
        };

    }

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

        public Item(String file, Integer icon) {
            this.file = file;
            this.icon = icon;
        }

        @Override
        public String toString() {
            return file;
        }
    }

    @Override
    protected Dialog onCreateDialog(int id) {
        Dialog dialog = null;
        AlertDialog.Builder builder = new Builder(this);

        if (fileList == null) {
            Log.e(TAG, "No files loaded");
            dialog = builder.create();
            return dialog;
        }

        switch (id) {
        case DIALOG_LOAD_FILE:
            builder.setTitle("Choose your file");
            builder.setAdapter(adapter, new DialogInterface.OnClickListener() {
                @Override
                public void onClick(DialogInterface dialog, int which) {
                    chosenFile = fileList[which].file;
                    File sel = new File(path + "/" + chosenFile);
                    if (sel.isDirectory()) {
                        firstLvl = false;

                        // Adds chosen directory to list
                        str.add(chosenFile);
                        fileList = null;
                        path = new File(sel + "");

                        loadFileList();

                        removeDialog(DIALOG_LOAD_FILE);
                        showDialog(DIALOG_LOAD_FILE);
                        Log.d(TAG, path.getAbsolutePath());


                    }

                    // Checks if 'up' was clicked
                    else if (chosenFile.equalsIgnoreCase("Back") && !sel.exists()) {

                        // present directory removed from list

                        String s = str.remove(str.size() - 1);

                        // path modified to exclude present directory
                        path = new File(path.toString().substring(0,
                                path.toString().lastIndexOf(s)));
                        fileList = null;

                        // if there are no more directories in the list, then
                        // its the first level
                        if (str.isEmpty()) {
                            firstLvl = true;
                        }
                        loadFileList();

                        removeDialog(DIALOG_LOAD_FILE);
                        showDialog(DIALOG_LOAD_FILE);
                        Log.d(TAG, path.getAbsolutePath());

                    }
                    // File picked
                    else {
                        // Perform action with file picked
                         fileExtension
                          = MimeTypeMap.getFileExtensionFromUrl(sel.toString());
                        // Toast.makeText(getApplication(), fileExtension, Toast.LENGTH_LONG).show();

                            try{
                            //  ArrayList<String> MyFiles = new ArrayList<String>();
                           FileInputStream fIn = new FileInputStream(sel);  
                            BufferedReader myReader = new BufferedReader(  
                                    new InputStreamReader(fIn));  

                            while ((aDataRow = myReader.readLine()) != null) {  
                                aBuffer.append(aDataRow.toString()).append("\n"); 

                            }
                           // aBuffer1 = aBuffer.toString();
                            myReader.close();  




                            }catch (FileNotFoundException e) 
                        {e.printStackTrace();}  
                        catch (IOException e) {  
                            e.printStackTrace();  
                        }  
                     // Toast.makeText(getApplicationContext(),aBuffer,Toast.LENGTH_LONG).show();  
                        Intent returnIntent = new Intent();
                        returnIntent.putExtra("name", aBuffer.toString());
                        setResult(RESULT_OK, returnIntent);

                        finish();

                    } 
                    aBuffer.delete(0, aBuffer.length());
                //  aBuffer1=null;  
                    }

                }
            );
            break;
        }
        dialog = builder.show();
        return dialog;  
    }

    @Override
    public void onBackPressed() {
        // TODO Auto-generated method stub
        super.onBackPressed();
        Intent i= new Intent(this, File_Selecter.class);
          startActivity(i);
    }


}
公共类文件\u资源管理器扩展活动{
//存储已遍历目录的名称
ArrayList str=新的ArrayList();
//检查目录结构的第一级是否显示
私有布尔值firstLvl=true;
字符串aDataRow=“”;
静态StringBuilder aBuffer=新StringBuilder();
字符串aBuffer1=“”;
私有静态最终字符串TAG=“F_PATH”;
私有项[]文件列表;
私有文件路径=新文件(Environment.getExternalStorageDirectory().getAbsolutePath());
私有字符串选择文件;
私有静态最终整型对话框\加载\文件=0;
静态字符串fileExtension=“”;
列表适配器;
@凌驾
创建时的公共void(Bundle savedInstanceState){
super.onCreate(savedInstanceState);
loadFileList();
showDialog(对话框加载文件);
Log.d(标记,path.getAbsolutePath());
}
私有void loadFileList(){
试一试{
path.mkdirs();
}捕获(安全异常e){
Log.e(标签“无法在sd卡上写入”);
}
//检查路径是否存在
if(path.exists()){
FilenameFilter筛选器=新建FilenameFilter(){
@凌驾
公共布尔接受(文件目录,字符串文件名){
文件选择=新文件(目录,文件名);
//根据文件是否隐藏进行筛选
返回(sel.isFile()| | sel.isDirectory())
&&!sel.ishiden();
}
};
字符串[]fList=path.list(过滤器);
fileList=新项[fList.length];
for(int i=0;i