2013-11-04 56 views
0

我點擊它時無法打開文本文件我的程序轉到開始頁面。我看到許多代碼 您在哪裏知道文本文件的確切位置,但我不知道它的確切位置時如何打開文本文件。如何在列表視圖中單擊時打開文本文件

其中包含3個按鍵,當我按下瀏覽按鈕的應用程序在手機打開所有文件

package com.example.fileexplorerb; 

import java.io.BufferedReader; 
import java.io.File; 
import java.io.FileNotFoundException; 
import java.io.IOException; 
import java.io.InputStream; 
import java.io.InputStreamReader; 
import java.util.ArrayList; 
import java.util.List; 

import android.net.Uri; 
import android.os.Bundle; 
import android.app.Activity; 
import android.content.Intent; 
import android.util.Log; 
import android.view.Menu; 
import android.view.View; 
import android.view.View.OnClickListener; 
import android.widget.AdapterView; 
import android.widget.ArrayAdapter; 
import android.widget.Button; 
import android.widget.TextView; 

public class MainActivity extends Activity implements OnClickListener{ 

    private static final int REQUEST_PATH = 1; 

    Button browseButton; 
    Button exitButton; 
    Button nextButton; 

    String curFileName; 




    @Override 
    protected void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.activity_main); 

     browseButton = (Button)findViewById(R.id.button1); 
     browseButton.setOnClickListener(this); 

     exitButton = (Button)findViewById(R.id.button2); 
     exitButton.setOnClickListener(this); 

     nextButton= (Button)findViewById(R.id.button3); 
     nextButton.setOnClickListener(this); 





    } 

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

    protected void onActivityResult(int requestCode, int resultCode, Intent data){ 
     // See which child activity is calling us back. 
     if (requestCode == REQUEST_PATH){ 
      if (resultCode == RESULT_OK) { 
       curFileName = data.getStringExtra("GetFileName"); 
       //edittext.setText(curFileName); 
      } 
     } 
    } 

    @Override 
    public void onClick(View v) { 

     if (v.getId() == R.id.button1){ 
      Intent intent1 = new Intent(this, FileChooser.class); 
      startActivityForResult(intent1,REQUEST_PATH); 
     } 
     if (v.getId() == R.id.button2){ 
      finish(); 
     } 

     if (v.getId() == R.id.button3){ 
      //Intent intent2 = new Intent(this, FileChooser.class); 
      //startActivityForResult(intent2,REQUEST_PATH); 

      Intent intent = new Intent(Intent.ACTION_VIEW); 
      intent.setType("application/txt"); 

      startActivity(intent); 
     } 
    } 


} 

文件ArrayAdapter顯示文件大小,創建日期時

主要活動的Open XML和名稱

package com.example.fileexplorerb; 

import java.util.List; 

import android.content.Context; 
import android.graphics.drawable.Drawable; 
import android.view.LayoutInflater; 
import android.view.View; 
import android.view.ViewGroup; 
import android.widget.ArrayAdapter; 
import android.widget.ImageView; 
import android.widget.TextView; 


public class FileArrayAdapter extends ArrayAdapter<Item>{ 

    private Context c; 
    private int id; 
    private List<Item>items; 

    public FileArrayAdapter(Context context, int textViewResourceId, 
      List<Item> objects) { 
     super(context, textViewResourceId, objects); 
     c = context; 
     id = textViewResourceId; 
     items = objects; 
    } 
    public Item getItem(int i) 
    { 
     return items.get(i); 
    } 
    @Override 
     public View getView(int position, View convertView, ViewGroup parent) { 
       View v = convertView; 
       if (v == null) { 
        LayoutInflater vi = (LayoutInflater)c.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
        v = vi.inflate(id, null); 
       } 

       /* create a new view of my layout and inflate it in the row */ 
      //convertView = (RelativeLayout) inflater.inflate(resource, null); 

       final Item o = items.get(position); 
       if (o != null) { 
         TextView t1 = (TextView) v.findViewById(R.id.textView1); 
         TextView t2 = (TextView) v.findViewById(R.id.textView2); 
         TextView t3 = (TextView) v.findViewById(R.id.textView3); 
         /* Take the ImageView from layout and set the city's image */ 
         ImageView imageCity = (ImageView) v.findViewById(R.id.imageView1); 
         String uri = "drawable/" + o.getImage(); 
         int imageResource = c.getResources().getIdentifier(uri, null, c.getPackageName()); 
         Drawable image = c.getResources().getDrawable(imageResource); 
         imageCity.setImageDrawable(image); 

         if(t1!=null) 
         t1.setText(o.getName()); 
         if(t2!=null) 
          t2.setText(o.getData()); 
         if(t3!=null) 
          t3.setText(o.getDate()); 

       } 
       return v; 
     } 

} 

FileChooser將圖像設置爲文件並且

package com.example.fileexplorerb; 

