0

我从教程中找到了这段代码,但它不起作用。

说目录不能创建

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

6 回答 6

2

这是错误的

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

它应该是"/data/data/net.learn2develop.Databases/databases/MyDB"

此外,永远不要"sdcard"在你的路径中对字符串进行硬编码

你应该得到如下输出文件:

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

还要检查您在清单文件中的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-12-27T12:45:26.140 回答
1

可能是,您需要向 Manifest 添加权限:

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-12-27T12:42:31.030 回答
0

尝试这个 :)

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

并检查清单中的权限

<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />
于 2012-12-27T12:43:56.010 回答
0

我认为您的数据库路径可能是错误的,在我的情况下使用以下方式:

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

还在您的 androidManifest.xml 中添加对 sd 卡写入外部存储设备的权限

于 2012-12-27T12:44:11.050 回答
0

下面的代码对我有用:):)..希望它也能帮助你。

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
    }

}

-普雷亚

于 2012-12-27T12:47:03.020 回答
0

这里有很多有用的答案。我只想举另一个例子来说明如何做到这一点。

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));
}
于 2013-12-16T05:24:57.027 回答