1

这是我的场景

  1. 用户从我的应用下载文件

  2. 用户按下菜单按钮转到位于 SD 卡中的下载文件

请告诉我如何实现一个链接到 SD 卡位置的按钮

以下是我的代码。

File sdCard = Environment.getExternalStorageDirectory();
File dir = new File (sdCard.getAbsolutePath() + "/downloaded/stuffs");
dir.mkdirs();

File file = new File(dir, fileName);

InputStream input = new BufferedInputStream(url.openStream());
FileOutputStream f = new FileOutputStream(file);
4

2 回答 2

2

您的按钮处理程序

myButton.setOnClickListener(new OnClickListener() {

public void onClick(View v) {
    Intent intent = new Intent( this, FileList.class);
    startActivity(intent);

 }
});

并显示该目录中的所有文件,像这样定义类 FileList

public class FileList extends ListActivity 
{
    private File file;
    private List<String> myList;

    public void onCreate(Bundle savedInstanceState) 
    {
        super.onCreate(savedInstanceState);

        myList = new ArrayList<String>();   

        File sdCard = Environment.getExternalStorageDirectory();
        File dir = new File (sdCard.getAbsolutePath() + "/downloaded/stuffs");

        File list[] = dir.listFiles();

        for( int i=0; i< list.length; i++)
        {
            myList.add( list[i].getName() );
        }

        setListAdapter(new ArrayAdapter<String>(this,
        android.R.layout.simple_list_item_1, myList ));

    }
}
于 2012-08-03T08:09:27.687 回答
0

试试这个 Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

于 2012-08-03T10:48:32.900 回答