import java.io.File; 
import java.sql.Date; 
import java.util.ArrayList; 
import java.util.Collections; 
import java.util.List; 
import java.text.DateFormat; 
import android.os.Bundle; 
import android.app.ListActivity; 
import android.content.Intent; 
import android.view.View; 
import android.widget.ListView; 

public class FileChooser extends ListActivity { 

    private File currentDir; 
    private FileArrayAdapter adapter; 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     currentDir = new File("/sdcard/"); 
     fill(currentDir); 
    } 
    private void fill(File f) 
    { 
     File[]dirs = f.listFiles(); 
     this.setTitle("Current Dir: "+f.getName()); 
     List<Item>dir = new ArrayList<Item>(); 
     List<Item>fls = new ArrayList<Item>(); 
     try{ 
      for(File ff: dirs) 
      { 
       Date lastModDate = new Date(ff.lastModified()); 
       DateFormat formater = DateFormat.getDateTimeInstance(); 
       String date_modify = formater.format(lastModDate); 
       if(ff.isDirectory()){ 


        File[] fbuf = ff.listFiles(); 
        int buf = 0; 
        if(fbuf != null){ 
         buf = fbuf.length; 
        } 
        else buf = 0; 
        String num_item = String.valueOf(buf); 
        if(buf == 0) num_item = num_item + " item"; 
        else num_item = num_item + " items"; 

        //String formated = lastModDate.toString(); 
        dir.add(new Item(ff.getName(),num_item,date_modify,ff.getAbsolutePath(),"directory_icon")); 
       } 
       else 
       { 

        fls.add(new Item(ff.getName(),ff.length() + " Byte", date_modify, ff.getAbsolutePath(),"file_icon")); 
       } 
      } 
     }catch(Exception e){  

     } 
     Collections.sort(dir); 
     Collections.sort(fls); 
     dir.addAll(fls); 
     if(!f.getName().equalsIgnoreCase("sdcard")) 
      dir.add(0,new Item("..","Parent Directory","",f.getParent(),"directory_up")); 
     adapter = new FileArrayAdapter(FileChooser.this,R.layout.file_view,dir); 
     this.setListAdapter(adapter); 
    } 
    @Override 
    protected void onListItemClick(ListView l, View v, int position, long id) { 
     // TODO Auto-generated method stub 
     super.onListItemClick(l, v, position, id); 
     Item o = adapter.getItem(position); 
     if(o.getImage().equalsIgnoreCase("directory_icon")||o.getImage().equalsIgnoreCase("directory_up")){ 
       currentDir = new File(o.getPath()); 
       fill(currentDir); 
     } 
     else 
     { 
      onFileClick(o); 
     } 
    } 
    private void onFileClick(Item o) 
    { 
     //Toast.makeText(this, "Folder Clicked: "+ currentDir, Toast.LENGTH_SHORT).show(); 
     Intent intent = new Intent(); 
     intent.putExtra("GetPath",currentDir.toString()); 
     intent.putExtra("GetFileName",o.getName()); 
     setResult(RESULT_OK, intent); 
     finish(); 
    } 



} 



XML for main activity shows main menu when you open application 


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

    <Button 
     android:id="@+id/button1" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignParentBottom="true" 
     android:layout_marginBottom="142dp" 
     android:text="Browse" /> 

    <Button 
     android:id="@+id/button2" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button1" 
     android:layout_alignTop="@+id/button1" 
     android:layout_marginTop="63dp" 
     android:text="Exit" /> 

    <Button 
     android:id="@+id/button3" 
     android:layout_width="match_parent" 
     android:layout_height="wrap_content" 
     android:layout_alignLeft="@+id/button2" 
     android:layout_below="@+id/button2" 
     android:layout_marginTop="14dp" 
     android:text="Next" /> 

</RelativeLayout> 

file_view XML shows al l手機中的文件

file view 
<?xml version="1.0" encoding="utf-8"?> 
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="match_parent" 
    android:layout_height="match_parent" > 

    <ImageView 
     android:id="@+id/imageView1" 
     android:layout_width="50dip" 
     android:layout_height="50dip" /> 

    <TextView 
     android:id="@+id/textView1" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_above="@+id/textView2" 
     android:layout_marginLeft="36dp" 
     android:layout_toRightOf="@+id/imageView1" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/textView2" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBottom="@+id/imageView1" 
     android:layout_alignLeft="@+id/textView1" 
     android:text="TextView" /> 

    <TextView 
     android:id="@+id/textView3" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:layout_alignBaseline="@+id/textView2" 
     android:layout_alignBottom="@+id/textView2" 
     android:layout_alignParentRight="true" 
     android:layout_marginRight="19dp" 
     android:text="TextView" /> 

</RelativeLayout> 

回答

0

您的FileChooser活動還應返回文件的完整路徑。當你在使用private void fill(File f)中的目錄時,它必須知道它 - 在某處存儲f,稍後用它來組合目錄路徑和文件名本身。

相關問題