2010-10-22 77 views
5

我想將圖像保存在不同的移動手機,如何獲取android galley文件夾的路徑動態?

Android攝像頭畫廊文件夾的路徑是不同的。例如在星系是

/sdcard/DCIM/camera/image.jpg 

,並在其他手機例如

/sdcard/media/images/image.jpg 

熱點動態獲取圖庫文件夾的路徑?

任何幫助,將不勝感激。

回答

3

您可以使用下面的示例文件資源管理器上顯示爲您的手機的所有的東西,你會從galary

選擇你的項目FileExplorer.java

package com.fileUpload; 

    import java.io.File; 
    import java.util.ArrayList; 
    import java.util.List; 
    import android.app.AlertDialog; 
    import android.app.ListActivity; 
    import android.content.DialogInterface; 
    import android.content.Intent; 
    import android.os.Bundle; 
    import android.view.View; 
    import android.widget.ArrayAdapter; 
    import android.widget.ListView; 
    import android.widget.TextView; 

    public class FileExplorer extends ListActivity { 

    private List<String> item; 
    private List<String> path; 
    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.file); 
    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,android.R.layout.simple_list_item_1, 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.getAbsolutePath() + "] 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 
    { 
     Bundle b=new Bundle(); 
     b.putString("param1",file.getAbsolutePath()); 
     Intent i=new Intent(this,DemoActivity.class); 
     i.putExtras(b); 
     startActivity(i); 

} 
} 
} 
+0

我曾嘗試我們的代碼,但它給NullPointerException異常的在getdir上。 – 2011-11-29 06:02:43

+0

也許現在就試試吧。 – FabianCook 2012-11-08 01:04:07

相關問題