Ali
Ali

Reputation: 61

chaging directory saved in sd card

I've created this code to save a pdf file in sd card, but I want to change the directory that has the saved files, from /sdcard/, to /sdcard/MYDIR/

 try {
            URL url = new URL(f_url[0]);
            URLConnection conection = url.openConnection();
            conection.connect();
            // getting file length
            int lenghtOfFile = conection.getContentLength();

            // input stream to read file - with 8k buffer
            InputStream input = new BufferedInputStream(url.openStream(), 8192);

            // Output stream to write file
            OutputStream output = new FileOutputStream("/sdcard/yes.pdf");

Upvotes: 1

Views: 399

Answers (2)

WarrenFaith
WarrenFaith

Reputation: 57672

The class you need is File. There you have methods like mkdirs() that creates the necessary directories.

You should make sure that you don't use hard coded paths in your application. On some devices your "/sdcard/" will fail. Check the class Environment and use the getExternalStorageDirectory() to get the path of the sd card.

Upvotes: 0

CommonsWare
CommonsWare

Reputation: 1006614

To create a directory in Java, use mkdir() or mkdirs() on File.

To correctly create a directory or file on external storage on Android, do not hard-code /sdcard, largely because it is the wrong value on most Android devices. Use Environment.getExternalStorageDirectory() to access the root of external storage.

File dir=new File(Environment.getExternalStorageDirectory(), "MYDIR");

dir.mkdir();

OutputStream output=new FileOutputStream(new File(dir, "yes.pdf"));

Upvotes: 1

Related Questions