Osama Ahmed
Osama Ahmed

Reputation: 1

Copying database file to sdcard

i found this code from a tutorial but its not working.

saying directory cant be created

    try {

myInput = new FileInputStream("//data//net.learn2develop.Databases//databases//MyDB");//this is
// the path for all apps
//insert your package instead packagename,ex:com.mybusiness.myapp

// Set the output folder on the SDcard
// File directory = new File(Environment.getExternalStorageDirectory().getPath());

                File directory = new File("/sdcard/some_folder"); 

                // Create the folder if it doesn't exist:
                if (!directory.exists()) 
                {
                    directory.mkdirs();
                } 
                // Set the output file stream up:

               // OutputStream myOutput = new FileOutputStream("/sdcard/");
                OutputStream myOutput = new FileOutputStream(directory.getPath()+
                         "/database_name.backup");


                // Transfer bytes from the input file to the output file
                byte[] buffer = new byte[1024];
                int length;
                while ((length = myInput.read(buffer))>0)
                {
                    myOutput.write(buffer, 0, length);
                }
                // Close and clear the streams





                myOutput.flush();

                myOutput.close();

                myInput.close();

Upvotes: 0

Views: 497

Answers (6)

MinceMan
MinceMan

Reputation: 7592

Plenty of helpful answers here. I just want to give another example of how one can do this.

private final static String EXTERNAL_DIRECTORY = "/Android/data/{Name-Space}/databases/";
private final static String INTERNAL_DIRECTORY = "/data/data/com.fitocracy.app/databases/";

public static void copyDatabaseToSdcard() {
        String state = Environment.getExternalStorageState();

    if (Environment.MEDIA_MOUNTED.equals(state)) {  
        BufferedInputStream is = null;
        BufferedOutputStream os = null;
        try {
            is = getFIS();
            os = getFOS();

            int current = 0;
            while ((current = is.read()) != -1) {
                os.write(current);
            }

            os.flush();
            os.close();
            is.close();

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }

    }
}

private static BufferedInputStream getFIS() throws FileNotFoundException {
    File db = new File(INTERNAL_DIRECTORY + DATABASE_NAME);
    return new BufferedInputStream(new FileInputStream(db));
}

private static BufferedOutputStream getFOS() throws FileNotFoundException {
    String path = Environment.getExternalStorageDirectory().getAbsolutePath();
    File dir = new File(path + EXTERNAL_DIRECTORY);
    if (!dir.exists()) {
        dir.mkdirs();
    }
    File db = new File(dir, DATABASE_NAME);
    return new BufferedOutputStream(new FileOutputStream(db));
}

Upvotes: 0

No_Rulz
No_Rulz

Reputation: 2719

Try this :)

String path=Environment.getExternalStorageDirectory().getAbsolutePath();
File directory = new File(path+"your directory");

And Check the permission in manifest

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 0

preeya
preeya

Reputation: 169

The below code work fro me :):).. Hope it helps you too .

public class FileDownloader implements IJuicerDownloader {
public static final String tag = "FileDownloader";
public void download(String downloadFileWebPath, File savedFilename ) throws Exception 
{

    try
    {
        URL myFileUrl =null; 
        myFileUrl= new URL(downloadFileWebPath);
        Log.d(tag,"downloadFileWebPath " + downloadFileWebPath );
        HttpURLConnection conn= (HttpURLConnection)myFileUrl.openConnection();
        conn.setDoInput(true);
        conn.connect();
        InputStream downloadFile = conn.getInputStream();
        int bytes = downloadFile.available();
        FileOutputStream fos = new FileOutputStream(savedFilename);          
        BufferedInputStream bis = new BufferedInputStream(downloadFile);
        ByteArrayBuffer baf = new ByteArrayBuffer(5000);
         int current = 0;
         while ((current = bis.read()) != -1) {
             baf.append((byte) current);
          }

          fos.write(baf.toByteArray());
          fos.flush();
          fos.close();

    }

    catch (MalformedURLException e) {
        // TODO Auto-generated catch block


        e.printStackTrace();
       //  throw your own exception
        }
    catch (FileNotFoundException e) {

        e.printStackTrace();
           //  throw your own exception
    }catch ( UnknownServiceException  e) {


        e.printStackTrace();

           //  throw your own exception
        } catch (IOException e) {

        e.printStackTrace();
           //  throw your own exception
    }

}

-Preeya

Upvotes: 0

tasomaniac
tasomaniac

Reputation: 10342

This is wrong

"//data//net.learn2develop.Databases//databases//MyDB"

It should be "/data/data/net.learn2develop.Databases/databases/MyDB"

Besides, never harcode "sdcard" string in your path

You should get the output file as follows:

File directory = new File(Environment.getExternalStorageDirectory(), "directory_name");
directory.makeDirs();

Also check your permission in the Manifest file

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 2

Bhavdip Sagar
Bhavdip Sagar

Reputation: 1971

I think your database path it might be wrong,use following way, in my case :

    public static String PRIVATE_DATA = "/data/data/<your package name>/databases/<your db name>";

also add permission for sd card write over external storage device in your androidManifest.xml

Upvotes: 0

May be, you need add permission to Manifest:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

Upvotes: 1

Related Questions