我正在尝试附加位于 Sdcard 中某个文件夹中的文件。我可以从图库和其他媒体类型中获取文件,但到目前为止还没有浏览 SD 卡,所以我可以选择文本文件(Doc、docx、pdf 等)。该要求与Android设备的Email-Application中的Email-attach菜单选项非常接近,该选项允许用户通过Intent查看Sdcard文件夹。
如果有人有这样的浏览SD卡及其内容的经验,请分享代码或指导我。提前谢谢了。
最近我使用以下代码打开但它没有显示 Sdcard 选项`:-
Uri startDir = Uri.fromFile(new File(Environment.getExternalStorageDirectory()
.getAbsolutePath()));
Intent intent = new Intent();
intent.setData(startDir);
intent.setType("*/*");
intent.setAction(Intent.ACTION_VIEW);
startActivity(intent);
最佳答案
试试这个...
public OnClickListener buttonOnClickBrowse = new OnClickListener() {
@Override
public void onClick(View v)
{
loadFileList();
showDialog(DIALOG_LOAD_FILE);
}
};
//*************************************
ArrayList<String> str = new ArrayList<String>();
// Check if the first level of the directory structure is the one showing
private Boolean firstLvl = true;
private Item [] fileList;
private File path = new File(Environment.getExternalStorageDirectory() + "");
private String chosenFile;
private static final int DIALOG_LOAD_FILE = 1000;
ArrayAdapter adapter1;
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.file_icon);
// Convert into file path
File sel = new File(path , fList [i]);
// Set drawables
if (sel.isDirectory())
{
fileList [i].icon = R.drawable.directory_icon;
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("Up" , R.drawable.directory_up);
fileList = temp;
}
} else
{
Log.e(TAG , "path does not exist");
}
adapter1 = new ArrayAdapter<Item>(ImageReader.this , android.R.layout.select_dialog_item , android.R.id.text1 , fileList) {
@Override
public View getView(int position , View convertView , ViewGroup parent)
{
View view = super.getView(position , convertView , parent);
TextView textView = (TextView) view.findViewById(android.R.id.text1);
// put the image on the text view
textView.setCompoundDrawablesWithIntrinsicBounds(fileList [position].icon , 0 , 0 , 0);
// add margin between image and text (support various
// screen
// densities)
int dp5 = (int) (5 * getResources().getDisplayMetrics().density + 0.5f);
textView.setCompoundDrawablePadding(dp5);
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;
}
}
File setetedFileName;
@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 ur file");
builder.setAdapter(adapter1 , new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog , int which)
{
chosenFile = fileList [which].file;
File sel = new File(path + "/" + chosenFile);
setetedFileName = sel;
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("up") && !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
Toast.makeText(getApplicationContext() , "" + setetedFileName.getAbsolutePath() , Toast.LENGTH_SHORT).show();
}
}
});
break;
}
dialog = builder.show();
return dialog;
}
// *******************************************************************
关于java - 如何在 Android 中打开 Intent Chooser 来浏览 Sdcard 文件夹及其内容,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20323204/