2011-01-08 14 views
3

私はちょうどエミュレータのsdcardからファイルの内容を(イメージファイル/ビデオファイル/そのような音楽ファイルとして)表示したいと思います。アンドロイドのsdcardからデータを読み込む

以下は私のコードです。

public class listfiles extends ListActivity { 
private ArrayList<String> item = null; 
private ArrayList<String> path = null; 
private String root="/"; 
private TextView myPath; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     setContentView(R.layout.sub); 
     myPath = (TextView)findViewById(R.id.path); 
     getDir(root); 
    } 

    private void getDir(String dirPath) 
    { 
    myPath.setText("Location: " + dirPath); 

    item = new ArrayList<String>(); 
    path = new ArrayList<String>(); 

    File f = new File(dirPath); 
    File[] files = f.listFiles(); 

    if(!dirPath.equals(root)) 
    { 

     item.add(root); 
     path.add(root); 

     item.add("../"); 
     path.add(f.getParent()); 

    } 

    for(int i=0; i < files.length; i++) 
    { 
     File file = files[i]; 
     path.add(file.getPath()); 
     if(file.isDirectory()) 
     item.add(file.getName() + "/"); 
     else 
     item.add(file.getName()); 
    } 

    ArrayAdapter<String> fileList = 
     new ArrayAdapter<String>(this, R.layout.row, item); 
    setListAdapter(fileList); 
    } 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    File file = new File(path.get(position)); 

    if (file.isDirectory()) 
    { 
    if(file.canRead()) 
    getDir(path.get(position)); 
    else 
    { 
    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.icon) 
    .setTitle("[" + file.getName() + "] folder can't be read!") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     } 
     }).show(); 
    } 
    } 
    else 
    { 
    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.icon) 
    .setTitle("[" + file.getName() + "]") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 

     @Override 
     public void onClick(DialogInterface dialog, int which) { 
     // TODO Auto-generated method stub 
     } 
     }).show(); 
    } 
} 
} 

出力にはファイルパス&があります。しかし、ファイルをクリックすると、内容は表示されません。私はそのために何をすべきですか? it.Myコードを以下に示し訂正おかげ

は最終的に私は..

public class SDCardActivity extends ListActivity { 
private List<String> item = null; 
private List<String> path = null; 
private String root="/sdcard"; 
private TextView myPath; 

    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 
     // Intent intent=getIntent(); 

     setContentView(R.layout.sub); 
     myPath = (TextView)findViewById(R.id.path); 
     getDir(root); 
    } 

    private void getDir(String dirPath) 
    { 
    myPath.setText("Location: " + dirPath); 

    item = new ArrayList<String>(); 
    path = new ArrayList<String>(); 

    File f = new File(dirPath); 
    File[] files = f.listFiles(); 

    if(!dirPath.equals(root)) 
    { 

     item.add(root); 
     path.add(root); 

     item.add("../"); 
     path.add(f.getParent()); 

    } 

    for(int i=0; i < files.length; i++) 
    { 
     File file = files[i]; 
     path.add(file.getPath()); 
     if(file.isDirectory()) 
     item.add(file.getName() + "/"); 
     else 
     item.add(file.getName()); 
    } 

    ArrayAdapter<String> fileList = 
     new ArrayAdapter<String>(this, R.layout.row, item); 
    setListAdapter(fileList); 
    } 

@Override 
protected void onListItemClick(ListView l, View v, int position, long id) { 

    File file = new File(path.get(position)); 

    if (file.isDirectory()) 
    { 
    if(file.canRead()) 
    getDir(path.get(position)); 
    else 
    { 
    new AlertDialog.Builder(this) 
    .setIcon(R.drawable.icon) 
    .setTitle("[" + file.getName() + "] folder can't be read!") 
    .setPositiveButton("OK", 
     new DialogInterface.OnClickListener() { 

     public void onClick(DialogInterface dialog, int which){ 
     // TODO Auto-generated method stub 
      dialog.dismiss(); 
     } 
     }).show(); 
    } 
    } 
    else 
    { 
     Intent intent = new Intent(); 
     intent.setAction(Intent.ACTION_VIEW); 
     Uri uri = Uri.parse("file://" + file.getPath()); 
     String fname=file.getName(); 
     if(fname.endsWith(".jpeg")||fname.endsWith("png")||fname.endsWith(".gif")) 
     { 
      intent.setDataAndType(uri, "image/*"); 
      startActivity(intent); 
     } 
     else if(fname.endsWith(".mp4")||fname.endsWith(".3gp")) 
     { 
      intent.setDataAndType(uri, "video/*"); 
      startActivity(intent); 
     } 
     else if(fname.endsWith(".mp3")) 
     { 
      intent.setDataAndType(uri, "audio/*"); 
      startActivity(intent); 
     } 
     else 
      try { 
       EditText tv = (EditText)findViewById(R.id.tn); 
       StringBuilder text = new StringBuilder(); 

       BufferedReader br = new BufferedReader(new FileReader(file)); 
       String line; 

       while ((line = br.readLine()) != null) { 
        text.append(line); 
        text.append('\n'); 

        //Set the text 
        tv.setText(text); 

       } 
      }//try 
      catch (IOException e) { 
       //You'll need to add proper error handling here 
      }//catch 

    } 
} 
} 

答えて

4

たぶん私はあなたのコードでそれを見逃してしまったが、私はそれでどんなIntentを見つけることができませんでした。あなたは、表示させたいファイルのためにIntentACTION_VIEWフラグで呼び出さなければなりません。

たとえば、

Intent intent = new Intent(); 
intent.setAction(Intent.ACTION_VIEW); 
Uri imgUri = Uri.parse("file://" + file.getPath()); 
intent.setDataAndType(imgUri, "image/*"); 
startActivity(intent); 

あなたは、単純な私たちのケースでACTION_VIEWあるアクションを設定し、Intentのインスタンスを作成します。次に、ファイルオブジェクトのパスをfile://に連結してUriオブジェクトを作成します。今すぐ行う必要があるのは、uriとタイプ文字列を指定してデータを設定し、意図を入力することだけです。私の例ではすべての画像タイプ。ただし、特定の画像タイプを指定することはできます。あなたの意図が設定され、準備が整ったら、インテントをパラメータとしてアクティビティを開始することでそれを消します。

Androidは、目的のデータを表示するために適切なアプリケーションを見つけようとします。

+0

返信いただきありがとうございます。私はあなたのコードを私のところに挿入しました。 – sanjay

+0

@Sudha onListItemClick –

+0

ありがとうございました。私はついにそれを得ました。 – sanjay

8

以下のコードは、SDcard.simpleからファイルの内容を読み取る方法を示しています.Sdcardに1つのテキストファイルを挿入し、プログラム内に以下のコードを実装します。

try{ 
      File f = new File(Environment.getExternalStorageDirectory()+"/f1.txt"); 
      fileIS = new FileInputStream(f); 
      BufferedReader buf = new BufferedReader(new InputStreamReader(fileIS)); 
      String readString = new String(); 
      //just reading each line and pass it on the debugger 
      while((readString = buf.readLine())!= null){ 
       textdata.setText(readString); 
       Log.d("line: ", readString); 
      } 
     } catch (FileNotFoundException e) { 
      e.printStackTrace(); 
     } catch (IOException e){ 
      e.printStackTrace(); 
     } 
関連する問題