KC Chai
KC Chai

Reputation: 1617

Android Button to show downloaded files

Here's my scenario

  1. User downloads a file from my app

  2. User presses the menu button to go to the downloaded files located in SD Card

Please show me how to implement a button to link to SD CARD location

below are my codes.

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);

Upvotes: 1

Views: 266

Answers (2)

Yash
Yash

Reputation: 1751

try this Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS);

Upvotes: 0

Imdad Sarkar
Imdad Sarkar

Reputation: 1235

Your button handler

myButton.setOnClickListener(new OnClickListener() {

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

 }
});

And to show all files in that directory define class FileList like this

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 ));

    }
}

Upvotes: 2

Related Questions